Practical approach to perl day2

353 views
307 views

Published on

Learn perl

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
353
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Practical approach to perl day2

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

×