Upload
rakesh-mukundan
View
243
Download
0
Embed Size (px)
DESCRIPTION
Learn perl
Citation preview
Practical Approach to PERL (Day2)
Rakesh Mukundan
String Comparison
We want to check if a string contains the pattern ldquoblahblahrdquo
Consider the strings ldquoI am so bored blahblahrdquo ldquoblahblahblahrdquo ldquoAnd so blahblah am Irdquo ldquoBlahblah is so blahblah ldquo
Regex Perls 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
Contd
Check if the line starts with a string ^ ^blahblah
Matches only to ldquoblahblah is merdquo Dont match to ldquoam so blahblahrdquo
Check if the line ends with a string $ blahblah$
bull Dont match ldquoblahblah is merdquobull Only match ldquoam so blahblahrdquo
Contd
To match any charecter use dot()operator bt
will match to bitbatb1tb0t etc To match one or more character plus(+)
sho+t will match shotshootshoootshooooooot But will not match to sht
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
String Comparison
We want to check if a string contains the pattern ldquoblahblahrdquo
Consider the strings ldquoI am so bored blahblahrdquo ldquoblahblahblahrdquo ldquoAnd so blahblah am Irdquo ldquoBlahblah is so blahblah ldquo
Regex Perls 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
Contd
Check if the line starts with a string ^ ^blahblah
Matches only to ldquoblahblah is merdquo Dont match to ldquoam so blahblahrdquo
Check if the line ends with a string $ blahblah$
bull Dont match ldquoblahblah is merdquobull Only match ldquoam so blahblahrdquo
Contd
To match any charecter use dot()operator bt
will match to bitbatb1tb0t etc To match one or more character plus(+)
sho+t will match shotshootshoootshooooooot But will not match to sht
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Regex Perls 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
Contd
Check if the line starts with a string ^ ^blahblah
Matches only to ldquoblahblah is merdquo Dont match to ldquoam so blahblahrdquo
Check if the line ends with a string $ blahblah$
bull Dont match ldquoblahblah is merdquobull Only match ldquoam so blahblahrdquo
Contd
To match any charecter use dot()operator bt
will match to bitbatb1tb0t etc To match one or more character plus(+)
sho+t will match shotshootshoootshooooooot But will not match to sht
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Contd
Check if the line starts with a string ^ ^blahblah
Matches only to ldquoblahblah is merdquo Dont match to ldquoam so blahblahrdquo
Check if the line ends with a string $ blahblah$
bull Dont match ldquoblahblah is merdquobull Only match ldquoam so blahblahrdquo
Contd
To match any charecter use dot()operator bt
will match to bitbatb1tb0t etc To match one or more character plus(+)
sho+t will match shotshootshoootshooooooot But will not match to sht
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Contd
To match any charecter use dot()operator bt
will match to bitbatb1tb0t etc To match one or more character plus(+)
sho+t will match shotshootshoootshooooooot But will not match to sht
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Contd
To match zero or more characters star() shot
Will match to shtshotshootshooooot To match any charecter any number of times
bt Will match btbotbitbootbooootbaaaaaaat
How to match an operator say plus(+) in string Use escape char() B+ will match ldquoB+rdquo
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
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 acatbcatccat [123]456 will match 145624563456
Fancy way [0-4] is same as [01234] [a-d] is same as [abcd] [0-2a-c] is same as [012abc]
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Example RegexAn IP adress19216811
dd+
d+
d+
d+d+d+d+d+
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Date
03032012d+d+d+
d+d+d+
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Extracting Matches
Consider alpha+gamma It matches string ldquoxxxalphazzzzgammardquo 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
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Extracting Date
Extract datemonthyear from the string ldquo20102012rdquo
if($MyString =~(d+)(d+)(d+)) $date= $1 $month=$2 $year=$3
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
FILE Opening
open(FILEHANDLE MODE EXPR) open($FHlttracetxt) or die $
Modes gt WriteCreate lt Read gtgt AppenedCreate +lt ReadWrite +gt ReadWriteCreate +gtgt ReadAppendCreate
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Reading a File
To read a single line $MyLine = lt$FHgt To read the whole file $MyFile = ltFHgt
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= lt$FHgt) process a line
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
File Closing
Use close function along with file handler close($FH)
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
File Closing
Use close function along with file handler close($FH)
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
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
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
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()
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Values passed to a sub routine will be available in a special array named _
sub MyFunction argArray = _ print Dumper argArray
MyFunction(lsquoarg1rsquo789)
Passing values to sub
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Use return $variable
sub MyFunction $num1 = shift(_) $num2 = shift(_) $sum = $num1 + $num2 return $sum
Returning Values From sub
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Perl Modules
Modules are similar to libraries For code re usability Standard modules are available with perl installation
Eg DataDumper Non standard modules can be downloaded and
installed CPAN Comprehensive Perl Archive Network
Typically will be in a pm file
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Creating a Module
Similar to normal Perl code Start module package MyModule
The file should be named MyModulepm End Module with 1 To use a modulein code use MyModule To call a sub routine in module MyModule-
gtMyFuntion()
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash
Strict Usage
By default perl doesnt 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 methodperl will strictly ask you declare variable
my $MyFirstVar my MyFirstArray my MyFirstHash