Unit-5 Oss
Unit-5 Oss
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.
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 {
} else
The ternary operator ? which is also a conditional operator is used similarly as an alternative to the above
statement.
(condition) ? statement1: statement2
WHILE – DO WHILE
The below statement will check for the condition until it is true before executing the block:
$cnt = 5;
while ($cnt > 0) {
$cnt--;
The below statement will check for the condition until it is false before executing the block:
$cnt = 1;
$nt++;
The below block uses do while where the condition is tested or checked after executing the block:
$cnt = 5;
do {
$cnt--;
The following for loop statements are similar to C language statements where it initializes, checks the
conditions, and iterates:
If you’re working on arrays, for each can be used to perform looping construct:
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:
For example:
sub Hi_Intro {
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
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:
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.
# 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
Output:
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);
$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
}
$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:
# Code to be Executed
}
}
while (condition)
{
while (condition)
{
# Code to be Executed
}
}
do{
do{
# Code to be Executed
}while(condition);
}while(condition);
Nested until loop
until (condition) {
until (condition)
{
# Code to be Executed
}
}
Example :
$a = 5;
$b = 0;
$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: A hash can also be passed to subroutines which automatically converted
into its key-value pair.
Example:
#!/usr/bin/perl
# Subroutine definition
sub Display_hash {
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 {
# passing list
@li = (10, 20, 30, 40);
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:
#!/usr/bin/perl
# subroutine definition
sub Sum {
foreach $i (@_)
{
$s += $i;
}
# returning sum
return $s;
}
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 {
$str = "GFG";
print "Inside the Subroutine: $str\n";
}
# Calling Subroutine
Geeks();
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:
#!/usr/bin/perl
use strict;
use warnings;
# defining subroutine
sub Multiplication {
# private variable containing
# default value as 1
my $mul = 1;
return $mul;
}
print "\n";
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:
# Defining Subroutine
sub Display_List
{
# Driver Code
# passing list
@list = (1, 2, 3, 4);
#!/usr/bin/perl
# Defining Subroutine
sub Display_List
{
# 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;
# Defining subroutine
sub max
{
# Subroutine definition
sub Display_Hash
{
# Driver Code
# defining hash
%Hash = ('Company' => 'GeeksforGeeks',
'Location' => 'Noida');
#!/usr/bin/perl
sub printer
{
my $file = shift;
while (<$file>) {
print;
}
}
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;
#!/usr/bin/perl
# Subroutine call
Calculator::addition($a, $b);
# 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
# Subroutine call
Calculator::addition($a, $b);
# 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
# 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
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.
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.
(<) Syntax\
The < sign is used to open an already existing file. It opens the file in read mode.
(>) Syntax
The > sign is used to open and create the file if it doesn't exists. It opens the file in write mode.
(+>+<) Syntax
(>>) 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.
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:
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:
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:
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.
Output:
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.
ADVERTISEMENT
Output:
The print() function prints back the information given through filehandle operators.
Output:
Welcome to my site
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.
Output:
done
A new file file2.pl will be created in the location where file1.pl exists.
There are different test operators to check different information about a file. Some of the test operators
are as follows: