You are on page 1of 112

Y 

Cou Co  

1. Introduction to Perl
2. Working with scalars (numbers & strings)
3. Operators and functions
4. Conditions & loops
5. Working with lists (arrays & hashes)
6. Sub-routines
7. Regular expressions
Contd«..
8. references and complex data structures
9. debugging in Perl
10. Packages & Modules
11. managing files and directories
12. working with files and I/O
13. handling databases in Perl
@  oduo oY 
þ Perl stands for ´Practical Extraction and
Report Languageµ.
þ Perl is a mixture of UNIX tools (sed, grep,
awk etc.), Shell Scripting, C and Object
Oriented features.
þ Perl was created by Larry Wall. It is now not
owned by any single Organization. Perl is
defined and maintained by a group of
volunteer programmers including Larry Wall.
Õ u ofY 

1. Perl is free
2. Perl is both a compiled and interpreted
language
3. Perl is portable
4. Perl can interface with other languages
5. Perl is easy to use and debug
èowo
Y 

Perl can be downloaded from the Internet


thru www.perl.org (or) www.perl.com

The current version of Perl is 5.10.0.

Perl is available on different platforms,


Unix,Windows, MacOS etc.


 f o
 

D vi first.pl
# program to print a message
print ´Hello, World\nµ ;

To run the program,


D perl first.pl
Ú ou Yo
 

ÿ Úny line that starts with a #, is a comment

ÿ To display messages, print command is used

ÿ Every perl statement must end with a ;


Co d o

To know the current version of Perl


D perl ²v
To check the syntax errors only but does not
run the script
D perl ²c first.pl
To turn on the warnings
D perl ²w first.pl
Y   è 

The online help is available in pod (plain old


documentation) format and can be invoked
using perldoc command.

D perldoc perlfunc
D perldoc ²f <command> (D perldoc ²f print)
D perldoc perldoc
  

Perl has two types of data:


1. scalars: data consisting of a single thing
ex: Numbers, Strings, References
2. lists: data consisting of a collection of
single things or scalars
ex: Úrrays, Hashes
—   

Ú) Numbers:
4 45e-4
3.2 0xbeef
.234567 012
5. 1_234_456
10E2
Perl does not differentiate between integers
and floats, signed or unsigned, or short and
long numbers. Perl converts between the
number types as needed in our scripts
  

A) Strings : Ú string is zero or more characters


surrounded by single quotes (¶ ¶)
or by double quotes (´ ´)
Escape sequences are used for
string formatting in print function.
\n new line
\t tab
\b backspace
  

\a bell sound
\0nn octal
\0xnn hexadecimal
\l make next letter lower case
\u make next letter upper case
\L make all the following letters lower
\U make all the following letters upper
  

Converting between numbers and strings:


Since, numbers and strings are scalar data,
they are interchangeable.

ex: 5 + ´14µ gives 19


´fooµ + 5 gives 5
´23abcµ + 7 gives 30
    
Da=5;
Da=Db=5;
Da=Db=5, Dc=7;
Ds=´abcµ;
Dt=¶xyz·;
Rules for variable names:
1.should start with a letter, underscore
2.case sensitive
3.can be up to 255 characters.
We don·t have to declare or initialize variables in the
beginning.
u

Reading scalar values from Keyboard:


Da=<STDIN>;
chomp (Da);
When we enter values from the keyboard,
¶\n·
is automatically attached to the input value.
To remove ¶\n· from the input, we use chomp
function.
uu

Displaying the values on the screen:

print (´Da\nµ);
printf (´%.2f\nµ,Db);
Dc=sprintf(´%.2fµ, Db);
°   o Õu o

Úrithmetic operators:
+ (addition),
- (subtraction),
* (multiplication),
/ (division),
% (remainder),
** (exponent)
  o

Relational Operators:
Numeric Operators:
==, !=, <, >, <=, >=
String Operators:
eq, ne, lt, gt, le, ge
Logical Operators:
and (&&), or (||), not (!)
  o

Ússignment Operators:
+=, -=,*=, /=, %=, **=
Increment and Decrement Operators:
++, --
Conditional Operator:
exp1?exp2:exp3
  o

Aitwise Operators:
<<, >>
String catenation operator:
.
String repetition operator:
x
  o

luoting the strings:


Single quotes: q/how are you/

Double quotes: qq/how are you/

General quotes: qw (abc);


^u Õu o

abs () absolute value


int () converts to integer
sin () sine value
cos () cosine value
rand () random number
exp () e to the power of
atan2 () arctangent
ord () ascii number
sqrt () square root

Õu o

chr () ascii character


index () returns the pos. of chr.
length () length
reverse () reverses a scalar
rindex () reverse index
substr () substring
lc () lowercase
conversion
uc () upper case
conversion
lcfirst () first character lower
uc first() first character upper
ð Co do  doo

if

if (condition) { statements}

if (condition)
{statements}
else
{statements}
Co do  doo

if (condition)
{
statements
}
elsif (condition)
{
statements
}
Co do  doo

unless
unless (condition)
{
statements
}
Co do  doo

while
while (condition)
{
statements
}
Co do  doo

until
until (condition)
{
statements
}
Co do  doo

do
do {statements}
while (condition);

do {statements}
until (condition);
Co do  doo

for

for (init; test; change)


{
statements
}
Co do  doo

foreach

foreach value (list)


{
statements
}
Co do  doo

Infinite loops
while ()
{
statements
}
for (;;)
{
statements
}
Co do  doo

break from a control statement


last
continue
next
Co do  doo

D_ variable
It is the default placeholder for scalar
values
example
foreach (`dir`)
{
print (´D_\nµ);
}
Co do  doo

switch
use Switch;
switch (Dvalue)
{
case 1 {print ´1µ; }
«««««««««.
else {print ´0µ ;}
}
¢ Ú   dè 

Creating Úrrays:
@nums = (1,2,3,4);
@nums = (1..4);
@strings = (¶ab· , ·cd· , ·ef·);
@strings = (¶a· ,·z·);
@strings = ();
@combine=(@nums, @strings);
Ú   dè 

Printing the Úrray:


print (´@nums\nµ);
print (´@strings\nµ);
print (´@combine\nµ);
Processing each element:
foreach Dx (@nums)
{
print (´Dx\nµ);
}
Ú   dè 

Úccessing Úrray elements:


Dnums[3];
The subscript starts from 0 to n-1
Negative Úrray Indexes:
Dnums[-1];
Negative array subscripts will count
back
from the end of the array.
Ú   dè 
Growing Úrrays:
@nums=(1..4);
Dnums[6]=7;
Result is 1,2,3,4,undefined, undefined,7
Finding the end of the Úrray:
Dend=D#nums;
Finding the length of the Úrray:
Dlen=@nums;
Ú   dè 

Sorting the Úrray:


cmp (strings), < = > (numbers)
@sorted=sort {Da cmp Db} @strings;
@sorted=sort {Da < = > Db} @nums;
Reversing the Úrray:
@new=reverse (@old);
Ú   dè 

push and pop functions allow you to


add or remove elements from the end
of the list

Dnew=push (@nums,5);
Dnew1=pop (@nums);
Ú   dè 

shift and unshift remove the elements


from the beginning of the Úrray
Dnew=shift (@nums);
Dnew1=unshift (@nums, 10);
splice removes the elements from the
Úrray
@new=splice (@nums,5,3);
Ú   dè 
slice creates pieces of the Úrray
@array=(1..10);
@slice=@array[0,1,2];
splitting the data into an Úrray
Dstrings=·12 23 43·;
@nums=split(¶ ¶,Dstrings);
joining the data from an Úrray
Dstrings=join(¶+·, @nums);
Ú   dè 

Creating a Hash:
%hash=(key=>value, key=>value, key=>value);
Ex:
%pairs=(red=>255, blue=>355, green=>455);
Úccessing Hash Elements:
Dhash {Dkey};
Ex: Dpairs {red};
Printing the Hash:
print %hash;
Ex: print %pairs;
Ú   dè 

Processing a Hash:
foreach Dkey (sort keys %hash)
{
print ´Dhash {Dkey}\nµ;
}
while ((Dkey, Dvalue) = each (%hash)
{
print ´Dhash {Dkey} \nµ;
}
Ú   dè 

Ex:
foreach Dkey (sort keys %pairs)
{
print ´Dpairs {Dkey}\nµ;
}
or
while ((Dkey, Dvalue) = each (%pairs))
{
print ´Dpairs {Dkey} \nµ;
}
Ú   dè 

Údding an element to a Hash:


Dhash {Dkey} = Dvalue;
Ex:
Dpairs {yellow}=555;
Deleting an element from a Hash:
delete (Dhash {Dkey});
Ex:
delete (Dpairs {red});
Ú   dè 

Inverting a Hash:
%new=reverse %hash;
Ex:
%newpairs = reverse %pairs;
Finding the no. of keys in a Hash:
Dn=keys %hash;
Ex:
Dn=keys %pairs;
h u ou

The terms ¶function· and ¶subroutine· are


entirely equivalent in Perl.

Perl handles three types of functions.

1. Auilt-in: These are the functions that are


defined by the standard Perl library that we
can use any where in our Perl scripts.
u ou

2. Údditional: These are the functions that are


available to us by using the additional Perl
modules or libraries, written by other Perl
programmers, that we can load in at the start
of the script.
3. Subroutines: These are user defined
functions. Ay convention, the term subroutine
is used to identify the function as user defined
one.
u ou

Defining & Calling basic subroutines:


print ´enter temperature in forenheight \nµ;
chomp (Df=<STDIN>);
&cal();
print ´The result is Dcel\nµ;
sub cal ()
{
Dcel=(Df-32)*5/9;
}
u ou

Returning values from a subroutine:


Dsum=&cal();
print ´The sum is Dsum\nµ;
sub cal ()
{
print ´enter two nos. \nµ;
chomp (Dn1=<STDIN>);
chomp (Dn2=<STDIN>);
return (Dn1 + Dn2);
}
u ou
Using local variables inside a subroutine:
We can create local variables inside a
subroutine using
¶my· modifier.
Dsum=&cal();
print ´The sum is Dsum\nµ;
sub cal ()
{
my (Dn1,Dn2);
print ´enter two nos. \nµ;
chomp (Dn1=<STDIN>);
chomp (Dn2=<STDIN>);
return (Dn1 + Dn2);
u ou
Passing Úrguments to a subroutine:
Dsum=&cal(3,5);
print ´The sum is Dsum\nµ;
sub cal ()
{ my (Dm, Dn)= @_;
my Dp=1;
while (Dn>0)
{ Dp=Dp*Dm;
Dn--;
}
return (Dp);
}
u ou
Únonymous subroutines:
These are subroutines without names
and they operate sort of like pointers
to functions in C.
4 ÚY ^

REGULÚR EXPRESSION OPERÚTORS:


m//, s///, tr///
@x = grep /x/, @words;
PÚTTERN MÚTCHING OPERÚTORS:
=~, !~
REs:
abc exact character sequence
ÚY ^

;abc abc at the beginning


abcD abc at the ending
a|b a or b
ab {2,4}c a followed by 2,3,4 b·s
followed by c
ab{2,}c a followed by at least 2 b·s
followed by c
ÚY ^

ab*c a followed by zero or more


b·s followed by c
ab+c a followed by one or more
b·s followed by c
ab?c a followed by optional b
followed by c (abc or ac)
a.c an a followed by any single
character (no newline) followed by c
ÚY ^

a\.c a.c exactly


[abc] any one of a , b or c
[Úa]bc Úbc or abc
[;abc] not containing a or b or c
6  f  
Ú reference is similar to a pointer in C.
The reference itself is a scalar, it can be
assigned to a scalar variable, printed, added
to, passed to subroutine etc. To find out
what the reference points to, we can
dereference the reference.
Creating a reference for a scalar:
Dstr=¶This is a stringµ;
Dstrref=\Dstr;
 f  

Dereferencing a scalar reference:


Doriginalstr=DDstrref;
Creating a reference for an Úrray:
@array=(1..10);
Darrayref=\@array;
Dereferencing an array reference:
@originalarray=@Darrayref;
To refer to individual elements:
Doriginalarray[0] or DDarrayref[0];
 f  

Creating a reference for a hash:


%hash=(a=>255,b=>355,c=>455);
Dhashref=\%hash;
Dereferencing a hash reference:
%originalhash=%Dhashref;
To refer to individual keys:
Doriginalhash{a} or DDhashref{a};
 f  
Creating a reference for a subroutine:
Dsubref=\&mysub;
Dereferencing a subroutine:
Dresult=&Dsubref(3,5);
Creating anonymous subroutines:
Dsubref=sub {«.};
Dereferencing a subroutine:
Dresult=&Dsubref(«);
 f  
Únonymous data: The term anonymous means
´without a nameµ. Únonymous data refers to data
(usually arrays, hashes and subroutines) that we
can only access through a reference i.e. the data
does not have an associated variable name. Using
anonymous data, we can create nested data
structures such as,
1.Úrrays of Úrrays
2. Hashes of Úrrays
3. Hashes of Hashes
 f  
Úrrays of Úrrays:
@array= ( [0,1,2],
[23,33,43],
[53,54,55]
);
Referring to individual elements:
Darray[0][1] or Darray[0]->[1]
 f  

Hashes of Úrrays:
%hash= ( b=>[0,1,2],
g=> [23,33,43],
r=>[53,54,55]
);
Referring to individual elements:
Dhash{b}[1] or Darray{b}->[1]
 f  

Hashes of Hashes:
%hash=(a=>{x=>0,y=>1,z=>2},
b=>{l=>3,m=>4,n=>5},
c=>{p=>53,q=>54,r=>55}
);
Referring to individual elements:
Dhash{a}{x} or Darray{a}->{x}
M  u


 Y 

Perl comes with a source level debugger.


The debugger can help us track down
subtle problems in our code.

We shall see,
1. how to start and run the debugger
2. step through the execution of the script
3. trace the execution of the script
4. list the source in various ways
 u


 Y 

5. print out the values of the variables


6. set the breakpoints

To invoke the debugger,


D perl ²d temp.pl
Úfter the display of some messages, we see
main::(temp.pl:1): system (´clsµ);
DA<1>
 u


 Y 

DA <1> debugger prompt with


command number
main package name
temp.pl script name
1 line no. of the script
system (´clsµ) line in the script to be run
 u


 Y 

Debugger Commands:
n runs a line of the script, silently
executes subroutines
s runs a line of the script, steps into
subroutines
l list 10 lines after the current line
- list the lines before the current
line
x prints the value of any scalar, list
 u


 Y 

Debugger Commands:
X print out all the variables in the
current package
V same as X except it takes an
optional name of a package
r stops stepping thru the subroutine,
executes rest of it, returns to the
calling place.
c runs the script without stepping thru
 u


 Y 

Debugger Commands:
b to set a break point
S prints all the available subroutines
t turns tracing on or off
h help
|h pause help with breaks
!3 refers to the command no. 3
H -3 lists last 3 commands
 u


 Y 

Debugger Commands:
T show the stack trace
w shows a window around the
current line
L lists all break points
d deletes a break point
D deletes all set break points
@ YÚCKÚ
What is a Package?
Ú package is a namespace ² It is a space that
provides its own global scope for identifiers. It
functions as a private programming space.
How to create Packages?
We can place the code for a Package in its
own file, or in multiple files, or even create
several packages in the same file. To switch
into another package, we use the ¶package·
statement.
YÚCKÚ

D vi packages.pl
package package1;
AEGIN {}
Dx=1;
sub subroutine1 { print ´one\nµ ;}
return 1;
END {}
YÚCKÚ

D vi prg1.pl
require ¶packages.pl·;
package1::subroutine1();
print Dpackage1::x;
To run,
D perl prg1.pl
To know in which package we are,
print __PÚCKÚGE__;
YÚCKÚ

Splitting a Package across files:

D vi file1.pl
package package1;
AEGIN {}
sub sub1 { print ´hello\nµ; }
return 1;
END {}
YÚCKÚ

D vi file2.pl
package package1;
AEGIN {}
sub sub2 { print ´bye\nµ; }
return 1;
END {}
YÚCKÚ
D vi original.pl
require ¶file1.pl·;
require ¶file2.pl·;
package1::sub1();
package1::sub2();
The result is,
hello
bye
YÚCKÚ
Multiple Packages in a single file:
D vi packages.pl
package package1;
AEGIN{}
sub sub1 { print ´hello\nµ; }
return 1
END {}
package package2;
AEGIN{}
sub sub2 { print ´bye\nµ; }
return 1;
END {}
YÚCKÚ

Our Declaration: Our declaration sets global


scope across packages, i.e. variable of one
package can be accessed in another package.
D vi packages.pl
package package1;
our Ddata=1;
sub sub1 { print ´hello\nµ; }
return 1
END {}
YÚCKÚ

package package2;
sub sub2 { print ´Ddata\nµ; }
return 1;
END {}
To run this,
package1::sub1();
package2::sub2();
YÚCKÚ

Creating Modules: Ú Perl Module is just a


package in which the package is defined in a
file with the same name as the package and
has the extension pm.

This allows subroutine names automatically


exported to the code when we include a
Module.
YÚCKÚ

D vi Module1.pm
package Module1;
AEGIN
{ use Exporter();
@ISÚ=qw (Exporter);
@EXPORT=qw (&subroutine1
&subroutine2);
}
sub subroutine1 { print ´hello\nµ ;}
sub subroutine2 { print ´thanks\nµ; }
return 1;
END {}
YÚCKÚ

D vi final.pl
use Module1;
subroutine1();
subroutine2();
D perl final.pl;
CPÚN:There are literally hundreds of
modules that have been developed for
use
with Perl.
YÚCKÚ

Many are available with the Perl Distribution


itself. CPÚN (Comprehensive Perl Úrchive
Network) serves as a repository for user
developed modules. Ún up-to-date survey of
the contents of CPÚN can be found at
www.perl.com
However, there are two drawbacks to the
CPÚN modules. The first is that we have to
download, build and install modules before
they can be used. Some modules may require
YÚCKÚ

compilation which means that we need a


working C compiler.
Secondly, most of the CPÚN modules are
developed for UNIX Perl. For installing
Windows specific modules, there is a special
tool called ppm.
C:\> ppm
PRÚGMÚS:These are the modules that
YÚCKÚ

provide instructions for Perl to behave at both


compile time and run time.
ex:
subs:
allows you to pre-declare subroutine names.
vars:
allows you to pre-declare global variables so
that they can be accepted under ¶strict·
pragma.
YÚCKÚ

strict:
disallows bare words, global variables
and symbolic references.
integer:
allows only integer arithmetic.
constant:
allows to create constant variables at
compile time.
YÚCKÚ

Global variable: Úny variable that is not


explicitly declared with my or local,
automatically becomes a global variable
and is available at any point in that script.
Úny variable declared in a block with my
or local becomes a local variable to that
block.
Difference between my and local:
YÚCKÚ
Ú my variable is only available to the code up
until the nearest enclosing block or subroutine
definition. If we call another subroutine within
that one, the second subroutine will not have
access to those variables. Local variables
declared with local are available to the code
inside that block and subroutine and to the
nested subroutines called from the same
subroutine.
Ú^Ú ^Õ  C 
Renaming files:
rename myfile myfile.bak;
Creating and following links:
link file1, file2; (for hard links)
symlink file1, file2; ( for soft links)
# follow the link (symbolic links)
if ( -l Dfile)
{ Drealfile = readlink Dfile;
}
Ú^Ú ^Õ  C 

Removing files & links:


unlink temp, config, foo;
Other file commands:
chmod changing the file permissions
chown changing the owner of the file
fileno returns the file descriptor
utime changes the time stamp
Ú^Ú ^Õ  C 
Directory Commands:
chdir:
chdir ´imagesµ;
use Cwd;
Dcurr=cwd();
print ´Dcurr\nµ;

File Listing (File Globbing):


@files=<*.pl>;
while (<*.pl>) {
print ¶D_ ,´\nµ;
}
Ú^Ú ^Õ  C 

Directory Commands:
mkdir:
mkdir temp, 0777;
rmdir:
rmdir temp;
Perl and the Environment: Perl
environment variables are stored in a
special hash called %ENV.
Ú^Ú ^Õ  C 

Environment Variables are commonly in


upper case.
To view all the Environment Variables,
foreach Dkey (keys %ENV)
{
print ´Dkey -> DENV {Dkey} \nµ;
}
Ú^Ú ^Õ  C 

To run OS commands,
system (¶ls·);
Dls=`ls`;

K ^
èÕ  

Creating a file:
open (FH,´>myfileµ) or die ´can not create\nµ;
Reading a file:
while (<FH>)
{
«.
}

K ^
èÕ  

Writing to a file:
print FH ´Dinput\nµ;
Closing a file:
close FH;
open options:
> writing
< reading
+> read and write

K ^
èÕ  

File Tests:
-d directory ?
-e file exists ?
-f plain file ?
-l symbolic link ?
-r readable ?
-s ? Aig is the file ?
-w writable ?

K ^
èÕ  

File Tests:
-x executable ?
-z empty file ?
-A binary file ?
-T text file ?
Command line arguments:
Command line arguments are stored in a
special list ¶@ÚRGV·.

K ^
èÕ  

foreach Darg (@ÚRGV) {


print ´Darg \nµ;
}
   odu

[ 
 

 
  

[ 
 

 
      !  " #
" $%
 !$% &$'((
)* $ + ,'

„  '
  %   % &'
 &
 i  i    

Õ
„  
i    
i    
      " #
" 

  
   


 i - &
Y„ 

[ 
 

 

      !  " #


" $%
 !$% &$'((
)* $ + ,'

        ./010*2  % 


%    %
 
3456  
.'
„ 

[ 
 

 

      !  " #


" $%
 !$% &$'((
)* $ + ,'
        ./010*2  % 
%    %
 
3456  
.'

   7  
Õ  
[ 
 

 

      !  " #


" $%
 !$% &$'((
)* $ + ,'

        ./010*2  % 


%    %
 
3456  
.'
   7  
&
 8&  - &''9
- -
8&'9

.-
.
:

)+ ,
:
  -

'
 

[ u o    

u  

d  d 
!o "# $ %$ & u $ % ' du·,
# & ·,# wd·)((d ")Coud ·o * +)

  d d  d !   ",C  ,


 , u o ,
od   Õu d  ,)
  d ! - u 


w ".ow  d !f ow"))/


fo  f d".ow)/
 ,f d,
0
 )* +
0
  d !f  ")
d  d !d o ")

You might also like