Perl Directories Process MGMT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Perl Directories

Prof. C. Prayline Rajabai


Assistant Professor
SENSE
VIT University

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

VIT - SENSE

23-01-2015

Examples to print all the files in the directory


Example 1 :
opendir (DIR, '.') or die "Couldn't open directory, $!";
while ($file = readdir DIR) {
print "$file\n";
}
closedir DIR;

Example 2 : # To print only the .c files


opendir(DIR, '.') or die "Couldn't open directory, $!";
foreach (sort grep(/^.*\.c$/,readdir(DIR))){
print "$_\n";
}
closedir DIR;
3

VIT - SENSE

23-01-2015

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";

$! - If used in a numeric context, yields the current value of the error no

variable, identifying the last system call error.


$! - If used in a string context, yields the corresponding system error

string.
4

VIT - SENSE

23-01-2015

Directory Access Commands


We can use rmdir function to remove a directory.
Note : Necessary permission is required to remove a directory and the

directory should be empty before you try to remove it.


Example :
$dir = "/tmp/perl";
# This removes perl directory from /tmp directory.
rmdir( $dir ) or die "Couldn't remove $dir directory, $!";
print "Directory removed successfully\n";

VIT - SENSE

23-01-2015

Directory Access Commands


We can use chdir function to change a directory and go to new

location.
Note : You need to have required permission to change a directory and

to go inside the new directory.


Example :
#!/usr/bin/perl
$dir = "/home";
# This changes perl directory and moves you inside /home directory.
chdir( $dir ) or die "Couldn't go inside $dir directory, $!";
print "Your new location is $dir\n";

VIT - SENSE

23-01-2015

Removing and Renaming Files


In perl we use the 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";

VIT - SENSE

23-01-2015

Perl - 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.

VIT - SENSE

23-01-2015

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;

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

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 scurries off to

perform the requested action while Perl naps.


12

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

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

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

and return its status.


If you fork without ever waiting on your children using waitpid()

function, you will accumulate zombies.

15

VIT - SENSE

23-01-2015

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);
1;

16

VIT - SENSE

23-01-2015

defined function - 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
var2 is not defined
17

VIT - SENSE

23-01-2015

Thank You

You might also like