Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Removing a directory

 rmdir() removes an empty directory

EX: program to delete a directory

Ex: create subdirectories in a specified directory


Ex: check for the presence of a file or subdirectory in a specified
directory
#!/usr/bin/perl
opendir(D1,"dir2");

do
{
$line=readdir(D1);
if($line eq "my_nums")
{
print "file is found\n";
exit;
}
}while($line ne "");

print "file not found\n";

closedir(D1);
Ex: create a new file in the specified directory
Unlink(): removes a file
remove(): removes a file
Ex: program to remove a file
rename(oldfile,newfile): renames a file
link(filename, linkname): creates hard links
symlink(filename,linkname): creates symbolic links
chmod(perms,filename): changes file permissions
Random access files
 seek() and tell() helps us to access files randomly.
 seek(filevariable, distance, relative_to): moves file pointer from
one place to another.
 distance: specifies how many bytes to move
 Relative_to: specifies from which location, the pointer has to be
moved.0 indicates from beginning, 1 indicates from current
position and 2 indicates the end of the file.
 tell(filevariable): specifies where the file pointer is currently.

Subroutines

 A subroutine is a separate block of code designed to perform a


particular task.
 It helps to break the program into smaller parts, making it
easier to read and understand.
 It also helps to perform same task multiple times, needless
duplication.
Defining a subroutine
sub subroutine_name
{
Statements
}
 Keyword ‘sub’ tells Perl interpreter that this is a subroutine
definition.
 Perl subroutine can appear anywhere in a program, even n the
middle of a conditional statement as:
while(1)
{
---
---
sub fn1
{


}
}

Invoking a subroutine
 To call a subroutine, prefix the name of it with an “&” symbol.
Ex:
&subroutine_name([arguments]);

 Though we can write a subroutine anywhere in the program, its


always better to write them at the beginning of the program.
 When a subroutine is defined before it is called, it is called
forward references to the subroutine.
 In this case, there is no need to use ‘&’ symbol to call the
subroutine.
Forward references to a subroutine
 Even though the subroutine is called before defining it inside
the program, we can call it with out the need of ‘&’ , if we
provide forward references for the subroutine.
Returning values from a subroutine

 We can return values from a subroutine using the ‘return’


statement.
 Also, the value of the last expression that is evaluated in the
subroutine is automatically considered as the return value.
Using local variables in subroutines
 Perl has 2 statements to define local variables.
1. ‘my’ statement: which defines variable, whose scope is only
with in the subroutine.
2. ‘local’ statement: defines variables whose scope is with in the
subroutine and also with in the subroutines that were called
inside it.
 ‘my’ statement is available from Perl version 5 only.
 Local variables can be defined any where inside the program,
but its always better to define all of them at the beginning of
the subroutine.
Passing parameters to a subroutine

 Subroutines can accept values passed from the ‘main’ program.


They are called parameters.
 In Perl, the parameters that were passed to a subroutine are
stored in a built-in array called
“@_”
Passing parameters and returning values
‘state’ variables

Passing hashes to a function


Recursive functions

You might also like