Professional Documents
Culture Documents
PERL On Unix/Linux: Practical Extraction and Reporting Language
PERL On Unix/Linux: Practical Extraction and Reporting Language
Contents
Introduction
Scalar Variables and Lists
Arrays and Hashes
Operators and Precedence
Conditional statements and Loops
Regular Expressions
Subroutines
File and Directory Handling
History
Developed by Larry Wall in 1987
Derives from the ubiquitous C programming language
and to a lesser extent from sed, awk, the Unix Shell.
PERL was originally designed under Unix, but now also
runs under all OS(Including Windows).
Introduction
What is PERL?
Interpreted language that is optimized for string
manipulation, I/O and system tasks.
Why PERL?
Speed of development Don't have to compile create object
file and then execute.
Power of flexibility of a high programming language.
Easy to use, freely available and portable.
Makes easy jobs easy, without making hard jobs impossible.
Man Pages
Man command used to read the documentation.
Command
Description
perl
perldelta
perlfaq
perltoc
perlsyn
Perl Syntax
perlop
perlre
perlfunc
Built in functions
perlsub
Subroutines
perlvar
Predefined Variables
Basic Syntax
# is used for commenting the line.
Simple Programs
Example
#! /usr/local/bin/perl
# Directs to perl interpreter on the system.
print Welcome to perl;
# Prints a message on the output.
This Program displays:
Welcome to perl
Example:
#! /usr/local/bin/perl c
Print welcome to perl;
This Program displays:
Syntax ok
Basic Options
-c
-v
-w
: Prints warnings
Standard Files
STDIN
STDOUT
STDERR
Variables
Variables are used to refer data which is held as value.
Perl defines
hashes.
Scalars :
Holds a single value it may be a string ,number or reference.
Begin with $, followed by a letter then by letters, digits or
underscores.
Example : $var =1
$var = Hello_world
$var=2.65
$3var = 123
# integer
# string
# Decimal number
#Error, Shouldnt start with
#number
Variable Interpolation
Interpolation takes place only in double quotation marks.
Example
#! /usr/local/bin/perl w
$x = 12 ;
print Value of x is $x ;
This Program displays:
Value of x is $x
Example
#! /usr/local/bin/perl w
$x = 12 ;
print Value of x is $x ;
This Program displays:
Value of x is 12
Integers
Integers are usually expressed as decimal(10) but can be
specified in several different formats.
234
0765
0b1101
0xcae
decimal integer
octal integer
binary integer
hexadecimal integer
Integers (Contd..)
Example
#! /usr/local/bin/perl w
$bin = 0b1010;
$hex = sprint f %x, $bin;
$oct = sprint f %o ,45;
print binary =$bin \n hexa =$hex \n octal =$oct;
This Program displays:
binary= 1010
hexa = a
octal = 55
Integers (Contd..)
Example
#! /usr/local/bin/perl w
$x = 98 ;
print f ( Value in decimal =%d\n, $x ) ;
print f ( Value in octal=%o\n, $x ) ;
print f ( Value in binary =%b\n, $x ) ;
print f ( Value in hexadecimal=%x\n, $x ) ;
This Program displays:
Value in decimal
=98
Value in octal
=142
Value in binary
=1100010
Value in hexadecimal =62
Escaped Sequences
Character strings that are enclosed in double quotes accept
escape sequences for special characters.
The escape sequences consist of a backslash (\) followed by
one or more characters
Escape Sequence
Description
\b
Backspace
\e
escape
\f
Form feed
\l
\L
\u
\U
\r
Carriage Return
\v
Vertical Tab
Built-in functions
Function
Description
chomp( )
Chop( )
Chr ()
Ord()
Lists
List is a group of scalar used to initialize array or hash.
The elements of a list can be numbers, strings or any other
types of scalar data.
Each element of the Perl lists can be accessed by a numerical
index.
The elements of a list are enclosed in a pair of round
parenthesis and are generally separated by commas.
Lists (Contd..)
Example :
$var = welcome
# normal variable
#first list
Lists (Contd..)
Flexible way of defining list is qw (quote word) operator
which helps avoiding to too many quotation marks, but be
cautious if white spaces are there.
Example
#! /usr/local/bin/perl w
print (sachin ,dravid, ganguly, kumble , \n);
print qw(sachin dravid ganguly kumble);
print \n;
print (sachin, dravid , ganguly, anil kumble ,\n);
print( k, a, qw(c p e r ),t , e, c , h );
print \n;
print (sachin ,dravid, ganguly, kumble )[1];
This Program displays:
Sachindravidgangulykumble
Sachindravidgangulykumble
Sachindravidgangulyanil kumble
kacpertech
dravid
Lists (Contd..)
Difference between list and array(or hash) is, array is a
variable that can be initialized with a list.
Range Operator
Defined by symbol ..
Used to create a list from a range of letters or numbers.
Example :
Print (2 .. 4 ** a .. d )
This program displays:
234**abcd
Lists (Contd..)
List functions
A list is joined into a string using join function.
Example:
Print join( ,(perl ,is ,a, scripting ,language));
This program displays:
Perl is a scripting language
Lists (Contd..)
List functions (Cont..)
map evaluates expression or block for each element of list.
Example:
Print join(, ,(map lc, A, B, C));
This program displays
a, b, c
Arrays
An one dimensional ordered list of scalar variables.
Array provides dynamic storage for a list ,and so can be
shrunk ,grown ,and manipulated by altering values.
Represented using @(at) symbol.
Array without a name is called a list.
Elements of an array are accessed using the index number
(first element has index zero, next has one, and so on)
Each element in an array is a scalar.
$#array holds the last index value in the array.
Arrays (Contd..)
( ) represents the empty list.
Example:
@arr= (perl ,2 , 5.143 );
print @ arr;
This program displays:
perl 2 5.143
Example:
@num = (1,2,3,4,5) ;
print \@num has ($#num + 1) elements;
This program displays
@num has 5 elements
Arrays (Contd..)
Example
#! /usr/local/bin/perl w
@a =(a .. z) ;
@len1 =@a;
@len2 =scalar (@arr);
Print length of a =$len1 \n ;
Print length of a =$len2;
Array Methods
push
Push function adds a value or values to the end of an array.
Example:
@num = (1,2,3,4,5) ;
Push (@num , 6) ;
Print @num ;
This program displays:
123456
pop
Example:
@num = (1,2,3,4,5) ;
Pop (@num) ;
Print @num ;
This program displays:
1234
kumble ganguly
dravid
ratan-CEO
spilt
Split() function is the opposite of join function.
Syntax :LIST = split(/PATTERN/, EXPR, LIMIT)
Hashes
An associative array ideal for handling attribute/value pair.
Lists and arrays are ordered and accessed by index ,hashes
are ordered and accessed by specified key.
Represented using % symbol.
First element in each row is called a Key and the second
element is a Value associated with that key.
Example : %coins = (quarter,25, dime,5); or
%coins = ( quarter => 25 , dime => 5);
Key
Value
Hashes (Contd..)
Hah values can be any scalar ,just like an array ,but hash
keys can only be strings.
Example: Printing the hash.
#!usr/local/bin/perl
%hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4);
print %hash1;
#we cant use print %hash1;
print @{[hash1]} \n;
@temp = %hash1;
Print @temp;
This program displays:
three3one1two2four4
three 3 one 1 two 2 four 4
three 3 one 1 two 2 four 4
Hashes (Contd..)
Hash can have only scalars as values.
{ } are used to access individual elements of the hash.
Example:
#!usr/local/bin/perl
%hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4);
$ele = $hash1(three);
#single key, use scalar
@mul_ele = @hash1(four ,one);
#multiple key ,use array
print single element =$ele ;
print multiple elements =@mul_ele;
This program displays:
single element =3
multiple elements = 4 1
Hashes (Contd..)
keys function can be used to find the no. of keys and list of
entries in a hash.
values function can be used to find the no. of values list of
values in a hash.
Example:
#!usr/local/bin/perl
%hash1 = ( one => 1 ,two => 2 ,three =>3 ,four =>4);
$ele = $hash1(three);
#single key, use scalar
@mul_ele = @hash1(four ,one);
#multiple key ,use array
print single element =$ele ;
print multiple elements =@mul_ele;
This program displays:
single element =3
multiple elements = 4 1
Manipulating Hashes
To add or change the value key we can do like this
$hash1{ three } = PERL .
It will overwrite the previous value if already existing. Otherwise it is
added as a new key.
undef function is used to remove the value of the key, but key will
still exists.
Example: undef $hash1{ two} ;
delete function is used to remove the value as well as key from the
hash.
Example :delete $hash1 {four };
Hash Sorting
Hashes are not ordered and we must not rely on the order in
which we added the hash items Perl uses internally its own
way to store the items.
We can sort hashes either by
function.
19
rohit
45
sachin
10
10
dravid
19
rohit
45
Operators
Operators can be broadly divided into 4 types.
Unary operator which takes one operand.
Example: not operator i.e. !
Binary operator which take two operands
Example: addition operator i.e. +
Ternary operator which take three operands.
Example: conditional operator i.e. ?:
List operator which take list operands
Example: print operator
Arithmetic Operators
Operator
Description
++
--
Decrements by one
**
Shift Operators
shift operators manipulate integer values as binary numbers,
shifting their bits one to the left and one to the right
respectively.
Operator
Description
<<
Left Shift
Print 2 >>3 ; left shift by three positions, prints 8
>>
Right Shift
Print 42 >>2;
Repetition Operator.
Ex: print hi x 3;
Output : hihihi
Ex2: @array = (1, 2, 3) x 3; #array contains(1,2,3,1,2,3,1,2,3)
Ex3 :@arr =(2)x80
#80 element array of value 2
Logical Operators
Logical operators represented by either symbols or names.
These two sets are identical in operation, but have different
precedence.
Operator
Description
&& or AND
|| or OR
XOR
! or NOT
Bitwise Operators
Bitwise operators treat their operands as binary values and
perform a logical operation between the corresponding bits of
each value.
Operator
Description
&
Bitwise AND
Bitwise OR
Bitwise XOR
Bitwise NOT
Comparison Operators
The comparison operators are binary, returning a value
based on a comparison of the expression
Operator
Description
<
Lessthan
>
Greaterthan
==
Equality
<=
Lessthan or equal
>=
Greaterthan or equal
<= >
!=
Inequality operator
Description
eq
le
ge
gt
gt
cmp
ne
.(dot)
Binding operator
The binding operator ,=~ ,binds a scalar expression into a
pattern match.
String operations like s/// ,m//,tr/// work with $_ by default.
By using these operators you can work on scalar variable
other than $_ .
The value returned from =~ is the return value of the regular
expression function, returns undef if match failed.
The !~ operator performs a logical negation of the returned
value for conditional expressions, that is 1 for failure and
'' for success in both scalar and list contexts.
Conditional Statements
if Statement
if keyword to execute a statement block based on the evaluation of an
expression or to choose between executing one of two statement
blocks based on the evaluation of an expression
Example :$firstVar = 2;
if ($var == 1) {
Jump Keywords
The last Keyword
The last keyword is used to exit from a statement block.
Example :- last
@array = ("A".."Z");
for ($index = 0; $index < @array; $index++) {
if ($array[$index] eq "T") {
{
last
print("$index\n");
This program displays:
19
} $array[$index] = "*";
print("@array\n");
Regular Expressions
Regular expression(regexps) is simply a string that describes
the pattern (example for pattern ,to find files in a directory
which ends with .sv i.e. ls *.sv )
Used for finding and extracting patterns within the text.
The role of regexp engine is to take a search pattern and
apply it to the supplied text.
The following operators use regular expressions.
Matching Operator (m//)
Substitution Operator(s///)
Transliteration(Translation) Operator(tr///)
ganguly-bharat kumble-ind
ganguly-india
kumble-ind
Description
g(m//g or s///g)
gc
Description
c (tr///c)
Example :
#!usr/local/bin/perl
$text = winners see gain, losers see pain;
$count = ($test =~tr/e/E/);
print $text;
Print \n no..of replacements =$count;
This program displays:
winnErs sEE gain,losErs sEE pain
no..of replacements =6
braces
and
Characters
Character Classes
Alternative Match Patterns
Quantifiers
Assertions
Characters
In regular expression any single character matches itself,
unless it is a metacharacters with special meaning.
Beside normal characters, Perl defines special characters
that you can use in regular expression.
These character must start with backslash.(Otherwise Perl
treats it as a normal character).
Character
. (period)
Description
Used to match any single character except newline
character
Ex :$var1 = ~ /r.n/ # will match run , ran, ron
Characters (Contd..)
character
Description
\d
It is equivalent to [0 - 9]
Matches any digit.
Ex1 : $var =~ /\d/ # Will match any digit.
\D
It is equivalent to [^0 - 9]
Matches any non-digit.
Ex1 : $var =~ /\D/ # Will match any non-digit.
Characters (Contd..)
character
Description
\w
It is equivalent to [0-9a-zA-Z_]
Matches a word character allowable in Perl variable
name.
i.e. Match any 'word' or alphanumeric character, which is
the set of all upper and lower case letters, the numbers
0..9 and the underscore character _
Ex :if ( $var =~ /\w/)
\W
It is equivalent to [^0-9a-zA-Z_]
Matches any non-word characters. Inverse of \w
Ex :if ($var =~ /\W)
Characters (Contd..)
character
Description
\s
It is equivalent to [ \t\n\r]
Matches any white space character.
i.e. a space ,a tab ,a newline ,a return
Ex :if ($var =~ /\s/)
\S
It is equivalent to [^ \t\n\r]
Matches any non-white space character.
Ex :if ($var =~ /\S/)
Characters (Contd..)
character
Description
\Q
\E
Characters (Contd..)
character
Description
\U
\L
Characters (Contd..)
character
Description
\u
\l
Character Classes
A character class allows a set of possible characters, rather
than just a single character, to match at a particular point in
a regular expression.
Character classes are denoted by brackets [...], with the set of
characters to be possibly matched inside.
Matches one occurrence of any character inside the bracket
Ex 1: $var =~ /w[aoi]nder/
Grouping Alternatives
Grouping[ ( ) ] allows parts of a regular expression to be
treated as a single unit.
Parts of a regular expression are grouped by enclosing them
in parentheses.
Used to group similar terms by their common characters and
only specified the differences.
Example : $var2 =~ /(while |for)loop/
# will match either while loop or for loop
$3 = ing
Quantifiers
Quantifiers says how many times something may match,
instead of the default of matching just once.
You can use quantifier to specify that a pattern must match a
specific number of times.
Quantifiers in a regular expression are like loops in a
program.
Quantifiers (Contd..)
character
Description
Quantifiers (Contd..)
character
Description
{}
Quantifiers (Contd..)
Quantifiers are greedy by default, which means they
will try to match as much as they can.
Ex :$str =they are players, arent they ?
$str =~s/.*are/were/;
print $str;
It will print :werent they ?
Perl will use the *. Preceding are to match all the
characters upto the last are in the str.
*?
+?
??
{n}?
{n,}?
{m,n}
Assertions
Assertions (also called anchors) used to match conditions
within a string, not actual data.
Assertions are zero width because they do not consume any
characters when they match.
Anchor
Description
^(caret)
Assertions (Contd..)
character
Description
\Z
\z
Assertions (Contd..)
Character
Description
\A
\G
Assertions (Contd..)
Word Boundaries
\b matches on a word boundary.
This occurs whenever a word character is adjacent to a nonword character .
It is equivalent to \w\W|\W\w .
Within character classes \b represents backspace rather
than a word boundary, just as it normally does in any doublequoted string.
Assertions (Contd..)
Word Boundaries
\B matches on a non-word boundary .
This occurs whenever two word characters or two non-word
characters fall adjacent to each other.
It is equivalent to \w\w|\W\W.
Assertions (Contd..)
Word Boundaries
Example :
#!usr/local/bin/perl
$text = "one, ****, three, four";
$text1 = "one,****,three, four";
foreach ($text =~ /\b\w+\b/g){
print $_, "\t";
}
print "\n using \\B\n";
foreach ($text1 =~ /\B\w+\B/g){
print $_, "\t";
}
This program displays:
one three four
using \B
n
hre ou
# * is not a word
# character
Regular Expressions-Examples(1)
character
Description
$var =~m/^\./
$var =~ /\w+/
$var
=~/start\s*end/
Regular Expressions-Examples(2)
character
Description
$var =~/o\.m/
$var =~
Will match either bluecolor or bluecolour
/blue(colo(ur|r))/
Regular Expressions-Examples(3)
character
Description
$var =~ s/^\s+//
$var =~
m/(\d+)/
Subroutines
Subroutine is a separate body of code designed to perform a
particular task. It is same as function in C language.
The Idea behind subroutines is that old programming dictum
divide and conquer.
Subroutines allow you to divide your code into manageable
parts, which makes overall programming easier to handle.
Perl allows you to create subroutines using the sub control
structure
Subroutines (Contd..)
Example :
#!usr/local/bin/perl
$v1 =36;
large_small();
#subroutine call before definition , parentheses must
sub large_small{
if($v1 >40) { print "value is bigger than 40\n"; }
else{ print "value is smaller than 40\n";
} }
$v1 =45;
large_small;
Scope of variable
Perl variables have global package scope by default.
When we change a variable value in subroutine, well be
changing it in the rest of the program by mistake as well.
We can create a variables that are entirely local to subroutine
by using keyword local or my.
They can have same name as global variable and not affect
those global variables at all.
Perl returns the last value in a sub routine so you can omit
the return keyword .
In the above example $val1+$val2 gives the same.
Recursion
Recursion happens when a subroutine calls itself, either
directly, or indirectly, via another subroutine
Example :
#!usr/local/bin/perl0
$fact = fact(6);
print "factorial of given number =$fact\n;
sub fact {
local $val =shift(@_);
if($var ==0) {
return 1; }
elsif($val==1) {
return $val ; }
else{
return $val*fact($val-1);
This program displays:
factorial of given number =720
}}
Subroutines Examples(1)
Passing Lists
Example :
#!usr/local/bin/perl
@small= qw(sachin dravid ganguly );
@big =case_convert(@small);
print "@big";
sub case_convert {
@low =@_;
@caps =map(uc ,@low);
}
This program displays:
SACHIN DRAVID GANGULY
Subroutines Examples(2)
Nested subroutines
Example :
#!usr/local/bin/perl
call();
sub call{
display();
sub display {
print "you are in inner subroutine\n" ;
print "you are in outer subroutine\n" ;
}
print "you are in main\n" ;
This program displays:
you are in inner subroutine
you are in outer subroutine
you are in main
Pass by Reference
In general passing arrays or hashes flattens their elements
into one long list, so its a problem if you want to send two or
more distinct arrays or hashes.
To preserve integrity, you can pass references to arrays or
hashes instead.
References can be created by using a backslash (\) operator
on a variable. It is similar to address-of (&)operator in C.
\$var
\@arr
\%hash
Scalar Reference
Array Reference
Hash Reference
Returning by Reference
If you return two arrays normally, their values are flattened
into one long list. If you return references to arrays, you can
deference those arrays and reach the original arrays.
Example :
#!usr/local/bin/perl
sub get_strings{
@str1 = qw(Asia Austrelia Africa);
@str2 =qw(Brown White Black);
return \@str1 ,\@str2;
}
($ref_str1 ,$ref_str2) =get_strings;
print "@$ref_str1 \n";
print "@$ref_str2 \n";
This program displays:
Asia Austrelia Africa
Brown White Black
File Handling
A filehandle is nothing more than a nickname for the files
you intend to use in your PERL scripts and programs.
Filehandles are a connection between our program and an
external data source
Filehandles in Perl are a distinct data type.
STDIN or standard input represents the default input
filehandle and usually connected to the keyboard.
STDOUT or Standard output represents the default output
filehandle
and usually connected to the console
device(screen)
Opening a file
To open a file ,use the open function.
Syntax : open FILEHANDLE ,MODE,LIST
open FILEHANDLE ,EXPR
open FILEHANDLE
The open function takes a filename and creates the handle
for it.
Symbol
Description
Read
<
Write
>
Symbol
Description
Append
>>
Readupdate
+<
Symbol
Description
Writeupdate
>+
Appendupdate
>>+
print "$_";
Directories Handling
print function writes to the file specified, or to the current
default file if no file is specified.
Ex: print ("Hello, there!\n");
print OUTFILE ("Hello, there!\n");
write function uses a print format to send formatted output
to the file that is specified or to the current default file.
Ex : write (CD_REPORT);
Ex:
Ex :chdir ("/u/public/newdir");
Ex : closedir (mydir);
ThanQ