22
Practical Approach to PERL (Day2) Rakesh Mukundan

Practical approach to perl day2

Embed Size (px)

DESCRIPTION

Learn perl

Citation preview

Page 1: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 2: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 3: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 4: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 5: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 6: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 7: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 8: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 9: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 10: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 11: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 12: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 13: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 14: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 15: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 16: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 17: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 18: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 19: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 20: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 21: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
Page 22: Practical approach to perl day2

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

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22