Вы находитесь на странице: 1из 132

TIMTOWDI

There is More Then One Way To Do It

Prepared By: Rohit Sabharwal


Reviewed By:
Approved By:
Version: 2.5
Perl ~ Truth and Myth
 Practical Extraction & Reporting Language
 Initially meant for Text Processing
 Available on all platforms Unix, Linux, VMS, Windows
 Interpreted-Compiled Language
 Rapid Application Development
 No rigid framework
 Partially OO
 Slower than compiled languages
Running Perl
Invoking Perl
 Unix
Put starting Hash-Bang line as
#!/usr/local/bin/perl
Make your script executable
#chmod +x sample.pl
 Windows

Associate .pl extension with perl interpreter


Basic Command Line Switches
 Usage: /usr/bin/perl [switches] [--] [program-file] [arguments]
 -c Syntax check
 -d Runs program under debugger
 -e `command` Command line program
 -I `directory` Specify @INC
 -S Use PATH variable to find script
 -T Enable tainting checks
 -v Print version, subversion
 -V Print configuration summary
 -w Enable many useful warnings
 -W Enable all warnings
First Perl Program
PERL Program
#!c:\perl\bin\perl
use v5.6.1; # use 5.6.1; use 5.006_001;
use warnings; #important progma
use strict; #no symbolic references,
#undeclared variables, barewords
$| = 1; #unbuffer stdout
print “what is your username ?”; #print on screen
my $username = <>; #Take input from screen
chomp($username); #remove newline character
print “Hi, $username.\n”; #print on screen
# ‘#’ Is comment in perl and statements end with ;
# Blocks are defined by { }
Perl Basic Data types
Literals

 ‘Hello World’
 “Bye World”
 3
 3.5
 -123
 0xff (hex)
 0377 (octal)
Strings ‘string’
• Interpolation does not take place
• Everything is taken as per it’s face value
• Only exception being ‘\’ character
• Escape special meaning of ‘ with \ => ‘rohit\’s’
Strings “string”
 Everything is not taken on it’s face value
 Special meaning can be escaped with backslash character
 Interpolation: -
$user = “ROHIT”;
print “$user”;
prints ROHIT
print “\$user”;
prints $user
Numerical Literals
42; # The number 42
12.5; # A floating point number
101873.000; # 101,873
.005; # ve thousandths
5E-3; # same number as previous line
23e-100; # 23 times 10 to the power of -100 (very small)
2.3E-99; # The same number as the line above!
23e6; # 23,000,000
23_000_000; # The same number as line above
Data Types
•Scalars
Strings
Single quoted
Double quoted
Numerals
•Arrays
 Single Dimensional
 Multi Dimensional
•Hashes
 Simple Hash
 Hash of Hash
More Data Types
 References
 Scalar Reference: $ScalarRef = \$scalar;
# $$ScalarRef to dereference
 Array Reference: $ArrayRef = \@Array;
# @$ArrayRef to dereference
 Hash Reference: $HashRef = \%Hash;
# %$HashRef to dereference
• FileHandles
 open FILEHANDLE, $filename;
 Undefined
 Undefined value: $undef = undef;
 Typeglobs
 *name => ($name, @name, %name,
file-handles,code references,Format templates
Scalars
• Most basic data type
• Stores String and numerical literals
• $scalar_variable_name is preceded by $ symbol
$scalar = “hello”;
$scalar = 123;
$scalar = 10.12;
$scalar = ‘T’;
$scalar = “\n”;
Scalar Semantics
 All numerical and string operations supported
$val = 3 # Numerical
Assignment
$sum = 1 + $val; # Sum
$lastname = “Sabharwal”; # String Assignment
$fullname = “Rohit ” . $lastname; #
Concatenation
 Type casting during runtime based on operation
Operators
Operators
 Numerical Operators
 Comparison Operators

 Increment-Decrement Operators

 String Operator

 Assignment Operator

 Short-cut Operators

 Relational Operators
Numerical Operators

OPERATOR ASSOCIATIVITY DESCRIPTION


** RIGHT EXPONENTIATION
* LEFT MULTIPLICATION
/ LEFT DIVISION
% LEFT MODULUS
+ LEFT ADDITION
- LEFT SUBSTRACTION
Comparison Operators
OPERATION NUMERIC VERSION STRING VERSION RETURNED VALUE
LESS THAN < lt 1 if. $left is less than $right
LESS THAN OR EQUAL TO <= le 1 if. $left is less than or equal to $right
GREATER THAN > gt 1 if. $left is greater than $right
GREATER THAN OR EQUAL TO >= ge 1 if. $left is greater than or equal to $right
EQUAL TO == eq 1 if. $left is the same as $right
NOT EQUAL TO != ne 1 if. $left is not the same as $right
-1 if . $left is less than $right
0 if . $left is equal to $right
COMPARE <=> cmp 1 if . $left is greater than $right
Increment-Decrement Operators
 Increment Operator
 ++$x; #Pre Increment
 $x++; #Post Increment
 --$x; #Pre Decrement
 $x--; #Post Decrement
String Operator
 String Operator
 ‘x’ #To create multiple of a string
$B = “A” x 4;
#Value of $B becomes “AAAA”

 ‘.’ #To Concatenate two strings


$B = “I”;
$a = $B . “ AM ROHIT”;
#Value of $a becomes “I AM ROHIT”;
Assignment Operator
 ‘=‘ #To assign right-hand value to left-hand value
#Left hand value must be a variable not a constant
$a = “PerlTraining”; #Assigning a string
$b = 123; #Assigning a numeral
$c = $a; #Assigning $a’s value to $c
Short-cut Operators
 $a += 1; #means $a = $a + 1;
 $a *= 1; #means $a = $a * 1;
 $a /= 1; #means $a = $a/1;
 $a -=1; #means $a = $a –1;
 $a .=“Hello”; #means $a = $a . “Hello”;
Relational Operators
 if($A == $B && $C == $D)
{
print “Condition True\n”;
}
#&&, || are relation operators
Control Structures
Control Structures(Decision)
 if
 if else
 if elsif else
 unless
if
if(condition)
{
statements executed if condition is true;
} #Note braces are always required unlike ‘C’

#!c:\perl\bin\perl.exe
print “Enter your age\n”;
$age = <>;
chomp($age);
if($age > 35){
print “You are overage\n”;
}
if-else
if(condition){
statements executed if condition is true;
}
else{
statements executed if condition is false;
}
#!c:\perl\bin\perl.exe
print “Enter your age\n”;
$age = <>;
chomp($age);
if($age > 35){
print “You are overage\n”;
}
else{
print “You are selected\n”;
}
if-elsif-else
if(condition-1){
statements executed if condtion-1 is true;
}
elsif(condition-2){
statements executed if condition-2 is true and condition-1 is false;
}
else{
statements executed if condition-1 and condition-2 are false;
}
if-elsif-else
#!c:\perl\bin\perl.exe
print “Enter your age\n”;
$age = <>;
chomp($age);
if($age > 35){
print “You are overage\n”;
}
elsif($age < 21){
print “You are underage\n”;
}
else{
print “You are selected\n”;
}
unless
unless(condition)
{
statements executed when condition is false;
}

#!c:\perl\bin\perl.exe
print “Enter your age\n”;
$age = <>;
unless($age > 35){
print “You are OK\n”;
}
Control Structures(Iteration)
 while
 until
 do while
 do until
 for
 foreach
while
while(expression){
statements executed till expression is true;
}

$a = 5;
$b = 1;
while($a > $b){
print “$b\n”;
$b++;
}
Until

until(expression){
statements executed till expression is false;
}

$a = 5;
$b = 1;
until($a < $b){
print “$b\n”;
$b++;
}
do-while
do
{
statements to be executed if expression is true;
statements will be executed once after which, expression is checked;
}while(expression)

$a = 5;
$b = 1;
do{
print “$b\n”;
$b++;
}while($a > $b);
do-until
do
{
statements to be executed if expression is false;
statements will be executed once after which, expression is checked;
}until(expression)

$a = 5;
$b = 1;
do{
print “$b\n”;
$b++;
}until($a < $b);
for
for(initialization;expression;increment-or-decrement)
{
statements to be executed till the expression is true;
}

$a =5;
for($i=1;$i<=$a;$i++){
print “$i\n”;
}
foreach
foreach scalar-variable (list)
{
scalar-variable will have value of each element in list, changing
at each iteration;
}
#list can be array or hash
@Arry = (1,2,3,4,5);
foreach $i (@Arry){
print “$i\n”;
}
Next, Last, Redo
LABLE: while (EXPR) {
#redo always comes here and condition is not evaluated
redo LABLE if /:/;
next LABLE if /,/;
last LABLE if /^$/;
do_something;
} continue {
# next always comes here, executes next iteration of loop
do_something_else;
# then back the top to re-check EXPR
}
#last always comes here
Rest of data types
Arrays
 List of scalars
 Accessed using index
 Dynamic memory allocation
 Random access to Elements
 represented by ‘@’ character in front of array name

@a = (); # this list has no elements; the empty list


@a = qw//; # another empty list
@a = ("a", "b", "c",1, 2, 3); # a list with six elements
@a = qw/hello world how are you today/; # another list with six elements
@a = (1 .. 100); # a list of 100 elements: the numbers from 1 to 100
@a = ('A' .. 'Z'); # a list of 26 elements: the uppercase letters From A to Z
@a = ('01' .. '31'); # a list of 31 elements: all possible days of a month
Array Variables
•Elements accessed using $Array_Name[$element_no] notation
@a = (“hello”, “my”, “name”, “is”, “rohit”);
$a[0] = ‘hello’, $a[1] = ‘my’, $a[$#a] is “rohit”
• Last index of array is $#Array_Name
•Array length is $#Array_Name + 1
• Very large array $#Array = 50000
@someStuff = qw/Hello and welcome/; # An array of 3 elements
$#someStuff = 0; # @someStuff now is simply ("Hello")
$someStuff[1] = "Joe"; # Now @someStuff is ("Hello", "Joe")
$#someStuff = -1; # @someStuff is now empty
@someStuff = (); # does same thing as previous line
Array – Insert, Modify, Delete
 For Array @Array
 Insert an element
 $Array[0] = ‘A’;

 push(@Array,’A’,”B”,$C,@D,%E);

 unshift(@Array,’A’,”B”,$C,@D,%E);

 Modify an element
 $Array[0] = ‘B’;

 Delete an element
 delete($Array[0]);

 pop(@Array); # Removes last ($#Array) element

 shift(@Array); # Removes first (0th) element


Multi-Dimensional Array
 List of Lists of scalars
 Element access using Index

 represented by ‘@’ Character in front

@Array = ([“one”, “two”,”three”],


[“red”,”yellow”,”orange”],
[“left”,”middle”,”right”],);
print $Array[0][2]; #prints third element first row – ‘three’
print $Array[2][1]; #prints second element, third row – ‘middle’
Searching Lists -- Arrays
@MyArry = (5,3,4,6,2,1);
foreach my $element (@MyArry) {
if($element == 2){
print “$element found\n”;
last;
}
}
Sorting Lists -- Array
#Sorting Array
@sorted = sort{$a <=> $b} @unsorted #Ascending numerically
@sorted = sort{$b <=> $a} @unsorted #Descending numerically
@sorted = sort{$a cmp $b} @unsorted #Ascending alphabetically
@sorted = sort{$b cmp $a} @unsorted #Descending alphabetically
Example:-
@MyArry = (5,3,4,6,2,1);
foreach my $element (sort{$a <=> $b} @MyArry) {
print “$element\n”;
}
Array as Stack
 Arrays can be used as stacks i.e., LIFO using ‘push’ and ‘pop’
e.g.
@stack=();
push(@stack, 7, 6, "go"); # @stack is now qw/7 6 go/
$action = pop @stack; # $action is "go", @stack is (7, 6)
$value = pop(@stack) + pop(@stack); # value is 6 + 7 = 13, @stack is empty
Array as Queues
 Arrays can be used as a Queue i.e. FIFO using ‘pop’ and ‘unshift’
@queue=();
unshift (@queue, "Customer 1"); # @queue is ("Customer1")
unshift (@queue, "Customer 2"); # @queue is ("Customer2"
"Customer1")
unshift (@queue, "Customer 3"); # @queue is ("Customer3"
"Custome2” "Customer1")
$item = pop(@queue); # @queue is now ("Customer3" "Customer2")
print "Servicing $item\n"; # prints: Servicing Customer1\n
$item = pop(@queue); # @queue is now ("Customer3")
print "Servicing $item\n"; # prints: Servicing Customer2\n
Hash
 Index is arbitrary scalars values
 Preceded by ‘%’
 key => value pair: Rohit => 25

%Emp_Age = (); #Empty Hash


%table = qw/schmoe joe smith john simpson bart/; # intialization
@lastNames = keys %table; # qw/schmoe smith simpson/
@firstNames = values %table; # @firstNames is: qw/joe john bart/
Hashes vs Arrays

Array Hash

Key Value Key Value


joel grow
1 joel bill clinton
2 bill george bush
2 two
3 al what_the heck
4 john
Hash – Insert, Modify,Delete
 Inserting Key=>Value
$Emp_Age{“ROHIT”} =25;
 Modifying Key=>Value(only value)
$Emp_Age{“ROHIT”} =26;
 Deleting Key=>Value
delete($Emp_Age{“ROHIT”} );
 Accessing Key=>Value
while ( ($key, $value) = each(%table) ) {
print “$key => $value \n”;
}
#or
if(defined($hash{$key}){
#do something with $key or value $hash{$key}
}
Hash
#hardcoded hash
of Hash
%identities = (
'RohitSabharwal' =>{
'Name' =>{'First' =>"Rohit",'Last' =>"Sabharwal"},
'Phone' =>{'Home' =>"123 4567890", 'Work' =>undef},
'Address' =>{'Street' =>"N-3 XYZ", 'City' => "New Delhi", 'Country' => "India"
},
'NavinSabharwal' =>{
'Name' =>{'First' =>"Navin",'Last' =>"Sabharwal"},
'Phone' =>{'Home' =>"123 4567890", 'Work' =>undef},
'Address' =>{'Street' =>"N-3 XYZ", 'City' => "New Delhi", 'Country' => "India"
},
);
print “$identities{'RohitSabharwal'}{'Name'}{'First'} \n”;
print “$identities{'NavinSabharwal'}{'Name'}{'First'} \n”;
Searching Lists -- Hash
 Searching Keys and Values
while ( ($key, $value) = each(%table) ) {
if($key eq ‘smith’){
print “$key => $value \n”;
}
if($value eq ‘john’){
print “$key => $value \n”;
}
}
#Using defined
if(defined($hash{‘smith’}){
print “smith => $hash{‘smith’} \n”;
}
Sorting Lists -- Hash

#Sorting Hash
#Alphabetically or numerically based on keys
@keys = sort { $a cmp or (<=>) $b } (keys %hash);
foreach $key (@keys){
$value = $hash{$key};
print “$key = > $value \n”;
}
#Alphabetically or numerically based on values
foreach $key (sort{$hash{$a} cmp or (<=>) $hash{$b}} keys %hash){
print “$key => $hash{$key}\n”;
}
HASH Excercise
 Enter Employee name and age comma separated
 Rohit,28,Rahul,30,Guru,40
 Operation(Add,Replace,Delete,List)?
 Add
 Username and age comma separated?
 Arjun,50
 Arjun aged 50 added
 Operation(Add,Replace,Delete,List)?
 Replace
 Username for replacement
 Arjun
 Enter new age for Arjun
 40
 User Arjun’s age changed to 40
 Operation(Add,Replace,Delete,List)?
 Delete
 Username to delete?
 Arjun
 Arjun deleted
 Operation(Add,Replace,Delete,List)?
 List
 Rohit 28
 Rahul30
 Guru 40
 Operation(Add,Replace,Delete,List)?
File Handling
File Handling
 A file is opened using open function
 To read

open(FILE_HANDLE, “file-path\\filename”) or warn “Can not open


filename: $!”;
 To create\overwrite

open(FILE_HANDLE, “>file-path\\filename”) or die “Can not create


filename: $!”;
 To append

open(FILE_HANDLE, “>>$filename”) or die “Can not append


$filename: $!”;
 To read and write

open(FILE_HANDLE, “+>$filename”) or die “Can not read-write


$filename: $!”;
 To close an open file

close FILE_HANDLE;
File Handling Example
FOR WRITING FILE
#!/usr/bin/perl
open(FILE, “>test.txt”) or die “Can not create test.txt: $!”;
print FILE “This is a test\n”;
close FILE;

FOR READING FILE


#!/usr/bin/perl
open(FILE, “test.txt”) or die “Can not open test.txt: $!”;
while(<FILE>){
print “$_”;
}
close FILE;
Regular Expressions
Regular Expression
Pattern Matching:
The basic pattern matching operations:
Matching : String matches pattern
Substitution : Portion of string matched by pattern is replaced with
given literal
Definition of Regular Expressions:
“A regular expression is a set of rules describing a generalized string. If
the characters that make up a particular string conform to the rules of
a particular regular expression, then the regular expression is said to
match that string”
Ex:- /b./ Matches : ‘bovine’, ‘above’, ‘Bobby’, and ‘Bob Jones’
/b./ Does not match :’Bell’, ‘b’, or ‘Bob’.
Assertions

Assertions : anchor parts of the pattern to word or string boundaries


The ^ assertion matches the start of a string
Regular expression ^fool
Matches : fool and foolhardy
Does not match: tomfoolery or April fool.
Regular Expression Assertions

Assertion Matches Example Matches Doesn't Match

^ Start of string ^fool foolish tomfoolery

$ End of string fool$ April fool foolish

\b Word boundary be\bside be side beside

\B Non-word boundary be\Bside beside be side


Atoms
Atoms are fundamental building blocks of a regular expressions
The . as in b. is an example of a regular expression atom

Regular Expression Atoms

Atom Matches Example Matches Doesn't


Match
. Any character b.b bob bb
except newline

List of Any one of those ^[Bb] Bob, bob Rbob


characters in characters
square brackets
Regular Anything that ^a(b.b)c$ abobc abbc
expression in regular expression
parentheses matches
Quantifiers
Quantifier : quantifies number of times atoms should appear in a pattern
Regular Expression Atom Quantifiers
Quantifier Matches Example Matches Doesn't Match

* Zero or more instances ab*c ac, abc abb


of the atom

+ One or more instances ab+c abc ac


of the atom

? Zero or one instances ab?c ac, abc abbc


of the atom

{n} n instances of the atom ab{2}c abbc abbbc

{n,} At least n instances of ab{2,}c abbc, abbbc abc


the atom

{n,m} At least n, at most m ab{2,5}c abbc abbbbbbc


instances of the atom
Special Characters
Regular Expression's Special Characters
Symbol Matches Example Matches Doesn't Match
\d Any digit b\dd b4d bad

\D Non-digit b\Dd bdd b4d

\n Newline
\r Carriage return
\t Tab
\f Formfeed
\s White space character
\S Non-white space character
\w Alphanumeric character a\wb a2b a^b

\W Non-alphanumeric character a\Wb aa^b aabb


Regular Expression Semantics
• Match Operator : /regular expression or $regular_expression/
•Similar Operator : =~
• if($filename =~ /dat$/){die "Can't use .dat files.\n";}
• Not Similar Operator : !~
• if($ENV{'PATH'} !~ /perl/){ warn “No Perl";}
• Alternate Match Operator :
m!regular expression!
m^regular expression^
m : Operator can use any character as match operator
• Switches: ‘g’, ‘i’,’o’
• $a =~ /regular expression/gio
• g = Global, Match in whole string for all occurrences of pattern
• i= Case insensitive match
• o= Compile only once, in case of regular expression is a variable
The match variables

 $& #Last Successful pattern match in scope


 $` #String Preceding whatever was matched
 $‘ #String Proceeding whatever was matched
 $+ #Last bracket that was matched
 $1, $2.. # Matched Elements
Regular Expression Example
Open test.txt in notepad and write these 4 lines :-
Test.txt--------------------------------
Hello 123 how are u?
I am fine only three left me
R u 12 only now?
Yes
-----------------------------------------
#!c:\perl\bin\perl.exe
open(FILE, “test.txt”) or die “Can not open: $!”;
while(<FILE>){
if($_ =~ /\d+/){
print “$_”;
}
}
Regex Expression Example2
#!/usr/bin/perl
if(scalar(@ARGV) == 2){
if($ARGV[1] =~ /$ARGV[0]/){
print "Maching\n";
}else{
print "Did not match\n";
}
}else{
print "Usage $0 pattern_to_be_used string_to_match\n";
}
Back-references
• Use parenthesis for sub patterns
• Sub pattern matches will be saved in $1, $2, $3...
• $1, $2, $3 are called Back-references
• Back-references will have value even if other portions did not match
• Back-references will have last matched value if multiple matches
• Count left parenthesis rule followed
• $+ will have value of last bracket that matched

EX1 : Back References


$_ = 'http://www.intel.com/default.asp'; #Example string
/^http:\/\/([^\/]+)(.*)/; # In [^\/] ^ is used to negate /
print "host = $1\n";
print "path = $2\n";
#host = www.intel.com
#path = /default.asp
Greedy or Non Greedy
 Regex behaviour
 Match as soon as possible
 Match as much as possible
 Match characters like ‘+’ and ‘*’ gobble up characters till
there is a match
 Greedy match

$_ = "This 'test' isn't successful?";


#Hoping to match 'test'
($str) = /('.*')/;
#Matches 'test' isn'
 Non greedy match

$_ = "This 'test' isn't successful?";


($str) = /('.*?')/; # *? Works in Perl version 5 otherwise use
/‘[^’]*’/
#Matches 'test'
List Context Match
EX2 : Array Context
@hits = ("Yon Yonson, Wisconsin" =~ /(\won)/g);
print "Matched on ", join(', ', @hits), ".\n";

@hits will be “yon, Yon, son, con”


Note that if patterns are matched in an array context as above special
variables $1, $2, ..., and $`, $', and $& are not set
Safe Pattern Matching
 Subjected to same interpolation as double
quote
 Use quotemeta to escape special meaning
 Escape special meaning when pattern
source unreliable
chomp($pat = <STDIN>);
$quoted = quotemeta $pat;
#For example, hello :-)
#Now hello\ \:\-\) which is safe to match
Substitution
• The substitution operator is s///
• The optional i, g, and o switches apply to substitution too
• The pattern to be replaced goes between the first and second delimiters
• The replacement pattern goes between the second and third
• There should be variable on left of =~
$house = "henhouse";
$house =~ s/hen/dog/;
Now, $house = “doghouse”

$Greet = “Hello how are you”;


$Greet =~ s/\s+/_/g; #Global substitution of spaces with underscore
Now, $Greet = “Hello_how_are_you”
Running External
Commands
Running External Commands
 system (“Command”, “Args”); #System
 $output = `command`; #Backtick
 @output = `command`; #Backtick, output in an array
 open(CMD_HANDLE, “Command Line |”) #Output Piped open
while(<CMD_HANDLE>){
print $_;
}
 open(CMD_HANDLE, “| Command Line”) #Input piped open
print CMD_HANDLE “Username \n Password \n $Data \n”;
close CMD_HANDLE;
Example mail sender
open MAIL, ("|/usr/sbin/sendmail -t") || die "Unable to
send mail: $!";
print MAIL "To: $from\n";
print MAIL "From: root\n";
print MAIL "Subject: mime parser test\n\n";
print MAIL “Hi Rohit\nHow are you?\nGiving a
training I guess?\n”;
close MAIL;
Command Line
Arguments
Command Line Arguments
 All command line arguments are automatically stored in
@ARGV
#!/usr/bin/perl –w
my %args;
if (scalar(@ARGV)%2){
die “Odd number of arguments passed to $0”;
}
else{
%args = @ARGV; #Convert Array to hash
foreach (keys %args){
die “Bad argument ‘$_’ does not start with –” unless /^-/;
}
}
Functions
Functions
Text processing related built-in functions
chomp(); #used for removing line terminators
Example:-
chomp($_); #removes new line character from current
line
chop(); #used for chopping of one character from string
end
Example:-
$name = “ROHIT”;
for($i=1;$i<=2;$i++) {
chop($name);
}
print $name;
Functions

length(); #Gets length of string


Example:-
length($string);
split ();
Example:-
while (<FILE>) {
chomp; # avoid \n on last field
@array = split /:/ , $_; # split current line delimited by ‘:’
#& push them in array @array
}
built-in functions – partial list
 Functions for SCALARs or strings
 chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct,
ord, pack, q/STRING/, qq/STRING/, reverse, rindex, sprintf,
substr, tr///, uc, ucfirst, y///
 Regular expressions and pattern matching
 m//, pos, quotemeta, s///, split, study, qr//
 Numeric functions
 abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt,
srand
 Functions for real @ARRAYs
 pop, push, shift, splice, unshift
 Functions for list data
 grep, join, map, qw/STRING/, reverse, sort, unpack
 Functions for real %HASHes
 delete, each, exists, keys, values
 Functions for fixed length data or records
 pack, read, syscall, sysread, syswrite, unpack, vec
built-in functions (cont.)
 Input and output functions
 binmode, close, closedir, dbmclose, dbmopen, die, eof,
fileno, flock, format, getc, print, printf, read, readdir,
rewinddir, seek, seekdir, select, syscall, sysread, sysseek,
syswrite, tell, telldir, truncate, warn, write
 Functions for filehandles, files, or directories
 -X, chdir, chmod, chown, chroot, fcntl, glob, ioctl, link,
lstat, mkdir, open, opendir, readlink, rename, rmdir, stat,
symlink, umask, unlink, utime
 Keywords related to the control flow of your perl program
 caller, continue, die, do, dump, eval, exit, goto, last, next,
redo, return, sub
 Keywords related to perl modules
 do, import, no, package, require, use
 Functions for processes and process groups
 alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe,
qx/STRING/, setpgrp, setpriority, sleep, system, times,
wait, waitpid
 Time-related functions
built-in functions (cont.)
 Keywords related to classes and object-orientedness
 bless, dbmclose, dbmopen, package, ref, tie, tied, untie,
use
 Low-level socket functions
 accept, bind, connect, getpeername, getsockname,
getsockopt, listen, recv, send, setsockopt, shutdown,
socket, socketpair
 System V interprocess communication functions
 msgctl, msgget, msgrcv, msgsnd, semctl, semget, semop,
shmctl, shmget, shmread, shmwrite
 Fetching user and group info
 endgrent, endhostent, endnetent, endpwent, getgrent,
getgrgid, getgrnam, getlogin, getpwent, getpwnam,
getpwuid, setgrent, setpwent
 Fetching network info
 endprotoent, endservent, gethostbyaddr, gethostbyname,
gethostent, getnetbyaddr, getnetbyname, getnetent,
getprotobyname, getprotobynumber, getprotoent,
Subroutines
Subroutines
 User written functions
 Are called like functions
 Makes code clean
 Code reuse
 Start with keyword ‘sub’
 Input parameters are arrays
 Called in program as
 &sub_name Old method
 sub_name(); New method
Subroutine Example
#!c:\perl\bin\perl.exe
print “I am not in subroutine\n”;
print “Enter Your Name\n”;
$name = <>;
chomp($name);
print “Calling subroutine\n”;
&print_my_name($name);
print “Finally I am out of subroutine\n”;
sub print_my_name
{
my @name = @_; #@_ by reference, @name by value
print “I am $name[0] and I am in subroutine now\n”;
}
Variable Scope
 If not defined variables are global in scope
 my(); #Lexical local variable
#Only available in local scope
 local(); #Dynamic local variable
#Value visible to subroutines
#Localizing special variables
Text Processing
Regex to process data
 Regex operations are slower
 Do not use regex for simple string operations
 For simple string operations use index, rindex, substr, and
tr///.
 Example
 This is a natural application for split:
($a, $b, $c) =~ /^(\S+)\s+(\S+)\s+(\S+)/;
 String comparison operators twice as fast than Regex

do_it() if $answer eq 'yes'; #Fastest way to compare string


equality
do_it() if $answer =~ /^yes$/; #Slower
do_it() if $answer =~ /yes/; #Too slow and buggy ‘my eyes’
Index operator
 The index operator locates an occurrence of a shorter string
in a longer string
 The rindex operator locates the rightmost occurrence, still
counting character positions from the left:
$_ = "It's a Perl Perl Perl Perl World.";
$left = index $_, 'Perl'; #7
$right = rindex $_, 'Perl'; #22
Substr
 The substr operator extracts a portion of a string, given a
starting position and (optional) length
 The substr operator is much faster than a regular
expression written to do the same thing
$str = "It's a Perl World.";
print substr($str, 7, 4), "\n"; #Perl
print substr($str, 7), "\n"; #Perl World
Substr for substitution
 Substr and index
substr($str, 7, 4) = "Small"; #It’s a Small World
$str = "It's a Perl World.";
substr($str, index($str, 'Perl'), 4) ="Mad Mad Mad Mad";
#It’s a Mad Mad Mad Mad World
 s/// operator

$str =~ s/Perl/Mad Mad Mad Mad/; #Probably faster


 substr, index and tr///, translation

$str = "It's a Perl World.";


substr($str, index($str, 'Perl'), 4) =~ tr/a-z/A-Z/;
#It’s a PERL World.
Split for clarity
 Split is more clear and efficient than Regex
($a, $b, $c) = split /\s+/, $_;
#Get first 3 fields of $_
($a, $b, $c) = split;
# Splits $_ on whitespace by default
Unpack for efficiency
 Data in organized columns

chomp (@ps = `ps l`); #Collect output


shift @ps;
for (@ps) {
($uid, $pid, $sz, $tt) = unpack '@3 A5 @8 A6 @30 A5 @52 A5',
$_;
print "$uid $pid $sz $tt\n";
}
#'@3 A5 @8 A6 @30 A5 @52 A5‘ is called template
# to know more about template run ‘perldoc –f pack’;
Using Modules
Modules
 A module is basically a collection of
subroutines (and sometimes variables) that
increases the abilities of Perl
 Often, modules are put together by other
people, and distributed for public use
 Two types of modules:
 Standard (built in): Modules which are so
useful (or popular) that they are included
with the standard distributions of Perl
 Custom installed : Modules which are added
to a distribution of perl by an end user
do,require,use directives

#To add code from many files or modules


 do ‘filename.pl’;
 # old not used any more
 require ‘filename.pl or module::name ’;
 # runtime check
 use ‘filename.pl or module::name’;
 # compile time check
Modules Semantics

 To Use Modules use Module_Name;


 Module functions can be used by
Module::Module_Function;
 Example :-
Module
use Win32;
use Win32::OLE;
use File;
use File::Find;
Module Variable
$File::Find::name
Module Function
find(\&sub_name, '/pattern');
Modules Example
EX1: Current Working directory module
use Cwd; # import names from Cwd::
$here = getcwd();
print $here;
EX2: Net-Ping module to ping hosts
use Net::Ping;
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();
Others
Net::SSH, Net::SCP, Net::DNS, File::Copy
Perl Resources
 www.cpan.org

 www.perl.com

 www.activestate.com
BACKUP
Reports
Reports
 Perl provides FORMAT data-type and format
function for formatting reports
 syntax
#To Create
format MYFORMAT =
This is a @<<<<< justified field
“left”
.
#To Write
write MYFORMAT;
Reports
 Anchors
@, ^
 Auto Repeating Line
~~
 Placeholders
Placeholder Alignment Example
< Left Justified @<<<<
> Right Justified @>>>>
| Center Justified @||||
# Numeric @####
* Multiple Line @*
. Floating Point @####.##
% Percentage @##%
 Special Variables
Variable Corresponds To
$= The page length
$% The page number
$- The number of lines remaining
$^L The formfeed string
Report Example
@employee = ("Rohit", "Naveen", "Abhinav", "Rajeev");
@salary = (1000, 2000, 3000, 4000);

format STDOUT_TOP =
@|||||||||||||||||||||||
"Employee Salary Report";
------------------------
@<<<<<<<<<< @<<<<<<<
"NAME", "SALARY";
------------------------
.
for($i=0;$i<=3;$i++)
{
format STDOUT =
@<<<<<<<<<< @####.##
$employee[$i], $salary[$i];
.
write;
}
print "------------------------\n";
$footer = "\nEnd Page $%\n";
print $footer;
Reports — Script Output

Employee Salary Report


------------------------
NAME SALARY
------------------------
Rohit 1000.00
Naveen 2000.00
Abhinav 3000.00
Rajeev 4000.00
------------------------

End Page 1
Debugging
Errors & Warnings
 Catch $!
 Use warn or die
 Catch $^E to get OS reported errors
 Use eval{} block to catch $@
 use Fatal qw(open close sysopen);
 use POSIX qw(:errno_h);
 use Carp, crap, croak, cluck, confess
 use Sys::Syslog;
 use Win32::Eventlog;
Debugging
 Compile perl with –DDEBUG option
 use debug flags(Next slide)
 use ‘print’ or own debug routines
 Example:- Profiling a script
perl –d:DProf script.pl
dprofpp
OUTPUT
Total Elapsed Time = -0.00034 Seconds
User+System Time = 0.129317 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
46.4 0.060 0.120 2 0.0300 0.0599 main::BEGIN
15.4 0.020 0.020 1 0.0200 0.0200 vars::BEGIN
Debugging
This shows the debug flags, which can be specified with the -D command-line option. If you specify a
number, you can simply add all the numbers of each flag together so that 6 is 4 and 2. If you use the letter as a
flag then simply list all the options required. The following two calls are equivalent:
#perl -d -Dls test.pl
#perl -d -D6 test.pl Perl Debugging Flags
Flag Number Flag Letter Meaning of Flag
1 p Tokenizing and parsing
2 s Stack snapshots
4 l Label stack processing
8 t Trace execution
16 o Operator node construction
32 c String/numeric conversions
64 P Print preprocessor command for -P
128 m Memory allocation
256 f Format processing
512 r Regular expression parsing
1024 x Syntax tree dump
2048 u Tainting checks
4096 L Memory leaks (not supported anymore)
8192 H Hash dump; usurps values()
6384 X Scratchpad allocation (Perl 5 only)
32768 D Cleaning up (Perl 5 only)
Subroutine Recursion
One the most powerful features of subroutines is their ability to call
themselves. There are many problems that can be solved by repeated
application of the same procedure. However, care must be taken to set up
a termination condition where the recursion stops and the execution can
unravel itself. Example is the calculation of a factorial value:
#!/usr/bin/perl -w
# # Example factorial using recursion
for ($x=1; $x<100; $x++) {
print "Factorial $x is ",&factorial($x), "\n";
}
sub factorial {
local($x) = @_;
if ($x == 1) {
return 1;
}
else {
return ($x*($x-1) + &factorial($x-1));
}
}
Perl Command Line Switches
 Usage: C:\Perl\bin\Perl.exe [switches] [--] [program-file]
[arguments]
 -0[octal] specify record separator (\0, if no argument)
 -a autosplit mode with -n or -p (splits $_ into @F)
 -C enable native wide character system interfaces
 -c check syntax only (runs BEGIN and CHECK blocks)
 -d[:debugger] run program under debugger
 -D[number/list] set debugging flags (argument is a bit mask
or alphabets)
 -e 'command' one line of program (several -e's allowed,
omit program file)
 -F/pattern/ split() pattern for -a switch (//'s are optional)
 -i[extension] edit <> files in place (makes backup if
extension supplied)
Perl Command Line Switches
 -Idirectory specify @INC/#include directory (several -I's allowed)
 -l[octal] enable line ending processing, specifies line terminator
 -[mM][-]module execute `use/no module...' before executing program
 -n assume 'while (<>) { ... }' loop around program
 -p assume loop like -n but print line also, like sed
 -P run program through C preprocessor before compilation
 -s enable rudimentary parsing for switches after programfile
 -S look for programfile using PATH environment variable
 -T enable tainting checks
 -u dump core after parsing program
 -U allow unsafe operations
 -v print version, subversion (includes VERY IMPORTANT perl info)
 -V[:variable] print configuration summary (or a single Config.pm variable)
 -w enable many useful warnings (RECOMMENDED)
 -W enable all warnings
 -X disable all warnings
 -x[directory] strip off text before #!perl line and perhaps cd to directory
Perl Command Line Execution

The -e option is handy for quick Perl operations from the command line.
Want to change all instances of "oldstring" in Wiffle.bat to "newstring"?
Try
perl -i.old -p -e "s/ oldstring/ newstring/g" wiffle.bat
This says: "Take each line of Wiffle.bat (-p); store the original in
Wiffle.old (-i); substitute all instances of oldstring with newstring (-e);
write the result (-p) to the original file (-i)."
perl -e 's/gopher/http/gi' -p -i.bak *.html
Changing ‘gopher’ to ‘http’ in multiple files
Predefined Variables
 $! #Keeps Last Error or Exception
 $@ #Array of string indicating source of last exception
 $& #Last Successful pattern match in scope
 $` #String Preceding whatever was matched
 $‘ #String Proceeding whatever was matched
 $+ #Last bracket that was matched
 $1, $2..# Matched Elements
 $~ #Last match in current scope
 $. #Current line number of the last file read
 $_ #Last input line of string
 $0 #Name of perl script that is being executed
 $* #Command line arguments given for the script
 $$ #The process number of running script
 $? #Status of the executed child process
 $],$^V #Perl Version
Special Characters
 \\ => An actual, single backslash character
 \$ => A single $ character
 \@ => A single @ character
 \t => Tab
 \n => Newline
 \r => Hard return
 \f => Form feed
 \b => Backspace
 \a => Alarm (bell)
 \e => Escape
 \033 => Character represented by octal value, 033
 \x1b => Character represented by hexadecimal value, 1b
Some RegEx examples
# convert to UPPERCASE
# returns $line = $line (all in uppercase)
$line =~ tr/a-z/A-Z/;
# convert to lowercase
# returns $line = $line (all in lowercase)
$line =~ tr/A-Z/a-z/;
# remove leading whitespace
# returns $line with no leading whitespace
$line =~ s/^\s+//;
# remove trailing whitespace
# returns $line with no trailing whitespace
# note: this will also remove \n
$line =~ s/\s+$//
Some RegEx examples contd
# check for Y2K date (4-digit year specified)
# format exactly: nn/nn/nnnn or nn-nn-nnnn
# returns true if proper format
($line1 =~ m/[0-9]{2}[\/|-][0-9]{2}[\/|-][0-9]{4}/)
# find word that is bolded
# returns: $1 = 'text‘
$line = "This is some <b>text</b> with HTML <tags> and ";
$line =~ m/<b>(.*)<\/b>/i;
# convert <STRONG> to <B>
$line =~ s/<(\/?)strong>/<$1b>/gi;
# strip HTML tags (not the best way)
# returns $line = 'This is some with HTML and words'
$line = "This is some <b>text</b> with HTML <tags> and words";
$line =~ s/<(.*?)>//gi;
The Bit-Wise Operators

~ Bit-wise NOT: $op = ~0x01; #(result 0xFE or 254)


& Bit-wise AND: $op = 0x02 & 0x01; #(result 0x00)
| Bit-wise OR: $op = 0x02 | 0x01; #(result 0x03)
^ Bit-wise EXOR: $op = 0x0F ^ 0xF0; #(result 0xFF or 255)
>> Shift Right: Shift right one bit is the same as div by 2
$num = 8; $op = $num >> 2; #($op holds 2)
<< Shift Left: Shift left one bit is the same as times 2
$num = 8; $op = $num << 2; #($op holds 32)
EX: Substitution in Multiple files
#Following script substitutes gopher to www in multiple files
while(<*.html>) {
$oldname = $_;
open(OLD,"<$oldname")
|| die "Can't open input file $oldname $!";
s/\.html$/\.new/;
$newname = $_;
open(NEW,">$newname")
|| die "Can't open output file $newname $!";
while(<OLD>) {
s/gopher/www/gi;
print NEW; }
close(OLD);
close(NEW);
$backupname = $oldname;
$backupname =~ s/\.html$/\.bak/;
unlink $backupname
|| die "Can't delete old backup file $backupname $!";
rename $oldname, $backupname
|| die "Can't rename $oldname to $backupname $!";
rename $newname, $oldname
|| die "Can't rename $newname to $oldname $!";
}
EX: TIMTOWDI
All of the following lines are equivalent but are not easy to
read.

if ($x == 0) {$y = 10;} else {$y = 20;}


$y = $x==0 ? 10 : 20;
$y = 20; $y = 10 if $x==0;
unless ($x == 0) {$y=20} else {$y=10}
if ($x) {$y=20} else {$y=10}
$y = (10,20)[$x != 0];
Looking Forward, Looking Back
 Lookahead operator
 Positive Lookahead operator ?=
 /(?=foo)bar/;
 Negative Lookahead operator ?!
 /(?!foo)bar/;
 Lookbehind Operator
 Positive Lookbehind operator ?=<
 /(?=<foo)bar/;
 Negative Lookbehind operator ?!<
 /(?!<foo)bar/;
Forking ?
 Forking is cloning
 Kernal function
 Data Structures of parent copied to child
 Environment variables, UserID etc...
 File and socket handles copied to child
 Copy on write
 Data structures copied only when written in
parent
 Forking is not multi-threading
 Process can have threads not vice-versa
Fork Example
use POSIX ":sys_wait_h"; # required for Symbol WNOHANG
@Child_Data = (1,2,3,4,5);
foreach $data(@Child_Data){
my $pid = fork();
warn "Fork failed\n" unless defined $pid;
next if $pid; # get parent to reiterate and fork more kid
child_process($data);
exit; # kill child after it is finished
}
do {
$kids = waitpid(-1,&WNOHANG); # Main must process waits for
children
} until $kids == -1;
#child subroutine
sub child_process{
print "Running child @_\n";
}
Fork Example with timeout
sub child_process{
eval {
my($timeout) = @_;
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout; # create signal SIGALRM after 5 seconds
print "I am in @_\n"; # Child’s working code goes here
for(;;){ sleep 1; } # Processing forever for test
alarm 0; # resetting alarm signal in case everything went fine
};
if ($@) {
print “@_ timed out\n”;
die unless $@ eq "alarm\n"; # timed out
}
}
Exec

 Code Overlay function for forked child


 Changes code for child

 Copied data structures remain same

 Exec never returns unlike system


Exec Example
$pid=fork();
die "Cannot fork: $!" if (! defined $pid);
if (! $pid) { # Only the child does this
exec("/usr/bin/new.pl"); die "Could not exec: !$";
} #new.pl is new code for child
Writing Unix Daemon
use POSIX qw(setsid); #For demonizing script
chdir '/' or die "Can't chdir to /: $!"; # change to / required
umask 0; # For files that your daemon may write
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null:
$!";
open STDERR, '>/dev/null' or die "Can't write to /dev/null:
$!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
while(1){
# Working code goes here
}
Parallel::ForkManager
use Parallel::ForkManager;
$pm = new Parallel::ForkManager($MAX_PROCESSES);
foreach $data (@all_data) { # Forks and returns the pid for the
child
my $pid = $pm->start and next; #work with $data in the child
process
$pm->finish; # Terminates the child process
}
Expect
use Expect;
Module
$ssh = Expect->spawn("/usr/bin/ssh", $hostname); # Doing SSH
$r = $ssh->expect($timeout, -re => '[Pp]assword'); # Sending
Password
if(defined($r)){
$ssh->send_slow(0, "$passwd\n"); # Sending password
$r = $ssh->expect($timeout, -re => '\>'); # Expecting a ‘>’ prompt
from tcsh
if(defined($r)){ #if prompt is returned
$ssh->clear_accum(); # Removing previous stuff from buffer
$ssh->send(“some_cmd\n"); # Running a command on remote
system
}
$r= $ssh->expect($timeout, -re => '\>'); # Expecting prompt again
if(defined($r){
$output = $ssh->exp_before(); # Collecting commands output in
$output
$ssh->send("exit\n"); # Exiting SSH session
}

Вам также может понравиться