0% found this document useful (0 votes)
20 views31 pages

Unit-5 Oss

Uploaded by

then mozhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views31 pages

Unit-5 Oss

Uploaded by

then mozhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

MARUDHAR KESARI JAIN COLLEGE FOR WOMEN, VANIYAMBADI

PG DEPARTMENT OF COMPUTER APPLICATIONS

Class : III BCA


Subject Name : Open Source Software
Subject Code : CCA61

SYLLABUS
UNIT-V

UNIT:V:PERL : Perl backgrounder – Perl overview – Perl parsing rules – Variables and
Data – Statements and Control structures – Subroutines, Packages, and Modules–
Working with Files – Data Manipulation.
Introduction to Perl
Perl is a high-level, interpreted, general-purpose programming language originally developed for text
manipulation. It borrows many features from C and Shell script and is used for system administration,
networking, and other applications that involve user interfaces. It was initially developed by Larry Wall
in 1987 as a scripting language to make report processing easier and is implemented into the C
programming language. Perl refers to the Perl5 version through 2019, when it was redesigned as sister
language, Perl6, before it was altered to Raku in October 2019.
Perl Features
The following list of features are available in Perl and are broadly adopted from other programming and
scripting languages:
Perl gets most of its features from C, including variables, expressions, statements, control structures,
and subroutines
It also borrows features from shell scripting for identifying data types. unambiguously, like an array,
scalar, hash, through leading sigils
Perl also has inbuilt functions which are often used in shell programming, such as sort and system
facilities utilization
Perl5 also has added features to support complex data structures and an object-oriented programming
model that includes packages, references, and directives for the compiler
All of the versions of Perl include auto data typing and memory management; the interpreter understands
storage and memory requirements for each data type, allocates, and deallocates memory based on usage
It also does typecast during the run time like converting an integer to string etc. and other conversions,
which are not legitimate, that are thrown out as errors during execution
Perl doesn’t enforce or recommend any particular programming technique like procedural, object-
oriented, or functional—the interpreter, along with its functions, stands as a single specification of the
language
Perl comes with powerful utilities (APIs) for text manipulation that are useful for working with XML,
HTML, and other markup languages
Perl has the highest level of security and is even certified by a third-party security organization called
Coverity, with low defect density and fewer security flaws
Perl is also extendable and provides libraries to support XML and integration to databases including
Oracle and MySQL

Applications of Perl
Perl is popular among programmers, along with other programming languages like PHP and Python. In
earlier days, programmers used Perl to write CGI scripts. Perl is often used as a departmental glue
between multiple systems that are not homogenous or allowed to interoperate seamlessly. The system
administers love this language as they can enter a single command to accomplish a goal that otherwise
would require a program to be written. Perl is mainly portable, with some degree of customizations
between Windows and macOS.

Developers also use the language to build and deploy. It is used by most of the suppliers or software
manufacturers to package and deploy the software commercially (including COTS and bespoke). It is
widely used in the field of finance and bioinformatics due to its ability to handle and process large
volume data sets.
Perl Implementation
Perl is an interpreted language, as mentioned above, written in C with an extensive collection of modules
written in both C and Perl. The Perl interpreter is a whopping 150,000 lines of C code, which compiles
to 1 MB on most of the system architecture. There are more than 500 modules in Perl distribution with
300, 000 lines of code in Perl and 200,000 lines of code in C. The interpreter is based on the object-
oriented architecture in which the elements of Perl (arrays, scalars, and hashes) are represented as C
structures.
The interpreter passes through two phases in its life cycle compile and run phase. At compile time, the
interpreter parses the Perl code into a syntax tree. At run time, it runs the Perl program by moving along
the tree. Perl language is distributed as open source with 120,000 functional tests; it runs during the
build process and extensively tests the interpreter and other functional modules. If your changes pass all
of these 120K functional tests, you can safely assume that your code will not break the interpreter.

Variables and Types


Perl is a case-sensitive programming language like UNIX. Perl variables always start with $, @, or %,
followed by zero, letters, or other digits, supporting three variable types (namely Scalars, Arrays, and
Hashes). There are no Boolean data types in Perl, and assignment operation is carried out using = sign.
# sign starts with the comment.

Scalars – These variables contain a single string or numeric value and the variable name should start
with $
$item_name = “Orange”

Arrays – These variables are ordered sets of values and are prefixed with @
@item_name=(“Orange”, “Grape”, “Lemon”)

Hashes – these are key-value pairs, and they are prefixed with %
%item_catalog = ("Orange" => 5 , "Grape" => 8, "Lemon” => 24);
Decision Statements
Perl conditional statements allow statements to be executed based on true or false conditions. These
conditional statements include:
if (condition) statement
if (condition) {statement1; statement2;}
if (condition) statement else statement
$x=1

$y=2

if ($x = 1) then {

print “x is less than y”

} else

print “y is greater than x”

The ternary operator ? which is also a conditional operator is used similarly as an alternative to the above
statement.
(condition) ? statement1: statement2

if (condition) elsif (condition) statement else statement


unless (condition) statement
unless (condition) statement else statement
unless (condition) elsif (condition) statement else statement
Looping Statements
Like any other programming language, Perl also implements looping constructs through while and for
statements. It executes the statements in the block repeatedly until the conditions are met. There are two
types of while statements.

WHILE – DO WHILE

The below statement will check for the condition until it is true before executing the block:

$cnt = 5;
while ($cnt > 0) {

print "Timer is: $cnt\n";

$cnt--;

The below statement will check for the condition until it is false before executing the block:

$cnt = 1;

until ($cnt > 10) {

print "Timer is: $cnt\n";

$nt++;

The below block uses do while where the condition is tested or checked after executing the block:

$cnt = 5;

do {

print "Timer is: $cnt\n";

$cnt--;

} while ($cnt > 0)

FOR – FOR EACH

The following for loop statements are similar to C language statements where it initializes, checks the
conditions, and iterates:

for ($cnt = 1 ; $cnt < 10 ; $cnt++) {

print "My timer is: $cnt\n"; }

If you’re working on arrays, for each can be used to perform looping construct:

@cols = ('red', 'blue', ‘green');

foreach $cols (@cols) {


print "Color: $cols\n";

Operator
The operator in Perl is an element that influences the operands in all of the Perl expressions, and it
supports many operators like any other programming language. There are different types of operators in
Perl, including:

Arithmetic operators (addition, subtraction, negation, multiplication, division, modulus, and exponent)
Assignment operators (assignment simple, subtract and assign, addition and assign, multiply and assign,
divide and assign, increment and decrement, exponent and assign)
Bitwise operators (AND, OR, XOR, NOR, Shift Left and Shift Right)
Logical operators (Logical AND, Logical OR, Logical XOR, Logical NOT)
String operators (. for concatenation and x for repeating)
Misc operators (range operators for specifying the range)
Subroutine
Subroutines are critical to any programming language to enhance the modularity. They refer to the group
or collection of statements collectively performing a task in a program, and they can be called from
anywhere in the program, with or without arguments. The general syntax of the subroutine in Perl is
given below:

sub name_of _subroutine {

body of the subroutine

name_of _subroutine (argument list);

For example:

sub Hi_Intro {

print "Hi, this is my first subroutine\n";

The subroutine can be called in the following two ways (with and without ampersand)

Hi_Intro();
&Hi_Intro;

You can also pass arguments to the subroutines depending on the need of the program or the task it
wants to accomplish.

Regular Expressions
The regular expression in Perl is an important feature as it is most helpful in the extraction and reporting
of data (mostly text). The regular expression in Perl is used in numerous ways:

Search string that follows a specific pattern and replacing them with other string based on options
Counting the number of occurrences of a string or number in the line of text or numbers
Date formatting like converting date to mm/dd/yyyy from dd/mm/yyyy
Validation of fields submitted by the user in the front end typically in the HTML format

Perl | Loops (for, foreach, while, do…while, until, Nested loops)

Looping in programming languages is a feature which facilitates the execution of a set of instructions
or functions repeatedly while some condition evaluates to true. Loops make the programmers task
simpler. Perl provides the different types of loop to handle the condition based situation in the program.
The loops in Perl are :

for Loop

“for” loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement
consumes the initialization, condition and increment/decrement in one line thereby providing a shorter,
easy to debug structure of looping.
Syntax:

for (init statement; condition; increment/decrement )


{
# Code to be Executed
}

A for loop works on a predefined flow of control. The flow of control can be determined by the
following :

 init statement: This is the first statement which is executed. In this step, we initialize a
variable which controls the loop.
 condition: In this step, the given condition is evaluated and the for loop runs if it is True.
It is also an Entry Control Loop as the condition is checked prior to the execution of the
loop statements.
 Statement execution: Once the condition is evaluated to true, the statements in the loop
body are executed.
 increment/decrement: The loop control variable is changed here (incremented or
decremented) for updating the variable for next iteration.
 Loop termination: When the condition becomes false, the loop terminates marking the
end of its life cycle.

# Perl program to illustrate


# the for loop

# for loop
for ($count = 1 ; $count <= 3 ; $count++)
{
print "GeeksForGeeks\n"
}
Output:

GeeksForGeeks
GeeksForGeeks
GeeksForGeeks

A foreach loop is used to iterate over a list and the variable holds the value of the elements of the list
one at a time. It is majorly used when we have a set of data in a list and we want to iterate over the
elements of the list instead of iterating over its range. The process of iteration of each element is
done automatically by the loop.
Syntax:

foreach variable
{
# Code to be Executed
}
# Perl program to illustrate
# the foreach loop

# Array
@data = ('GEEKS', 'FOR', 'GEEKS');

# foreach loop
foreach $word (@data)
{
print $word
}
Output:
GEEKSFORGEEKS

A while loop generally takes an expression in parenthesis. If the expression is True then the code
within the body of while loop is executed. A while loop is used when we don’t know the number of
times we want the loop to be executed however we know the termination condition of the loop. It is
also known as a entry controlled loop as the condition is checked before executing the loop. The
while loop can be thought of as a repeating if statement.

Syntax :

while (condition)
{
# Code to be executed
}
# Perl program to illustrate
# the while loop

# while loop
$count = 3;
while ($count >= 0)
{
$count = $count - 1;
print "GeeksForGeeks\n";
}
Output:

GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks

Infinite While Loop: While loop can execute infinite times which means there is no terminating
condition for this loop. In other words, we can say there are some conditions which always remain true,
which causes while loop to execute infinite times or we can say it never terminates.

Example: Below program will print the specified statement infinite time and also give the runtime error
as Output Limit Exceeded on online IDE

# Perl program to illustrate


# the infinite while loop
# infinite while loop
# containing condition 1
# which is always true
while(1)
{
print "Infinite While Loop\n";
}

Output:

Infinite While Loop


Infinite While Loop

Infinite While Loop


Infinite While Loop
.
.
.
.

do…. while loop

A do..while loop is almost same as a while loop. The only difference is that do..while loop runs at least
one time. The condition is checked after the first execution. A do..while loop is used when we want the
loop to run at least one time. It is also known as exit controlled loop as the condition is checked after
executing the loop.
Syntax:

do {

# statements to be Executed

} while(condition);

# Perl program to illustrate


# do..while Loop

$a = 10;

# do..While loop
do {
print "$a ";
$a = $a - 1;
} while ($a > 0);
Output:

10 9 8 7 6 5 4 3 2 1

until loop

until loop is the opposite of while loop. It takes a condition in the parenthesis and it only runs until the
condition is false. Basically, it repeats an instruction or set of instruction until the condition is FALSE.
It is also entry controller loop i.e. first the condition is checked then set of instructions inside a block is
executed.

Syntax:

until (condition)
{
# Statements to be executed
}

# Perl program to illustrate until Loop

$a = 10;

# until loop
until ($a < 1)
{
print "$a ";
$a = $a - 1;
}
Output:

10 9 8 7 6 5 4 3 2 1

Nested Loops

A nested loop is a loop inside a loop. Nested loops are also supported by Perl Programming. And all
above-discussed loops can be nested.
Syntax for different nested loops in Perl:

Nested for loop


for (init statement; condition; increment/decrement )
{
for (init statement; condition; increment/decrement )
{
# Code to be Executed
}
}

Nested foreach loop

foreach variable_1 (@array_1) {

foreach variable_2 (@array_2)


{

# Code to be Executed
}
}

Nested while loop

while (condition)
{
while (condition)
{
# Code to be Executed
}
}

Nested do..while loop

do{
do{

# Code to be Executed

}while(condition);

}while(condition);
Nested until loop

until (condition) {
until (condition)
{
# Code to be Executed
}
}

Example :

# Perl program to illustrate


# nested while Loop

$a = 5;
$b = 0;

# outer while loop


while ($a < 7)
{
$b = 0;

# inner while loop


while ( $b <7 )
{
print "value of a = $a, b = $b\n";
$b = $b + 1;
}

$a = $a + 1;
print "Value of a = $a\n\n";
}
Output:

value of a = 5, b = 0
value of a = 5, b = 1
value of a = 5, b = 2
value of a = 5, b = 3
value of a = 5, b = 4
value of a = 5, b = 5
value of a = 5, b = 6
Value of a = 6
value of a = 6, b = 0
value of a = 6, b = 1
value of a = 6, b = 2
value of a = 6, b = 3
value of a = 6, b = 4
value of a = 6, b = 5
value of a = 6, b = 6
Value of a = 7

A Perl function or subroutine is a group of statements that together perform a specific task. In every
programming language user want to reuse the code. So the user puts the section of code in function or
subroutine so that there will be no need to write code again and again. In this article we will discuss the
following concepts:

Passing Hashes to Subroutines


Passing Lists to Subroutines
Returning Value from a Subroutine
Local and Global Variables in Subroutines
Different number of parameters in subroutine call

Passing Hashes to Subroutines: A hash can also be passed to subroutines which automatically converted
into its key-value pair.
Example:

# Perl program to demonstrate the


# passing of hash to subroutines

#!/usr/bin/perl

# Subroutine definition
sub Display_hash {

# hash variable to store


# the passed arguments
my (%hash_var) = @_;

# to display the passed list elements


foreach my $key (keys %hash_var )
{
my $val = $hash_var{$key};
print "$key : $val\n";
}
}
# defining hash
%hash_para = ('Subject' => 'Perl', 'Marks' => 97);

# calling Subroutine with hash parameter


Display_hash(%hash_para);

Output:
Marks : 97
Subject : Perl

Passing Lists to Subroutines: As we know that @_ is a special array variable inside a function or
subroutine, so it is used to pass the lists to the subroutine. Perl has a different way to accept and parse
arrays and lists that make it difficult to extract the discrete element from @_. In order to pass a list along
with other scalar arguments, it is necessary to make the list as the last argument.
Example:
# Perl program to demonstrate the
# passing of lists to subroutines

#!/usr/bin/perl

# Subroutine definition
sub Display_List {

# array variable to store


# the passed arguments
my @para_list = @_;

# to print the passed list elements


print "Given list is @para_list\n";
}

# passing scalar argument


$sc = 100;

# passing list
@li = (10, 20, 30, 40);

# Calling Subroutine with scalar


# and list parameter
Display_List($sc, @li);

Output:
Given list is 100 10 20 30 40
Returning Value from a Subroutine: A subroutine can also return a value like in other programming
languages such as C, C++, Java etc. If the user will not return a value from subroutine manually, then
the subroutine will return a value automatically. In this, the automatically returned value will be the last
calculation executed in the subroutine. The return value may be scalar, array or a hash.
Example:

# Perl Program to demonstrate the


# returning values subroutine

#!/usr/bin/perl

# subroutine definition
sub Sum {

# To get total number


# of parameters passed.
$num = scalar(@_);
$s = 0;

foreach $i (@_)
{
$s += $i;
}

# returning sum
return $s;
}

# subroutine calling and storing result


$result = Sum(30, 2, 40);

# displaying the result


print "Sum of the given numbers : $result\n";

Output:
Sum of the given numbers : 72

Local and Global Variables in Subroutines: All the variables inside a Perl program are Global variables
by default. But with the help of my keyword, you can create the local or private variables inside a block.
A private variable has a limited scope like between the block(if, while, for, foreach etc.) and methods
etc. Outside block or method, these variables can’t be used.
Example:
# Perl program to demonstrate the Local
# and Global variables in subroutine

#!/usr/bin/perl

# A Global variable
$str = "GeeksforGeeks";

# subroutine definition
sub Geeks {

# Private variable by using my


# keyword for Geeks function
my $str;

$str = "GFG";
print "Inside the Subroutine: $str\n";
}

# Calling Subroutine
Geeks();

print "Outside the Subroutine: $str\n";

Output:
Inside the Subroutine: GFG
Outside the Subroutine: GeeksforGeeks

Different number of parameters in subroutine call: Perl does not provide us any built-in facilities to
declare the parameters of a subroutine, which makes it very easy to pass any number of parameters to a
function.
Example:

# Perl program to demonstrate the variable


# number of parameters to the subroutine

#!/usr/bin/perl

use strict;
use warnings;

# defining subroutine
sub Multiplication {
# private variable containing
# default value as 1
my $mul = 1;

foreach my $val (@_)


{
$mul *= $val;
}

return $mul;
}

# Calling subroutine with 4 parameters


print Multiplication(8, 2, 3, 4);

print "\n";

# Calling subroutine again but


# with 3 parameters
print Multiplication(3, 5, 4);

Output:
192
60

Note: Generally, passing more than one array or hash as parameters to subroutines causes them to lose
their separate identities. Similarly, returning more than one array or hash from subroutine also causes to
lose their separate identities. We can solve these problems by using references.

Prerequisite: Perl | Subroutines or Functions A Perl function or subroutine is a group of statements that
together perform a specific task. In every programming language, the user wants to reuse the code. So
the user puts the section of code in a function or subroutine so that there will be no need to rewrite the
same code again and again. For this reason, function or subroutine is used in every programming
language. These functions or subroutines can take various different types of data structures as
parameters. Some of these are explained below:

Passing Lists or Arrays to a Subroutine


Passing References to a Subroutine
Passing Hashes to a Subroutine
Passing File Handles to a Subroutine
Passing Lists or Arrays to a Subroutine: An array or list can be passed to the subroutine as a parameter
and an array variable @_ is used to accept the list value inside of the subroutine or function. Example
1: Here a single list is passed to the subroutine and their elements are displayed.
#!/usr/bin/perl

# Defining Subroutine
sub Display_List
{

# array variable to store


# the passed arguments
my @List1 = @_;

# Printing the passed list elements


print "Given list is @List1\n";
}

# Driver Code

# passing list
@list = (1, 2, 3, 4);

# Calling Subroutine with


# list parameter
Display_List(@list);
Output:
Given list is 1 2 3 4
Example 2: Here two lists are passed to the subroutine and their elements are displayed.

#!/usr/bin/perl

# Defining Subroutine
sub Display_List
{

# array variable to store


# the passed arguments
my @List3 = @_;

# Printing the passed lists' elements


print "Given lists' elements are @List3\n";
}

# Driver Code

# passing lists
@List1 = (1, 2, 3, 4);
@List2 = (10, 20, 30, 40);
# Calling Subroutine with
# list parameters
Display_List(@List1, @List2);
Output:
Given lists' elements are 1 2 3 4 10 20 30 40

Passing References to a Subroutine: References can also be passed to the subroutines as a parameter.
Here the reference of the given array is passed to the subroutine and the maximum value of array
elements is returned. Example:

#!/usr/bin/perl
use warnings;
use strict;

# Creating an array of some elements


my @Array = (10, 20, 30, 40, 50);

# Making reference of the above array


# and calling the subroutine with
# reference of the array as the parameter
my $m = max(\@Array);

# Defining subroutine
sub max
{

# Getting the array elements


my $Array_ref = $_[0];
my $k = $Array_ref->[0];

# Iterating over each element of the


# array and finding maximum value
for(@$Array_ref)
{
$k = $_ if($k < $_);
}
print "The max of @Array is $k\n";
}
Output:
The max of 10 20 30 40 50 is 50
Passing Hash to a Subroutine: A Hash can also be passed to a subroutine as a parameter and its key-
value pair is displayed. Example:
#!/usr/bin/perl

# Subroutine definition
sub Display_Hash
{

# Hash variable to store

# the passed arguments


my (%Hash_var) = @_;

# Displaying the passed list elements


foreach my $key (keys %Hash_var)
{
my $value = $Hash_var{$key};
print "$key : $value\n";
}
}

# Driver Code

# defining hash
%Hash = ('Company' => 'GeeksforGeeks',
'Location' => 'Noida');

# calling Subroutine with hash parameter


Display_Hash(%Hash);
Output:
Company : GeeksforGeeks
Location : Noida
Passing File Handles to a Subroutine: For creating a file or accessing the file contents one needs a
filehandle which is nothing but a structure which is used along with the operators to access the file in a
certain mode like reading, writing, appending, etc. FileHandles can also be passed to a subroutine as a
parameter to perform various operations on Files. In the below example, a filehandle is passed to a
subroutine:- Example:

#!/usr/bin/perl

sub printer
{
my $file = shift;

while (<$file>) {
print;
}
}

open(fh, "Hello.txt") or die "File '$filename' can't be opened";

printer *fh;
Output:

A Perl package is a collection of code which resides in its own namespace. Perl module is a package
defined in a file having the same name as that of the package and having extension .pm. Two different
modules may contain a variable or a function of the same name. Any variable which is not contained in
any package belongs to the main package. Therefore, all the variables being used, belong to the ‘main’
package. With the declaration of additional packages, it is maintained that variables in different packages
do not interfere with each other.
Declaration of a Perl Module
Name of the module must be the same as that of the package name and has a .pm extension. Example :
Calculator.pm

package Calculator;

# Defining sub-routine for Addition


sub addition
{
# Initializing Variables a & b
$a = $_[0];
$b = $_[1];

# Performing the operation


$a = $a + $b;

# Function to print the Sum


print "\n***Addition is $a";
}

# Defining sub-routine for Subtraction


sub subtraction
{
# Initializing Variables a & b
$a = $_[0];
$b = $_[1];

# Performing the operation


$a = $a - $b;
# Function to print the difference
print "\n***Subtraction is $a";
}
1;
Here, the name of the file is “Calculator.pm” stored in the directory Calculator. Notice that 1; is written
at the end of the code to return a true value to the interpreter. Perl accepts anything which is true instead
of 1.
Using a Perl Module
To use this calculator module, we use require or use functions. To access a function or a variable from
a module, :: is used. Here is an example demonstrating the same: Examples: Test.pl

#!/usr/bin/perl

# Using the Package 'Calculator'


use Calculator;

print "Enter two numbers to add";

# Defining values to the variables


$a = 10;
$b = 20;

# Subroutine call
Calculator::addition($a, $b);

print "\nEnter two numbers to subtract";

# Defining values to the variables


$a = 30;
$b = 10;

# Subroutine call
Calculator::subtraction($a, $b);
Output:
Accessing a Package from a different directory
If a file accessing the package lies outside the directory then we use ‘::’ to tell the path of the module.
For example, file using calculator module is outside the calculator package so we write Calculator ::
Calculator to load the module, where the value on left of ‘::’ represents package name and value on the
right of ‘::’ represents Perl module name. Let’s see an example to understand this: Examples:
Test_out_package.pl outside Calculator directory

#!/usr/bin/perl

use GFG::Calculator; # Directory_name::module_name

print "Enter two numbers to add";


# Defining values to the variables
$a = 10;
$b = 20;

# Subroutine call
Calculator::addition($a, $b);

print "\nEnter two numbers to subtract";

# Defining values to the variables


$a = 30;
$b = 10;

# Subroutine call
Calculator::subtraction($a, $b);
Output:
Using Variables from modules
Variables from different packages can be used by declaring them before using. Following example
demonstrates this Examples: Message.pm

#!/usr/bin/perl

package Message;

# Variable Creation
$username;

# Defining subroutine
sub Hello
{
print "Hello $username\n";
}
1;
Perl file to access the module is as below Examples: variable.pl

#!/usr/bin/perl

# Using Message.pm package


use Message;

# Defining value to variable


$Message::username = "ABC";

# Subroutine call
Message::Hello();
Output:
Begin and End Block
BEGIN and END block is used when we want to run some part of the code at the beginning and some
part of the code at an end. The codes written within BEGIN{…} are executed at the beginning of the
script while the codes written within END{…} are executed at the end of the script. The program below
demonstrates this: Examples: begineg.pl

#!/usr/bin/perl

# Predefined BEGIN block


BEGIN
{
print "In the begin block\n";
}

# Predefined END block


END
{
print "In the end block\n";
}

print "Hello Perl;\n";


Output:
In the begin block
Hello Perl;
In the end block

Perl File Handling


File handling is the most important part in any programming language. A filehandle is an internal Perl
structure that associates with a file name.

Perl File handling is important as it is helpful in accessing file such as text files, log files or configuration
files.

Perl filehandles are capable of creating, reading, opening and closing a file.

Perl Create File


We are creating a file, file1.txt with the help of open() function.

The $fh (file handle) is a scalar variable and we can define it inside or before the open() function. Here
we have define it inside the function. The '>' sign means we are opening this file for writing.
The $filename denotes the path or file location.

Once file is open, use $fh in print statement. The print() function will print the above text in the file.

Now we are closing $fh. Well, closing the file is not required in perl. Your file will be automatically
closed when variable goes out of scope.
1. my $filename = 'file1.txt';
2. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
3. print $fh "Hello!! We have created this file as an example\n";
4. close $fh;
5. print "done\n";

Output:

done.

Perl Open File


We can open a file in following ways:

(<) Syntax\

The < sign is used to open an already existing file. It opens the file in read mode.

1. open FILE, "<", "fileName.txt" or die $!

(>) Syntax

The > sign is used to open and create the file if it doesn't exists. It opens the file in write mode.

1. open FILE, ">", "fileName.txt" or die $!


The "<" sign will empty the file before opening it. It will clear all your data of that file. To prevent this
use (+) sign before ">" or "<" characters.

(+>+<) Syntax

1. open FILE, "+<", "fileName.txt" or die $!


2. open FILE, "+>", "fileName.txt" or die $!

(>>) Syntax

The >> sign is used to read and append the file content. It places the file pointer at the end of the file
where you can append the information. Here also, to read from this file, you need to put (+) sign
before ">>" sign.

1. open FILE, "<", "fileName.txt" or die $!

Perl Read File

You can read a complete file at once or you can read it one line at a time. We'll show an example for
both. Opening a file to read is similar to open a file to write. With only one difference that ">" is used
to write and "<" is used to read the file.
We have created a file file1.txt with the following content:

1. This is the First Line.


2. This is the Second Line.
3. This is the Third Line.
4. This is the Fourth Line.

To read Single line at a time

First line of file1.txt will be displayed. Content of $row will be printed with "done" to make it clear that
we reached at the end of our program.

1. use strict;
2. use warnings;
3. my $filename = 'file1.txt';
4. open(my $fh, '<:encoding(UTF-8)', $filename)
5. or die "Could not open file '$filename' $!";
6. my $row = <$fh>;
7. print "$row\n";
8. print "done\n";

Output:

This is the First Line.


Done.

To read Multi lines at a time

Now we know to read single line from a file. To read multiple lines put $row in a while loop.

Every time, when while loop will reach its condition, it will execute my $row = <$fh>. It will read the
next line from the file. At the last line, $fh will return undef which is false and loop will terminate.

1. use strict;
2. use warnings;
3. my $filename = 'file1.txt';
4. open(my $fh, '<:encoding(UTF-8)', $filename)
5. or die "Could not open file '$filename' $!";
6. while (my $row = <$fh>) {
7. chomp $row;
8. print "$row\n";
9. }
10. print "done\n";

Output:

This is the First Line.


This is the Second Line.
This is the Third Line.
This is the Fourth Line.
Done.

Perl Write File

Through file writing, we'll append lines in the file1.txt. As already stated, new lines will be added at the
last of the file.

1. open (FILE, ">> file1.txt") || die "problem opening $file1.txt\n";


2. print FILE "This line is added in the file1.txt\n";
3. # FILE array of lines is written here
4. print FILE @lines1;
5. # Another FILE array of lines is written here
6. print FILE "A complete new file is created";
7. # write a second array of lines to the file
8. print FILE @lines2;

Output:

This line is added in the file1.txt


A complete new file is created

Perl Close File

Perl close file is used to close a file handle using close() function. File closing is not compulsory in perl.
Perl automatically closes file once the variable is out of scope.

1. open FILE1, "file1.txt" or die $!;


2. ...
3. close FILE1;

ADVERTISEMENT

Perl File Handle Operator, <FILEHANDL>


File handle operator is the main method to read information from a file. It is used to get input from user.
In scalar context, it returns a single line from the filehandle and in line context, it returns a list of lines
from the filehandle.

1. print "What is your age?\n";


2. $age = <STDIN>;
3. if($age >= 18)
4. {
5. print "You are eligible to vote.\n";
6. } else {
7. print "You are not eligible to vote.\n";
8. }

Output:

1. What is your age?


18
You are eligible to vote
2. What is your age?
16
You are not eligible to vote.

Perl File Handle print() function

The print() function prints back the information given through filehandle operators.

1. print "Welcome to my site\n";

Output:

Welcome to my site

Perl Copying a File

We can copy content of one file into another file as it is. First open file1 then open file2. Copy the
content of file 1 to file2 by reading its line through a while loop.

1. # Opening file1 to read


2. open(File1Data, "<file1.txt");
3. # Opening new file to copy content of file1
4. open(File2Data, ">file2.txt");
5. # Copying data from file1 to file2.
6. while(<File1Data>)
7. {
8. print File2Data $_;
9. }
10. close( File1Data );
11. close( File2Data );

Output:

done

A new file file2.pl will be created in the location where file1.pl exists.

Perl File Test Operators

There are different test operators to check different information about a file. Some of the test operators
are as follows:

Test operators Description

-A Return total access time of file since the program started.

-b Check whether file is block device or not.

-B Check whether it is a binary file.

-c Check whether file is character device.

-C Return inode change time of file since the program started.

-d Check whether file is a directory.

-e Check whether file exists or not.

-f Check type of file whether it is regular, symbolic link or other type


of file.

-g Check whether file have setgid bit set.

-k Check whether file have sticky bit set.

-l Check if file is a symbolic link. In dos, it always return false.

-M Return file modification time in days since the program started.


-o Check if file is owned by an effective uid, in dos, it always return true.

-O Check if file is owned by a real uid, in dos, it always return true.

-p Check whether file is a named pipe.

-r Check whether file is readable or not.

-R Check whether file is readable by real uid or not. In dos, it is same


as -r.

-s Return the file size in bytes.

-S Check if file is a socket.

-t Check if file is opened to a tty (terminal)

-T Check if file is a text file.

-u Check if file has setuid bit set.

-w Check if file is writable or not.

-W Check if file is writable by real uid/gid.

-x Check if file can be executed or not.

-X Check if file can be executed by real uid/gid.

-z Check if file size is zero or not.

You might also like