Professional Documents
Culture Documents
Perl Directories Process MGMT
Perl Directories Process MGMT
Perl Directories Process MGMT
Introduction
There are standard functions in perl to play with directories.
Syntax
Meaning
To open a directory
readdir DIRHANDLE
To read a directory
rewinddir DIRHANDLE
telldir DIRHANDLE
closedir DIRHANDLE
Closing a directory
VIT - SENSE
23-01-2015
VIT - SENSE
23-01-2015
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";
string.
4
VIT - SENSE
23-01-2015
VIT - SENSE
23-01-2015
location.
Note : You need to have required permission to change a directory and
VIT - SENSE
23-01-2015
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.
other
Example : rename "over_there/some/place/some_file", "some_file";
VIT - SENSE
23-01-2015
Introduction
%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
VIT - SENSE
23-01-2015
Backstick Operator
Example :
#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files){
print $file\n;
}
1;
10
VIT - SENSE
23-01-2015
Backstick Operator
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
11
VIT - SENSE
23-01-2015
system() Function
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 scurries off to
VIT - SENSE
23-01-2015
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
13
VIT - SENSE
23-01-2015
fork() Function
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);
14
VIT - SENSE
23-01-2015
fork() Function
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
15
VIT - SENSE
23-01-2015
kill() Function
kill('KILL', (Process List)) function can be used to terminate a pseudo-process
Example : #To send SIG INT to the process IDs 104 and 102
kill('INT', 104, 102);
1;
16
VIT - SENSE
23-01-2015
VIT - SENSE
23-01-2015
Thank You