2. String Comparison
We want to check if a string contains the pattern
“blahblah”
Consider the strings
“I am so bored blahblah”
“blahblahblah”
“And so blahblah am I!”
“Blahblah is so blahblah!! “
3. Regex: Perl's Way
To check if a pattern exists in a string variable
$MyString =~ /blahblah/;
The expression will return 1 if a match is
found else 0
Use it inside an if condition
if($MyString =~ /blahblah/){ }
To check if a pattern is not present in a string
$MyString !~ /blahblah/
4. Contd..
Check if the line starts with a string : ^
/^blahblah/
Matches only to “blahblah is me”
Don't match to “am so blahblah”
Check if the line ends with a string : $
/blahblah$/
• Don't match “blahblah is me”
• Only match “am so blahblah”
5. Contd..
To match any charecter use dot(.)operator
/b.t/
will match to bit,bat,b1t,b0t etc
To match one or more character: plus(+)
/sho+t/
will match shot,shoot,shooot,shooooooot
But will not match to sht
6. Contd..
To match zero or more characters: star(*)
/sho*t/
Will match to sht,shot,shoot,shooooot
To match any charecter any number of times
/b.*t/
Will match bt,bot,bit,boot,boooot,baaaaaaat
How to match an operator say plus(+) in string?
Use escape char()
/B+/ will match “B+”
7. Few more
Matching a digit: d
Matching a non digit : D
Matching white space : s
Matching any of the specified char square bracker[]:
Eg:/[abc]cat/ will match to acat,bcat,ccat
/[123]456/ will match 1456,2456,3456
Fancy way: /[0-4]/ is same as /[01234]/
/[a-d]/ is same as /[abcd]/
/[0-2a-c]/ is same as /[012abc]/
10. Extracting Matches
Consider /alpha.+gamma/
It matches string “xxxalphazzzzgamma”
Suppose we want to extract the match
Place the match in single bracket() matched
value will be available in the variable $1
if($MyString =~/alpha(.+)gamma/){
Print $1
}
11. Extracting Date
Extract date/month/year from the string
“20/10/2012”
if($MyString =~/(d+)/(d+)/(d+)/){
$date= $1;
$month=$2;
$year=$3;
}
13. Reading a File
To read a single line $MyLine = <$FH>
To read the whole file $MyFile = <@FH>
Not recommended as it will try to load the
entire file into memory
Instead use a loop
Safer way to process a large file
while($MyLine= <$FH>){
#process a line
}
14. File Closing
Use close function along with file handler
close($FH);
15. File Closing
Use close function along with file handler
close($FH);
16. Log Parser
Open the log file and count the number of lines
Count the number of packets
Identify Unique IPs and number of occurances of
each IP
Identify the IPs exchanging ICMP traffic
Identify missed pings if any
17. Functions(Sub Routines)
Used for code re use and maintainability
No need to declare subroutines, define and use
Defining
Sub MyFunction {
#code to be executed
}
Calling a function
MyFunction();
18. Passing values to sub
Values passed to a sub routine will be available in a
special array named @_
sub MyFunction{
@argArray = @_;
print Dumper @argArray;
}
MyFunction(‘arg1’,789);
19. Returning Values From sub
Use return $variable;
sub MyFunction{
$num1 = shift(@_);
$num2 = shift(@_);
$sum = $num1 + $num2;
return $sum;
}
20. Perl Modules
Modules are similar to libraries
For code re usability
Standard modules are available with perl installation
Eg: Data::Dumper
Non standard modules can be downloaded and
installed
CPAN : Comprehensive Perl Archive Network
Typically will be in a .pm file
21. Creating a Module
Similar to normal Perl code
Start module package MyModule;
The file should be named MyModule.pm
End Module with 1;
To use a modulein code use MyModule;
To call a sub routine in module MyModule-
>MyFuntion();
22. Strict Usage
By default perl doesn't need any variable to be
declared before use
Simple spelling mistakes in variable names can lead
to hours of code debugging!
By using the strict method,perl will strictly ask you
declare variable
my $MyFirstVar;
my @MyFirstArray;
my %MyFirstHash;