72
Perl ESA 2012/2013 Adam Belloum [email protected] Material Prepared by Eelco Schatborn

Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% [email protected]% % % Material%Prepared%by%Eelco%Schatborn%%

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  

ESA  2012/2013  Adam  Belloum  

[email protected]      

Material  Prepared  by  Eelco  Schatborn    

Page 2: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  .  .  .  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 3: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

IntroducDon  

•  “PracDcal  ExtracDon  and  Report  Language”  •  Created  as  a  way  to  make  report  processing  easier.  •  Developed  by  Larry  Wall,  as  system  administrator  at  NASA,  in  the  late  1980s  

•  It's  an  interpreted  language,  but  can  be  compiled  as  well  

•  Is  now  being  used  for:  –  System  administra5on  automa5on  –  Glue  between  systems,  conversion  –  CGI  backend  for  websites  – Much  more.  .  .  

Page 4: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Things  to  know  about  perl  

•  Perl  vocabulary  is  extremely  rich  and  its  grammar  is  very  flexible.  

•   Perl  programmers  usually  say  "There's  more  than  one  way  to  do  it".    

•  In  fact,  you  can  literally  write  a  Perl  script  your  way,  with  your  own  style.  Some  people  even  do  "poetry"  in  Perl  

•  Because  of  this,  some  Perl  scripts  can  be  very  difficult  to  read.  WriDng  them  though,  is  always  a  pleasure.  

Clement  Lefebvre:  h`p://www.linuxforums.org/arDcles/learn-­‐perl-­‐in-­‐10-­‐easy-­‐lessons-­‐lesson-­‐1_120.html  

Page 5: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

A  Perl  program  

h`p://www.perlmonks.org/?node_id=1590  

Page 6: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Poetry  in  Perl  

h`p://www.perlmonks.org/?node_id=1590  

Page 7: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  documentaDon  

•  Every  release  of  Perl  comes  with  documentaDon  in  a  set  of  files.  Most  releases  have  over  1,700  pages  of  documentaDon  included  in  reference  books,  user  guides,  FAQs,  and  so  on.  

•  On  most  operaDng  systems,  a  uDlity  called  perldoc  is  installed  as  part  of  the  Perl  system.    

•  The  perldoc  uDlity  can  search  for  and  format  Perl  documentaDon  for  you.    

•  To  use  perldoc  to  look  up  the  basic  syntax  for  perl,  open  a  terminal  or  console  and  issue  the  command:    perldoc perl

Page 8: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

More  on  perldoc  •  The  Perl  documentaDon  is  divided  into  parts  by  purpose:  

–  perlfunc  (Perl  funcDons)  –  perlfaq  (Perl  FAQs)  –  perlop  (Perl  operators)  

•  To  search  for  a  parDcular  keyword,  use  the  –k  opDons.  For  example  to  look  up  the  print  keyword:    perldoc –tf print

•  To  search  the  FAQs  use  –q  as  an  opDon:   perldoc –q perl

What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future.

Page 9: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  interpreter  

•  Perl  is  an  interpreted  language.    

•  This  means  that  the  code  you  will  write  using  the  Perl  language  will  need  a  program  called  the  Perl  interpreter  in  order  to  be  run.    

•  For  instance,  if  you  write  a  Perl  script  in  a  file  called  myScript.pl  (.pl  is  commonly  used  as  an  extension  for  Perl  scripts),  there  are  two  ways  to  run  a  perl  script.  call  the  interpreter  to  run  it:        $ perl myScript.pl

Clement  Lefebvre:  h`p://www.linuxforums.org/arDcles/learn-­‐perl-­‐in-­‐10-­‐easy-­‐lessons-­‐lesson-­‐1_120.html  

Page 10: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  interpreter:    debugger  

•  Part  of  the  Perl  interpreter  is  a  debugger  that  you  can  use  to  examine  the  execuDon  of  your  Perl  scripts.  

•  The  debugger  is  built  into  every  Perl  interpreter  and  is  acDvated  with  the  –d  opDon  when  launching  the  interpreter:  

perl –d myprog.txt

Page 11: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Using  strict  

•  Perl  has  a  keyword  called  strict.    

•  Using  strict  is  a  good  way  to  enhance  your  programming  abiliDes.    To  do  this,  simply  put:   use strict;

at  the  top  of  your  code.  

Page 12: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Simple  Perl  

A  simple  program  to  start  with:  #!/usr/bin/perl

# This is a sample of Perl code # and this how you write comments in Perl

print "Hi there!\n"; print "This is a very";

print "\tsimple program.\n";  

Page 13: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  subrouDnes  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 14: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables  

•  Three  different  types  of  variables:  scalars, arrays and hashes

•  Start  with  the  dollar  (‘$’),  at  (‘@’)  or  (‘%’)  depending  on  the  type  of  variable  $a = 2; $message = "Hello World!"; @colors = ("red", "green", "blue"); @primaryNumbers = (1, 2, 3, 5, 7); %phoneNumbers = (Alicia => "090-64-773315", Tom => "085-153-3214", Jimmy => "085-285-4545");

•  Variable  names  are  a  punctua5on  character,  a  leBer  or  underscore,  and  one  or  more  alphanumeric  characters  or  underscores  .  

Page 15: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Declaring  variables  

•  Unlike  many  programming  languages,  variables  do  not  need  to  be  declared  prior  to  use  with  Perl.    –   When  the  variable  is  assigned  an  iniDal  value,  Perl  can  figure  out  the  data  type.  

•  If  you  try  to  use  an  uninitalized  variable,  Perl  will    –  use  the  value  zero  for  a  numeric,    –  or  Null  for  a  string.    

•   Avoid  uniniDalized  variables  as  much  as  possible,  as  results  can  be  unpredictable.  

Page 16: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:  Scalars    

•  Start  with  the  dollar  (‘$’)  sign.  •  Can  be  a  number  or  a  string  (or  an  object  reference)  

•  Perl  automa5cally  converts  between  numbers  and  strings.   $n = 8 + "2” à $n  holds  "10” $n = 8 . "2" à $n holds  "82”

Note: concatenaDon  of  strings  using  a  period  `.’  

Page 17: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variable:  Scalars  (Strings)  

•  Escape  sequences  represent  special  characters:  \n,  \t,  .  .  .  

•  Only  in  double  quoted  (‘  “  ’)  strings  •  For  verba5m  strings  use  single  quotes  (‘  ‘  ’)          Perl:    print  "Hi  there!  \n  \t  It's  me.\n";  

     print  'And  me  \n  \t  as  well!';          

•  output:  Hi  there!            It's  me.        And  me  \n  \t  as  well!  

Page 18: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

The  q  and  qq  operators  

•  Perl  allows  you  to  use  these  structures:  –  q(  )  –  qq(  )  Instead  of  single  and  double  quotes,  respecDvely.    So,    qq(This is a test) is  the  same  as    “This is a test”

•  These  can  be  handy  when  embedding  marks  that  would  otherwise  need  escaping:  qq(He said “Help!” then “Now!”)

Page 19: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variable  Scalars  (Numbers)  

•  simple  numbers  as  per  usual:  –  decimal:  12,  -­‐17,  255  .  .  .  –  octal,  start  with  0:  015,  -­‐023,  0777  .  .  .  –  hexadecimal,  start  with  0x:  0xc,  -­‐0x11,  0XfF,  .  .  .  either  case  allowed  for  the  x  or  hex  digits  abcdef.  

FloaDng  point  numbers:  –  “one  and  a  quarter":  1.25  –  “7.25  Dmes  10  to  the  45th  power":  7.25e45.  –   “negaDve  12  Dmes  10  to  the  -­‐24th":  -­‐12e-­‐24.  

Page 20: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:  Array  (1)  

•  Start  with  the  at  (`@')  sign.  •  Arrays  represent  ordered  lists  of  scalars.  •  DefiniDon  using  parenthesized,  comma  delimited  lists:  @numbers = ( 1, 2, 4, 8, 16 );

@strings = ( "one", "two", "3" );

•  The  index  of  an  array  starts  at  0,  not  1  •  Processing  single  items  as  a  scalar  using  `['  and  `]':  

print $strings[1]; # prints "two"

$strings[1] = "2"; # we changed @strings

Page 21: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:  Array  (2)  

•  Arrays  can  be  created  using  scalar  assignment:    $names[0] = "Eelco"; à creates @names to hold ("Eelco")

•  The  length  of  an  array:  $#strings,  returns  the  number  of  items  in  the  array  minus  one.  –   If  equal  to  -­‐1,  the  array  does  not  exist  or  is  empty.    print $#strings; à prints 2 –  Assignment  to  $#strings  changes  the  size  of  the  array.  $#strings = 0; à@strings now holds ( "one" )

Page 22: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:  Hash  (1)  

•  Start  with  the  percentage  (`%')  sign.  •  Made  up  of  keys  and  values.  •  Each  key  has  exactly  one  corresponding  value.  •  DefiniDon  using  parenthesized,  comma  delimited  lists  of  keys  and  values:  %numbers = ("one" => "one", "two" => 2);

•  Processing  a  single  hash  as  a  scalar  using  `{'  and  `}':    print $numbers{"two"}; à prints "2" $numbers{"one"} = "1”; à Change %numbers

Page 23: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:  Array  Hash  (2)  

•  Like  arrays,  hashes  can  be  created  using  scalar  assignment:  

 $strings{"1"} = "one";    à creates %strings to hold ( "1" => "one" )

•  The  keys  <hash>  funcDon  will  return  an  array  filled  with  all  the  keys  in  the  hash.  

 print keys %numbers; à prints ( "one", "two" )

Page 24: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Variables:    $_    

•  The  $_  variable  is  used  by  Perl  as  a  default  variable.  You  can  use  it  in  place  of  variable  names  in  your  scripts:     $_=“This is a test”; print;

•   Use  the  default  operator  carefully  as  it  can  easily  be  confusing.  

Page 25: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Standard  operators  

•  Perl  supports  the  standard  mathemaDcal  operators  +,  -­‐,  *,  /,  %  (modulus)  and  **  (exponent)  

•  Operator  order  of  precedence  applies  according  to  standard  rules;  parentheses  group  operaDons  

•  Operators  can  be  combined  in  statements:  $num1=$num2 * ((3 + 8)*$num3/2);

•  MulDple  assignments  can  be  made  on  one  line:  $num1=$num2=$num3=7;

Page 26: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Boolean  operators  

•  Perl  supports  to  Boolean  AND  and  OR  operators  in  the  same  way  as  other  high-­‐level  languages:        &&    AND        ||      OR  

•  These  operators  are  o�en  used  for  condiDons:  if (($num1 < 10) && ($num1 > 5))

•  Perl  also  allows  the  use  of  the  words  “and”  and  “or”:  if ($num1 < 10 and $num1 > 5)

Page 27: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

String  operators  (relaDonships)  

•  There  are  relaDonal  operators  for  strings  (actually,  for  non-­‐numbers):  eq      equal  to  ne    not  equal  to  gt      greater  than  lt      less  than  ge      greater  than  or  equal  to  le      less  than  or  equal  to    

•  Comparisons  are  le�-­‐to-­‐right,  using  ASCII  values  

Page 28: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Your  second  Perl  script,  the  Calculator!  #!/usr/bin/perl $nbArguments = $#ARGV + 1; print "number of arguments: $nbArgumentsn"; exit(1) unless $nbArguments == 3; $a = $ARGV[0]; $b = $ARGV[2]; $operation = $ARGV[1]; if ($operation eq "+") { $result = $a + $b;} elsif ($operation eq "-") { $result = $a - $b;} elsif ($operation eq "/") { $result = $a / $b;} elsif ($operation eq "x") { $result = $a * $b;} print "$a $operation $b = $resultn";    Make  the  script  executable:  $ chmod a+rx calculator.pl Run  the  script  :    $ ./calculator.pl 5 + 6./calculator.pl 11 - 2./calculator.pl 4 x 3./

calculator.pl 33 /  3  

The  command  line  arguments  are  stored  in  array  @ARGV    -­‐  first  element  $ARGV[0]    -­‐    second  element  $ARGV[1]...  etc.    -­‐    the  index  of  the  last  element  in  the  array                can  be  obtained  by  $#ARGV.    

Two  operators  for  comparing  scalar  variables    ==    and  eq      Unless  is  used  as  a  control  statement    

Page 29: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,    control  statements,  subrouDne  

3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 30: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Code  blocks  

•  A  block  of  code  contain  a  list  of  statements.  •  Code  blocks  are  delimited  by  `{'  and  `}’  •  Each  code  block  is  treated  as  a  unit  by  Perl,  although  execuDon  is  sDll  always  top  to  bo`om  unless  moderated  by  control  structures  

•  Usually  blocks  will  be  associated  with  other  statements,  such  as  if  condiDons.  

Page 31: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Comparison  operators  

•  Perl  supports  the  standard  comparison  operators:  >        greater  than  <      less  than  >=      greater  than  or  equal  to  <=    less  than  or  equal  to  ==    exactly  equal  to  !=      not  equal  to  

Page 32: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

True  and  false  •  In  Perl,  any  condiDon  that  evaluate  to  false  is  assigned  a  value  of  zero.    Anything  that  is  non-­‐zero  is  true.    

•   This  applies  to  condiDons  in  statements  (such  as  the  if  )  as  well  as  for  numeric  evaluaDon:  0    false  3    true  (non-­‐zero)  5-­‐5    false  (evaluates  to  zero)  0.00  false  (it’s  zero  with  precision)  “”    Null  string  is  false  “ ”    String  with  a  space  or  anything  not  null    is        true  

Page 33: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Control  Statements:  CondiDons  (1)  

•  CondiDonal  statements:  if ( test ) { . . . } elsif { . . . }

else { . . . } unless ( test ) { . . . } else { . . . } while ( test ) { . . . } until ( test ) { . . . }

•  Note:  comparison  of  strings  using  `eq'.  0xa == " 10 " à true

0xa eq " 10 " à false

Page 34: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Control  Statements:  CondiDons  (2)  if ($a == 5) { print "It's five!\n";

} elsif ($a == 6) { print "It's six!\n";

} else { print "It's something else.\n";

} unless ($pie eq 'apple') { print "Ew, I don't like $pie flavored pie.\n";

} else { print "Apple! My favorite!\n";

}

Page 35: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Control  Statements:  loops  (3)  

$a = 0; while ($a != 3) { $a++;

print "Counting up to $a...\n";

}

until ($a == 0) { $a--;

print "Counting down to $a...\n";

}

Page 36: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

For  loop  

•  For  loops  are  for  repeDDon  •  Using  a  list  and  an  index  variable  a  block  of  code  will  be  executed  for  each  value  in  the  list.  for $i ( 1, 2, 3, 4 ) { . . . }

•  Using  `..'  you  can  also  give  a  range:  for $i ( 1 .. 3, 4 ) { . . . }

•  The  list  can  contain  any  scalars  or  other  lists.  @range = ( 1 .. 3 ); for $i ( @range, 4 ) { . . . }

Page 37: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

The  exit  statement  

•  The  exit  statement  is  used  to  terminate  a  Perl  script  at  any  Dme.  

•   You  can  send  a  return  status  code  back  to  the  calling  program  with  exit,  if  you  want,  by  appending  the  return  code  a�er  the  exit  statement:  if ($val1 == 0) { exit 0;}

–  This  will  exit  the  program  with  a  return  code  of  0  is  $val1  is  zero.  

Page 38: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Control  Statements:    next  &  last  

•  The  next  statement  causes  execuDon  of  a  loop  to  restart.  –   It  is  similar  to  the  conDnue  statement  in  some  languages.  Any  statements  below  the  next  statement  are  not  executed.      

•  The  last  statement  can  be  used  to  terminate  a  loop,  regardless  of  the  condiDon’s  value.    –  The  last  statement  is  similar  to  break  in  other  programming  languages  

Page 39: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Labels  

•  Blocks  of  code  and  the  for  and  while  statements  can  all  be  labeled,  and  idenDfied  by  that  label.    –  To  use  a  label,  place  it  before  the  statement  block  followed  by  a  colon:  BLOCK1: {statements…}

•  The  name  of  the  label  can  be  any  valid  ASCII  characters.      –  The  convenDon  is  to  use  uppercase  for  labels  so  there  is  no  conflict  with  exisDng  keywords.  

Page 40: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  control  statements,  subrouDne  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 41: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

SubrouDnes  

•  Subs  are  funcDons,  they  can  have  any  arguments  •  The  arguments  are  handed  down  using  the  special  array  @_  

•  A  value  can  be  returned  using  return  Perl: sub multiply {

my (@ops) = @_; return $ops[0] * $ops[1];

}

multiply(2, 3); output: 6

 

Page 42: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  funcDons  and  parentheses  

•  Perl  funcDons  can  be  wri`en  with  or  without  parentheses  in  most  cases,  so  the  statements:     index string, search_string;

and      

 index(string, search_string);

are  idenDcal.    It  is  up  to  you  whether  you  use  parentheses.  

Page 43: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  subrouDne  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 44: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Regular  expressions  (1)  

•  Very  mature,  can  be  highly  complex  •  Can  be  used  for  matching  (tesDng)  or  transliteraDon.  

•  The  matching  operator  `=~  m/…  /'  is  used  for  tesDng  

Perl: if ("This is perl!" =~ m/is/) {

print "Match!";

} output: Match!  

Page 45: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Regular  expressions  (2)  

•  `^'  and  `$'  match  the  beginning  and  end  of  a  string  respecDvely.  

•  You  can  use  character  classes  (`['  .  .  .  `]').  •  You  can  quanDfy  matches  using  `*',  `+'  or  `?'  a�er  a  character  or  character  class.  

•  You  can  quanDfy  matches  using  `{  from  ,  to  }’.  •  `.'  matches  any  character.  •  Use  a  backslash  (`\')  to  escape  characters  like:  

`.',  `^',  `$',  `/',  `\',  `{',  `}',  `[',  `]',  .  .  .  .  

Page 46: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Regular  expressions  (3)  

Metacharacters  in  regular  expressions:    \d      Matches  a  single  digit  character    \w      Matches  a  single  word  character  (a  le`er,  digit        or  underscore)    \s      Matches  a  whitespace  character  

         .  .  .  .  .  .    

Flags  that  can  follow  a  regular  expression:    i      Match  case  insensiDvely    g      Remember  the  current  posiDon  for  the  next            matching

Page 47: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Regular  expressions  (4)  

•  Matching  subexpressions:  –  You  can  match  subexpressions  by  encapsulaDng  them  with  `('  and  `)’  

–  Each  subexpression  is  stored  in  a  variable  $1  .  .  .  $n,  which  can    be  used  later  on.  

Perl: "This is it!" =~ m/\s(..)/;

print $1;

•  QuesDon:  What  is  printed  by  the  above  lines?  

Page 48: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Regular  expressions  (4)  Search  and  replace:  •  Using  s/  regexp  /  replacement  /  

–  Perl: $a = "This is it, or is it?" $a =~ s/\s.(s)/ wa$1/; print $a; –  output: This was it, or is it?

•  Using  the  g    flag  the  replacement  will  be  made  for  all  matches,  instead  of  just  the  first  one.  –  Perl: $a = "This is it, or is it?” $a =~ s/\s.(s)/ wa$1/g; print $a; –  output: This was it, or was it?

Page 49: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  .  .  .  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 50: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

File  Handling  •  Opening  and  closing  files  •  “open”  instrucDon    takes  two  arguments:  the  name  of  a  filehandle  and  

the  name  of  the  file  itself.    –  The  filehandle  is  like  a  variable  which  represents  the  handling  of  the  

file  within  the  script.      open  (CLIENTS,  "clients.txt");  

•  By  default,  the  file  is  open  in  read-­‐mode.  To  open  a  file  in  write-­‐mode  or  append-­‐mode  simply  add  a  “>”  or  “>>”    in  front  of  the  file  name:    open  (CLIENTS,  ">clients.txt");  open  (CLIENTS,  ">>clients.txt");  

Page 51: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

File  Handling:  open  •  “open”  instrucDon  returns    

–  true  if  it  managed  to  open  the  file,    –  false  otherwise.    

•  use  this  value  in  order  to  test  the  success  of  the  operaDon  •   The  following  code  opens  the  file  in  write-­‐mode  and  prints  

“Insufficient  privileges”  if  the  script  doesn't  manage  to  do  so.    open (CLIENTS, ">clients.txt") or print "Insufficient

privileges\n";

•  Remember  to  always  close  the  files  once  you're  finished  with  them.  If  you  don't  your  changes  might  be  lost  

close (CLIENTS);

Page 52: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

File  Handling:  write  

•  Wri5ng  into  files  •  Once  the  file  is  open  in  write  mode  you  can  write  into  it  simply  by  wriDng  into  its  filehandle.  

•   The  “print”  instrucDon  writes  things  on  the  screen  by  default,  but  you  can  specify  a  filehandle  to  write  into.    

open (CLIENTS, ">>clients.txt") or die "Insufficient privileges: $! \n"; print CLIENTS "Mr John Doen"; close (CLIENTS);  

Page 53: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

File  Handling:  read  •  Copying  the  content  of  the  file  into  an  array  

–  Each  line  will  then  correspond  to  an  element  of  the  array:   open(CLIENTS, "clients.txt"); @lines = <CLIENTS>; close(CLIENTS); print @lines;    

•  Looping  through  the  filehandle    –  you  can  loop  through  the  filehandle  in  a  while  loop  by  wriDng  while  ($line  =  )    

•  (think  of  it  as  “while”  there  are  lines  in  the  clients  file,  I'm  assigning  the  current  line  to  $line):  

•      open (CLIENTS, "clients.txt"); while ($line = <CLIENTS> ) { print $line; } close (CLIENTS);

Page 54: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Valid  tests  

•  These  tests  are  all  UNIX  tests  available  to  Perl:  -­‐B    true  if  a  binary  file  -­‐d      true  if  directory  -­‐e      true  if  file  exists  -­‐f      true  if  regular  file  -­‐M    returns  age  in  days  since  last  modificaDon  -­‐r        true  if  readable  -­‐s      returns  size  of  file  in  bytes  -­‐T    true  if  text  file  -­‐w    true  if  writable  -­‐z      true  if  file  exists  but  is  empty  

Page 55: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Your  3rd  Perl  script,  list  of  employees  !  #open  the  employeesfile  open  (EMPLOYEES,"employees.txt");  

 #for  each  line  while  ($line  =)  {  #remove  the  carriage  return  chomp  $line;  #split  the  line  between  tabs  #and  get  the  different  elements  ($name,  $department,  $salary)  =  split  /t/,  $line;  #go  to  the  next  line  unless  the  name  starts  with  "Mr  “  next  unless  $name  =~  /^Mr  /;  #go  to  the  next  line  unless  the  salary  is  more  than  25000.  next  unless  $salary  >  25000;  #go  to  the  next  line  unless  the  department  is  R&D.  next  unless  $department  eq  "R&D";  #since  all  employees  here  are  male,  #remove  the  parDcle  in  front  of  their  name  $name  =~  s/Mr//;  print"$namen”;  }  close  (EMPLOYEES);  

What  this  script  does  to  the  list  of  employees  ?  

 What  can  you  learn  from  the  example  (new  things  we  did  not  discuss)  

Page 56: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  .  .  .  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 57: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Packages  (1)  

•  Packages  are  a  way  to  split  up  perl  code  into  several  pieces  

•  A  package  can  be  included  into  a  piece  of  code  by  using  use  

•  Each  package  represents  a  new  namespace  •  Create  a  package    –  by  creaDng  a  new  file,  generally  ending  in  .pm  –  The  code  for  the  class  you  are  creaDng  goes  in  there.  –  The  package  name  is  the  same  as  the  filename,  this  is  easier  to  use,  but  not  obligatory.

Page 58: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Packages  (2)  •  Create  a  new  file,  for  instance  MyPackage.pm  •  The  first  line  should  provide  the  name  of  the  class  in  the  

 package: package MyPackage;

Next comes the code for the package . . .    •  The  file  should  end  with  1;  to  indicate  to  the  loading  

module  that  the  enDre  package  was  read  successfully.  •  Example:  

   package: package MyPackage; print "WOW, it worked!\n"; 1;

Page 59: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Packages  (3)  

•  To  use  our  package  run  the  following  perl  code:  

 #!/usr/bin/perl use MyPackage;

•  The  output  of  the  program:    WOW, it worked!;

Page 60: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Packages  (4)  •  You  can  refer  to  variables  and  filehandles  in  other  packages  by  

prefixing  the  iden5fier  with  the  package  name  and  a  double  colon:  $Package::Variable

•  You  can  create  subs  in  a  package  in  the  normal  way  

•  They  can  be  called  from  the  program  using  the  package:  $Package::the_sub("well?")

•  Special  code  blocks  can  be  used  for  iniDalizaDon  and  destrucDon: BEGIN {}, END {}, INIT {}, CHECK {}

•  Use  perldoc  perlmod  to  get  more  informaDon

Page 61: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

ESA:  Perl  

•  Today:  1.  Perl  introducDon  2.  Basic  Perl:  types,  variables,  statements,  .  .  .  3.  Regular  expression  4.  File  and  I/O  5.  Packages  6.  Object  Oriented  Perl  7.  DocumentaDon  

Page 62: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Objects  (1)  

“An  Object  is  simply  a  reference  that  happens  to  know  which  class  it  belongs  to.”  

 •  No  special  syntax  for  constructors,  you  have  to  make  your  own  

•  An  object  is  created  using  the  bless()  –  The  bless()  funcDon  creates  a  reference  to  an  object  Anything  can  become  blessed  

 

Page 63: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Objects  (2)  •  using  a  known  reference:  

package Critter; sub newSub { my $self = {}; bless $self; return $self; }

Note:    •  {}      allocates  an  anonymous,  empty  hash  and  returns  a  

reference  to  it  •  bless  takes  that  reference  and  tells  the  object  it  references  

that  it's  now  a  Cri`er,  and  returns  the  reference  •  Using  anonymous  objects  the  class  will  not  know  itself  

  h`p://perldoc.perl.org/perlobj.html  

Page 64: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Classes  (1)  “A  class  is  simply  a  package  that  happens  to  provide  methods  to  

deal  with  object  references.”    •  A  class  provides  the  methods  for  dealing  with  its  reference  •  Methods  have  a  constructor  funcDon,  generally  newSub()  

package Critter; sub newSub { my $self = {}; bless $self; return $self; }

h`p://perldoc.perl.org/perlobj.html  

Page 65: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Classes  (2)  •  A  method  is  simply  a  subrou5ne  that  expects  

–   an  object  reference  (or  a  package  name,  for  class  methods)  as  the  first  argument.  

•  A  constructor  can  call  methods  using  the  reference  package Critter; sub newSub { my $self = {}; bless $self; $self->initialize(); return $self; }

h`p://perldoc.perl.org/perlobj.html  

Page 66: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Classes  (3)  

•  Classes  can  be  inherited  •  For  that  use  the  two-­‐argument  version  of  bless  •  Bless  an  object  into  a  class  

package Critter; Sub newSub { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(); return $self; }

h`p://perldoc.perl.org/perlobj.html  

Page 67: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

documentaDon  

•  BOOKS!,  they  are  in  the  back  of  the  classroom.  .  .  •  Use  the  web,  there  are  a  lot  of  websites  on  perl  •  Check  www.perl.com  for  help.  •  Find  out  about  perldoc  

Material  for  these  slides  was  taken  from  h`p://www.perl.com/pub/a/2000/10/begperl1.html  

Page 68: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Assignment  Create  a  simple  program  that  can  keep  track  of  your  mp3's.  

 1.  first  program  the  following  using  only  packages,  no  OO          1.1.  store  the  names  and  locaDons  of  your  mp3's  in  a  large  array  or        hash,  and  allow  for  addiDonal  parameters  to  be  stored  as  well,  like        preference  (1-­‐5  stars),  number  of  Dmes  played,  etc.          1.2.  create  a  simple  interface  for  searching/adding/deleDng/ediDng        your  database.  Make  sure  to  use  subs  for  these  funcDons.  Make  use  of  RE...    2.  (OTIONAL)  recreate,  using  the  previous  code,  the  assignment  using  OO.          2.1  create  a  main  program  that  can  run  mulDple  databases  at  the  same.        Each  database  must  be  a  separate  object  instance.  Provide  an  interface        that  allows  the  user  to  choose  and  switch  between  the  databases  at  will.    3.  (BONUS)  add  funcDons  for  (re)storing  your  databases    

Page 69: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

Perl  Script  Exercise:  Netswitch  •  In  this  exercise,  we  want  to  be  able  to  switch  between  

networks.  We  defined  network  configuraDon  files  in  a  directory  called  "networks".  

•  For  instance,  here  is  the  content  of  ./networks/home:    interface=eth2  type=dhcp  proxy=none  And  here  is  the  content  of  ./networks/work:    interface=eth1  type=dhcp  proxy=www-­‐proxy.work.com  proxy_port=8080  

Page 70: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

SoluDon  (1)  #!/usr/bin/perl #default values

$interface="none";

$type="none";

$address="none";

$gateway="none";

$dns="none";

$proxy="none";

$proxy_port = "none";

#gather information from the network file $network = $1;

$networkFile = "./networks/$network";

open (NETWORK, "$networkFile") or die "$networkFile not found or not readable \n";

Page 71: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

SoluDon  (2)  while ($line = ) { chomp $line; ($variable, $value) = split /=/, $line; if ($variable eq "interface") $interface = $value; elsif ($variable eq "type") $type = $value; elsif ($variable eq "address") $address = $value; elsif ($variable eq "gateway") $gateway = $value; elsif ($variable eq "dns") $dns = $value; elsif ($variable eq "proxy") $proxy = $value; elsif ($variable eq "proxy_port") $proxy_port = $value; } #make sure the type and interface are defined if ($interface eq "none") die "Interface name not defined \n"; if ($type eq "none") die "Network type (dhcp, static) not define \n"; if ($type eq "dhcp") { print "Network type: dhcpn"; #just get an IP address and settings from the DHCP Server system("dhclient"); } elsif ($type eq "static") { print "Network type: staticn”;

Page 72: Perl% - OS3 · Perl% ESA2012/2013% Adam%Belloum% a.s.z.belloum@uva.nl% % % Material%Prepared%by%Eelco%Schatborn%%

SoluDon  (3)  #bring the interface up if ($address eq "none") die ("IP address not definedn"); system("ifconfig $interface $address up"); if ($gateway ne "none") { print "Gateway: $gatewayn"; system("route add default gw $gateway"); } if ($dns ne "none") { print "DNS Server: $dnsn"; $strNameServer = "cat "."'"."nameserver $dns"."' > /etc/

resolv.conf"; system($strNameServer); } } else die "Bad network type : $type. Use dhcp or static.n";