Sunteți pe pagina 1din 15

PERL DIRECTORY ACCESS &

PROCESS MANAGEMENT

C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
DIRECTORY ACCESS - INTRODUCTION
There are standard functions in perl to play with directories.

Syntax Meaning
opendir DIRHANDLE, EXPR To open a directory

readdir DIRHANDLE To read a directory

rewinddir DIRHANDLE Positioning pointer to the begining

telldir DIRHANDLE Returns current position of the dir

seekdir DIRHANDLE, POS Pointing pointer to POS inside dir

closedir DIRHANDLE Closing a directory

2
EXAMPLES
Program to print all the files in a directory

opendir (DIR, '.') or die "Couldn't open directory, $!";


while ($file = readdir DIR) {
print "$file\n";
}
closedir DIR;

3
EXAMPLES
Program to print all the files in a directory

opendir(DIR, '.') or die "Couldn't open directory, $!";


foreach (sort grep(/^.*\.c$/,readdir(DIR))){
print "$_\n";
}
closedir DIR;

4
DIRECTORY ACCESS COMMANDS
We use mkdir function to create a new directory.

Example :
$dir = "/tmp/perl";
# This creates perl directory in /tmp directory.
mkdir( $dir ) or die "Couldn't create $dir directory, $!";
print "Directory created successfully\n";

We use chdir function to change to another directory

chdir($dir);

We use rmdir function to remove a directory


5
rmdir($dir);
REMOVING AND RENAMING FILES
In perl, unlink command is used to remove a file or files.

Example : unlink "slate", "bedrock", "lava";

The above command deletes all the three files.

A file can be renamed with the new file name using the
rename function.

Example : rename "old", "new";

You can also use the rename function to move a file from one
directory to the other

Example: rename "over_there/some/place/some_file", "some_file";

6
PROCESS MANAGEMENT - INTRODUCTION
Perl has a feature to create new processes as per your
requirements

Every process created, maintains its own virtual environment


with-in %ENV variable.

The exit() function exits only from the child process which
executes this function.

All open handles are dup()-ed in child-processes, so that closing


any handles in one process does not affect the others.

7
BACKSTICK OPERATOR
Any Unix command can be executed in a perl program by
using the backstick operator.

The result of execution of the command can be stored in a


variable.

Example :
#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files){
print $file\n;
}
1;
8
BACKSTICK OPERATOR CONTD.,
The above program lists down all the files and directories in
the current directory.

Output :
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy

9
SYSTEM() FUNCTION
system() function is also used to execute any Unix command.

The output will go to the output of the perl script.

By default, it is the screen, i.e. STDOUT,.

It can be redirected to any file by using redirection operator


>:
Example :
system( "ls -l");
1;

The output of the above program will be the same as the


previous program.

The system function creates a child process, which then


10
scurries off to perform the requested action while Perl naps.
FORK() FUNCTION
It is used to clone a current process.

create a new process running the same program at the same


point.

It returns the child pid to the parent process, 0r to the child


process, or undef if the fork is unsuccessful.

exec() function can be used within a process to launch the


requested executable, which will be executed in a separate
process area.

exec() will wait for it to complete before exiting with the


same exit status as that process
11
FORK() FUNCTION CONTD.,

Example :

defined(my $pid = fork) or die "Cannot fork: $!";

unless ($pid) {

# Child process is here

exec "date";

die "cannot exec date: $!";

# Parent process is here

waitpid($pid, 0);
12
FORK() FUNCTION CONTD.,

wait() and waitpid() can be passed to a pseudo-process ID


returned by fork().

These calls will properly wait for the termination of the


pseudo-process and return its status.

If you fork without ever waiting on your children using


waitpid() function, you will accumulate zombies.

13
KILL() FUNCTION
kill('KILL', (Process List)) function can be used to
terminate a pseudo-process by passing it the ID
returned by fork().

It may typically cause memory leaks, because the


thread that implements the pseudo-process does not
get a chance to clean up its resources.

kill() function to send any other signal to target


processes

Example : To send SIG INT to the process IDs 104 and 102
kill('INT', 104, 102); 14
1;
DEFINED FUNCTION - EXAMPLE
Example:
$var1 = "This is defined";
if( defined($var1) ){
print "$var1\n";
}
if( defined($var2) ){
print "var2 is also defined\n";
}else{
print "var2 is not defined\n";
}

Output : This is defined


15
var2 is not defined

S-ar putea să vă placă și