SlideShare a Scribd company logo
Perl Welcome Pratical Extraction and Reporting Language Marcos Rebelo oleber@gmail.com
Perl - Larry Wall Larry Wall, programmer, linguist, author, born March 10, 1949 in Duncan, British Columbia, Canada. Perl computer language creation in 1987.
Quotes: The three chief virtues of a programmer are: Laziness, Impatience and Hubris
There's More Than One Way to Do It
What is Perl? Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including web development, system administration, network programming, code generation and more. It's intended to be practical rather than beautiful.
Supports both procedural and OO programming.
It has powerful built-in support for text processing
It has one of the world's most impressive collections of third-party modules (http://www.cpan.org).
Perl – Pros Quick development phases
Open Source, and free licencing
Excellent text handling and regular expressions
Large experienced active user base
Fast, for an interpreted language
Code developed is generally cross plaform
Very easy to write powerful programs in a few lines of code
Perl – Cons Limited GUI support
Can look complicated, initially, particularly if you're not familiar with regular expressions
Consider using for Web applications
System administration
Most command line applications
Batch processing
Reports
Database access and manipulation
Any application involving significant text processing
Hello World in a file: print "Hello, World!\n" on the shell #> perl -e 'print "Hello, World!\n"' with perl 5.10 #> perl -E 'say "Hello, World!"'
Basic syntax overview Perl statements end in a semi-colon. say("Hello, world!") ;
You can use parentheses for subroutines arguments or omit them according to your personal taste.  say "Hello, world!";
Comments start with a  #  and run to the end of the line. # This is a comment
Basic syntax overview Double quotes or single quotes may be used around literal strings: say( " Hello, world! " ); say( ' Hello, world! ' );
However, only double quotes interpolate variables and special characters such as newlines (\n): print("Hello,  $name!\n ");    # interpolate $name print('Hello,  $name!\n ');    # prints $name!\n literally
Basic syntax overview Whitespaces are irrelevant: print(    "Hello world");
... except inside quoted strings: print("Hello    world");   # this would print with a   # line break in the middle.
Use feature qw(say) Perl 5.10 as added a new feature use feature qw(say); say  'Hello, world!'; Is equivalent to: print 'Hello, world!', "\n"; We will see later that the correct expression is: {   local $\ = "\n";    print 'Hello, world!' }
Running Perl programs To run a Perl program from the Unix command line: #> perl progname.pl
Alternatively, put this shebang in your script : #!/usr/bin/perl and run your executable script #> progname.pl
-e : allows you to define the Perl code in the command line to be executed, -E to get 5.10  #> perl -e 'print("Hello, World!\n")'
Perl variable types Scalars my  $ animal = 'camel'; my  $ answer = 42; my  $ scalar_ref = \$scalar;
Arrays my  @ animals = ('camel', 'lama'); my  @ numbers = (23, 42, 69); my  @ mixed = ('camel', 42, 1.23);
Associative Array / Hash Tables my  % fruit_color = (   apple  => 'red',   banana => 'yellow' );
Scalar Type Scalar values can be strings, integers, floating point numbers or references. The variable name starts with a  $ . my  $ animal  = 'camel';
my  $ answer  = 42;
my  $ scalar_ref = \$scalar; Perl will automatically convert between types as required. print('3' + 4.5); # prints 7.5
Array Type An array represents a list of scalars. The variable name starts with a  @ . my  @ animals = ('camel', 'llama');
my  @ numbers = (23, 42, 69);
my  @ mixed = ('camel', 42, 1.23); Arrays are zero-indexed, negative index start by -1 at the end of the array.  print( $ animals [0] ); # prints "camel"
print( $ numbers [-1] ); # prints 69
Array Slice To get multiple values from an array: @ numbers [0,1] ; # gives (23, 42);
@ numbers [0..2] ; # gives (23, 42, 69);
@ numbers [1..$#numbers] ;  The single element we're getting from the array start with a  $ , the sub-array start with  @ .
@ numbers [2, 1]  =  @ numbers [1, 2] ;
($scalar1, $scalar2) = @numbers;
Array Type The special variable  $# array  tells you the index of the last element of an array: print($mixed[ $# mixed]); # prints 1.23 You might be tempted to use  $#array+1  to tell you how many items there are in an array. Using  @array  in a scalar context will give you the number of elements: if (@animals < 5) { ... }
if ( scalar( @animals )  < 5) { ... }
Associative Array Type Hash table A hash represents a set of key/value pairs: my  % fruit_color = (   'apple', 'red',    'banana', 'yellow' ); You can use whitespace and the  =>  operator to lay them out more nicely: my  % fruit_color = (   apple  =>  'red',    ' banana '   =>  'yellow' );
Associative Array Type Hash table To get at hash elements: $ fruit_color{ ' apple ' }; # &quot;red&quot;
$ fruit_color{banana};  # &quot;yellow&quot; You can get at lists of keys and values with  keys()  and  values() . my @fruits =  keys (%fruit_colors);
my @colors =  values (%fruit_colors); Hashes have no particular internal order, though you can sort the keys and loop through them.
Hash Slice To get/set multiple values from an hash @ fruit_color { 'watermelon', 'orange' }  =   ('green',  'orange'); Removing repetition from an array my @array = (1,2,9,5,2,5); my %hash ; @hash{@array} = (); @array =  keys(%hash) ; say &quot;@array&quot;; # 1 9 2 5
Variable Reference Scalar references \$ scalar,  \ '' ,  \ 123 Array references \@ array,  [ 'camel', 42, 1.23 ] Hash references \% hash,  { apple => 'red', banana => 'yellow' } Unref a variable $$ scalar_ref,  @$ array_ref,  %$ hash_ref
Complex data types More complex data types can be constructed using references: my $cities =  {   IT =>  [  'Milan', 'Rome', ... ] ,   PT =>  [  'Lisbon', 'Oporto', ... ] ,   ... } ; You can access it throw: print($cities ->{ ' IT' } -> [ 1 ] ); print($cities ->{ ' IT' }[ 1 ] );
my @citiesPT =  @{ $cities->{PT} } ;
Lexical Variables Throughout the previous slides the examples have used the syntax: my  $var = 'value'; The  my  is actually not required, you could use: $var = 'value'; However, the above usage will create global variables throughout your program.  my  creates lexically scoped variables instead. The variables are scoped to the block in which they are defined.
Lexical Variables Not having the variables scoped is usually a bad programming practice. my $a = 'foo'; if ($some_condition) {   my $b = 'bar';   print($a); # prints &quot;foo&quot;   print($b); # prints &quot;bar&quot; } print($a); # prints &quot;foo&quot; print($b); # prints nothing; $b has   # fallen out of scope
Lexical Variables It’s recommended to use  my  in combination with a  use strict  at the top of your programs. Using the pragma strict is highly recommended.  use strict; my $first = &quot;first&quot;; # OK $second = &quot;second&quot;;  # NOT OK Also highly recommended is the pragma  warnings  for having aditional warnings. You can enable or deseable this warnings.
Magic variables There are some ‘magic’ variables. These special variables are used for all kinds of purposes. $_ : is the ‘default variable’.
@ARGV : the command line arguments to your script.
$ARGV : contains the name of the current file when reading from  <>  or  readline() .
@_ : the arguments passed to a subroutine.
$a ,  $b : Special package variables when using  sort() .
$/ : The input record separator, newline by default.
Most common operators Arithmetic +  +=  addition
-  -=  subtraction
*  *=  multiplication
/  /=  division Boolean logic &&  and
||  or
!  not
Most common operators Miscellaneous =   assignment
.   string concatenation
x   string multiplication
..  range operator (creates a list of numbers)
Most common operators
Conditional constructs Perl has most of the usual conditional if  COND BLOCK
if  COND BLOCK  else  BLOCK
if  COND BLOCK  elsif  COND BLOCK
if  COND BLOCK  elsif  COND BLOCK  else  BLOCK The  COND  shall be conditional statement surround by  (  and  ) , and  BLOCK  shall be one ore more statements surround by  {  and  } . if ( is_valid( $value ) ) { … }
Conditional constructs There's also a negated version of if (don't use it): unless  ( is_valid( $value ) ) {    ...  }
This is provided as a 'more readable' version of  if  (  not(  is_valid( $value )   )  ) {    ...  }
0, '0', '', () and undef are all false in a boolean context. All other values are true.
Conditional constructs Note that the braces are required in Perl, even if you've only got one line in the block. However, there is a clever way of making your one-line conditional blocks more English like: The traditional way if ($zippy) {     print(&quot;Yow!&quot;);  }
The Perlish post-condition way print(&quot;Yow!&quot;)  if $zippy; print(&quot;No cubes&quot;)  unless $cubes;
while looping constructs Perl has most of the usual loop constructs. While Loop: while   ( is_valid( $value ) ) { … }
There's also a negated version (don't use it): until   ( is_valid( $value ) ) { … }
You can also use while in a post-condition: print(&quot;xpto\n&quot;)  while  condition;
Going throw a hash: while  (my ($key,$value) =  each (%ENV)){   print &quot;$key=$value\n&quot;; }
for looping constructs for  (my $i=0; $i <= $max; $i++) {   ... } The C style for loop is rarely needed in Perl since Perl provides the more friendly foreach loop. foreach  my $i (0 .. $max) {   ... }
foreach looping constructs Passing all elements of an array,  foreach  is an alias to  for . foreach  my $var (@array) { ... }
for  my $value (values(%hash)) { ... }  By default the value goes on  $_ foreach  (@array) { print &quot; $_ \n&quot; }  Changing the variable, changes the value inside the array.  $var  is an alias. for  my $var (@array) { $var  +=  $var }
Jump on loops LINE:  while ( defined(my $line = <>) ) {   next LINE  if not is_valid( $line );   #... }
Jump on loops: last LABEL : immediately exits the loop in question
next LABEL : starts the next iteration of the loop
redo LABEL : restarts the loop block without evaluating the conditional again If the LABEL is omitted, the command refers to the innermost enclosing loop.
Inverting the cycle With a single command print  foreach  (0..9) With multiply commands do {   ... }   while  ($true)
Warning:  last ,  next , and  redo  don't work in this case.
Exercises 1 - Scalars 1) Implement the ‘Guess the lucky number’. The program shall chose a random number between 0 and 100 and ask the user for a guess. Notice the user if the guess is bigger, smaller or equal to the random number. If the guess is correct the program shall leave otherwise re-ask for a number.
Exercises 1 - Array 2) Create an array that contains the names of 5 students of this class.  2a) Print the array.  2b) Create a new Array shifting the elements left by one positions (element 1 goes to 0, …) and setting the first element in the last position. Print the array.  2c) Ask a user to input a number. Print the name with that index.
Exercises 1 - Hash 3) Homer Family my %relatives = (   Lisa  => 'daughter',    Bart  => 'son',    Maggie => 'daughter',    Marge  => 'mother',    Homer  => 'father',    Santa  => 'dog'); 3a) Print all the characters name’s.  3b) Demand for a name and print is position. 3c) Print all the characters position’s, no repetitions.  3d) Demand for a position and print the name.
Subroutines
Subroutines The Perl model for subroutine call and return values is simple:  all subroutine are passed as parameters one single flat list of scalars (goes to  @_ )
all subroutines likewise return to their caller one scalar or a list of scalars.
lists or hashes in these lists will collapse, losing their identities - but you may always pass a reference.
The subroutine name start with a  & , for simplification can be omitted.
Subroutine - example sub  max {   my $mval =  shift(@_) ; #  my ($mval, @rest) =  @_ ; # big copy   foreach my $foo ( @_ ) {   if ( $mval < $foo ) {   $mval = $foo;   }   }   return $mval; }
my $my_max =  max( 1, 9, 3, 7 ) ; print $my_max; # prints 9
Subroutine input and output The parameters are passed to the subroutine in the array  @_ , changing the values of the array will change the values outside the subroutine call. sub double {    $_[0] *= 2 ;  } my $b = 5; double($b); print($b); # prints 10 The last statement value is returned by default. print(double($b)); # prints 20
Subroutine input and output Using  @_  is dangerouse and shall be carefully considered. It's always possible to do a copy. sub double {   my ($a) = @_;   $a *= 2;   return $a; } my $b = 5; print( double( $b ) ); # prints 10 print($b);  # prints  5
Persistent Private Variables Just because a lexical variable is statically scoped to its enclosing block, eval, or file. {   my $secretVal = 0;   sub gimmeAnother {   return ++$secretVal;   } } print gimmeAnother; # OK ++$secretVal;  # NOT OK
state variables From perl 5.10 you may use static variables. use feature 'state'; sub gimmeAnother {   state  $secretVal = 0;   return ++$secretVal; } print gimmeAnother; # OK Some features in perl 5.10 have to be activated to avoid colisions with old code. Activating all them: use feature ':5.10'; use v5.10; # use perl 5.10
Named Parameters We can implement named parameters using a hash. This provides an elegant way to pass in parameters without having to define them formally.  sub login {   my %param = @_;   ... } login(   user=>'User',    pass=>'Pass' );
Named Parameters We may pass the hash directly.  sub login {   my ($param) = @_;   ... } login({   user=>'User',    pass=>'Pass' });
Named Parameters We can easily give default values by checking the hash. sub login {   my ($param) = @_;    $param->{user} = $DEFAULT_USER   if not exists $param->{user};   $param -> {pass} = $DEFAULT_PASS   if not exists $param->{pass};   $param->{host} = $DEFAULT_HOST   if not exists $param->{host};   ... }
Named Parameters We can easily give default values by checking the hash. sub login {   my $param = {   'user' => $DEFAULT_USER,   'pass' => $DEFAULT_PASS,   'host' => $DEFAULT_HOST,    %{shift(@_)}   };   ... }
Named Parameters We can also write the subroutine so that it accepts both named parameters and a simple list. sub login {   my $param;    if ( ref($_[0]) eq 'HASH' ) {    $param = $_[0];    } else {    @{$param}{qw(user pass host)}=@_;    }   ... } login('Login', 'Pass'); login({user => 'Login', pass => 'Pass'});
Exercises 2 1) Create a new subroutine that calculates the Fibonacci series. Using this subroutine, do a program that receives multiple numbers as argument and prints the Fibonacci value. F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) 1a) with presistent variable 1b) with state variable
IO
Read a File open( FH, '<', 'path/to/file' )     or die &quot;can't open file:  $! &quot;; while ( defined( my $line =  <FH>  ) ) {   chomp($line);   … } close( FH ) ;
Open a Filehandler Opening a file for input. open (INFH, &quot; < &quot;, &quot;input.txt&quot;)  or die $!;
open (INFH, &quot; < input.txt&quot;)  or die $!;
open (INFH, &quot;input.txt&quot;)  or die $!; Opening a file for output. open (OUTFH, &quot; > &quot;, &quot;output.txt&quot;) or die $!;
open (OUTFH, &quot; > output.txt&quot;)  or die $!; Opening a file for appending open (LOGFH, &quot; >> &quot;, &quot;my.log&quot;)  or die $!;
open (LOGFH, &quot; >> my.log&quot;)  or die $!;
Open a Filehandler You can also use a scalar variable as filehandle: open( my $inFH , &quot;input.txt&quot;) or die $!;
a lexical scalar variable closes at the end of the block if it wasn't closed before It's possible to pipe from a command: open(my $net, &quot;netstat  | &quot;)    or die &quot;Can't netstat: $!&quot;; It's possible to pipe to a command: open(my $sendmail, &quot; |  sendmail -t&quot;)    or die &quot;Can't open sendmail: $!&quot;;
Write to a Filehandle We've already seen how to print to standard output using print(). However, print() can also take an optional first argument specifying which filehandle to print to: print STDERR  ('Are you there?');
print OUTFH  $record;
print { $FH }  $logMessage;
Note: There is no ‘,’ between filehandle and the text. Closing File Handles: close($inFH);
Read from a Filehandle You can read from an open filehandle using the  <>  operator or the  readline()  subroutine. Line by line: my $line  =  <$fh> ; my $line  =  readline($fh) ;

More Related Content

PPTX
PPT
Perl Basics with Examples
PPTX
Transcriptome analysis
PPTX
energy minimization
PPT
Chapter 5 data resource management
PPTX
What is an API?
PPTX
Python Seminar PPT
PDF
Data preprocessing using Machine Learning
Perl Basics with Examples
Transcriptome analysis
energy minimization
Chapter 5 data resource management
What is an API?
Python Seminar PPT
Data preprocessing using Machine Learning

What's hot (20)

PDF
Perl Scripting
PDF
Perl programming language
PDF
Functions and modules in python
PPT
Decision making and branching
PPT
PL/SQL Introduction and Concepts
PPTX
Datatype in c++ unit 3 -topic 2
PPT
1 - Introduction to PL/SQL
PPTX
Decision Making Statement in C ppt
PDF
Php array
PPTX
Looping Statements and Control Statements in Python
PDF
Datatypes in python
PPT
Introduction to Python
PPTX
Polymorphism in C++
PDF
Methods in Java
PPT
Operators in C Programming
PPT
Perl tutorial
PPT
Exception Handling in JAVA
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
PDF
Python programming : Inheritance and polymorphism
PPTX
Operators and expressions in c language
Perl Scripting
Perl programming language
Functions and modules in python
Decision making and branching
PL/SQL Introduction and Concepts
Datatype in c++ unit 3 -topic 2
1 - Introduction to PL/SQL
Decision Making Statement in C ppt
Php array
Looping Statements and Control Statements in Python
Datatypes in python
Introduction to Python
Polymorphism in C++
Methods in Java
Operators in C Programming
Perl tutorial
Exception Handling in JAVA
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python programming : Inheritance and polymorphism
Operators and expressions in c language
Ad

Viewers also liked (7)

ODP
Introducing Modern Perl
PPT
Efficient Shared Data in Perl
PDF
DBI Advanced Tutorial 2007
ODP
perl usage at database applications
ODP
Database Programming with Perl and DBIx::Class
PPT
Working with databases in Perl
Introducing Modern Perl
Efficient Shared Data in Perl
DBI Advanced Tutorial 2007
perl usage at database applications
Database Programming with Perl and DBIx::Class
Working with databases in Perl
Ad

Similar to Perl Introduction (20)

ODP
Beginning Perl
ODP
Introduction to Perl
ODP
Introduction to Perl - Day 1
ODP
Introduction to Perl - Day 2
PPT
LPW: Beginners Perl
PPT
Perl Presentation
ODP
Intermediate Perl
PPTX
PHP Powerpoint -- Teach PHP with this
PPT
P H P Part I, By Kian
PDF
Tutorial perl programming basic eng ver
PPT
Cleancode
PPT
CGI With Object Oriented Perl
PDF
perltut
PDF
perltut
ODP
Php Learning show
PPT
The JavaScript Programming Language
PPT
Javascript by Yahoo
PPT
The Java Script Programming Language
PPT
Javascript
Beginning Perl
Introduction to Perl
Introduction to Perl - Day 1
Introduction to Perl - Day 2
LPW: Beginners Perl
Perl Presentation
Intermediate Perl
PHP Powerpoint -- Teach PHP with this
P H P Part I, By Kian
Tutorial perl programming basic eng ver
Cleancode
CGI With Object Oriented Perl
perltut
perltut
Php Learning show
The JavaScript Programming Language
Javascript by Yahoo
The Java Script Programming Language
Javascript

More from Marcos Rebelo (6)

PDF
Store stream data on Data Lake
PDF
Coordinating external data importer services using AWS step functions
PDF
Mojolicious
ODP
ODP
Modern Perl
ODP
Perl In The Command Line
Store stream data on Data Lake
Coordinating external data importer services using AWS step functions
Mojolicious
Modern Perl
Perl In The Command Line

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Tartificialntelligence_presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
A Presentation on Artificial Intelligence
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
1. Introduction to Computer Programming.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Tartificialntelligence_presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
A comparative analysis of optical character recognition models for extracting...
A Presentation on Artificial Intelligence
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks

Perl Introduction

  • 1. Perl Welcome Pratical Extraction and Reporting Language Marcos Rebelo [email protected]
  • 2. Perl - Larry Wall Larry Wall, programmer, linguist, author, born March 10, 1949 in Duncan, British Columbia, Canada. Perl computer language creation in 1987.
  • 3. Quotes: The three chief virtues of a programmer are: Laziness, Impatience and Hubris
  • 4. There's More Than One Way to Do It
  • 5. What is Perl? Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including web development, system administration, network programming, code generation and more. It's intended to be practical rather than beautiful.
  • 6. Supports both procedural and OO programming.
  • 7. It has powerful built-in support for text processing
  • 8. It has one of the world's most impressive collections of third-party modules (http://www.cpan.org).
  • 9. Perl – Pros Quick development phases
  • 10. Open Source, and free licencing
  • 11. Excellent text handling and regular expressions
  • 13. Fast, for an interpreted language
  • 14. Code developed is generally cross plaform
  • 15. Very easy to write powerful programs in a few lines of code
  • 16. Perl – Cons Limited GUI support
  • 17. Can look complicated, initially, particularly if you're not familiar with regular expressions
  • 18. Consider using for Web applications
  • 20. Most command line applications
  • 23. Database access and manipulation
  • 24. Any application involving significant text processing
  • 25. Hello World in a file: print &quot;Hello, World!\n&quot; on the shell #> perl -e 'print &quot;Hello, World!\n&quot;' with perl 5.10 #> perl -E 'say &quot;Hello, World!&quot;'
  • 26. Basic syntax overview Perl statements end in a semi-colon. say(&quot;Hello, world!&quot;) ;
  • 27. You can use parentheses for subroutines arguments or omit them according to your personal taste. say &quot;Hello, world!&quot;;
  • 28. Comments start with a # and run to the end of the line. # This is a comment
  • 29. Basic syntax overview Double quotes or single quotes may be used around literal strings: say( &quot; Hello, world! &quot; ); say( ' Hello, world! ' );
  • 30. However, only double quotes interpolate variables and special characters such as newlines (\n): print(&quot;Hello, $name!\n &quot;); # interpolate $name print('Hello, $name!\n '); # prints $name!\n literally
  • 31. Basic syntax overview Whitespaces are irrelevant: print( &quot;Hello world&quot;);
  • 32. ... except inside quoted strings: print(&quot;Hello world&quot;); # this would print with a # line break in the middle.
  • 33. Use feature qw(say) Perl 5.10 as added a new feature use feature qw(say); say 'Hello, world!'; Is equivalent to: print 'Hello, world!', &quot;\n&quot;; We will see later that the correct expression is: { local $\ = &quot;\n&quot;; print 'Hello, world!' }
  • 34. Running Perl programs To run a Perl program from the Unix command line: #> perl progname.pl
  • 35. Alternatively, put this shebang in your script : #!/usr/bin/perl and run your executable script #> progname.pl
  • 36. -e : allows you to define the Perl code in the command line to be executed, -E to get 5.10 #> perl -e 'print(&quot;Hello, World!\n&quot;)'
  • 37. Perl variable types Scalars my $ animal = 'camel'; my $ answer = 42; my $ scalar_ref = \$scalar;
  • 38. Arrays my @ animals = ('camel', 'lama'); my @ numbers = (23, 42, 69); my @ mixed = ('camel', 42, 1.23);
  • 39. Associative Array / Hash Tables my % fruit_color = ( apple => 'red', banana => 'yellow' );
  • 40. Scalar Type Scalar values can be strings, integers, floating point numbers or references. The variable name starts with a $ . my $ animal = 'camel';
  • 41. my $ answer = 42;
  • 42. my $ scalar_ref = \$scalar; Perl will automatically convert between types as required. print('3' + 4.5); # prints 7.5
  • 43. Array Type An array represents a list of scalars. The variable name starts with a @ . my @ animals = ('camel', 'llama');
  • 44. my @ numbers = (23, 42, 69);
  • 45. my @ mixed = ('camel', 42, 1.23); Arrays are zero-indexed, negative index start by -1 at the end of the array. print( $ animals [0] ); # prints &quot;camel&quot;
  • 46. print( $ numbers [-1] ); # prints 69
  • 47. Array Slice To get multiple values from an array: @ numbers [0,1] ; # gives (23, 42);
  • 48. @ numbers [0..2] ; # gives (23, 42, 69);
  • 49. @ numbers [1..$#numbers] ; The single element we're getting from the array start with a $ , the sub-array start with @ .
  • 50. @ numbers [2, 1] = @ numbers [1, 2] ;
  • 52. Array Type The special variable $# array tells you the index of the last element of an array: print($mixed[ $# mixed]); # prints 1.23 You might be tempted to use $#array+1 to tell you how many items there are in an array. Using @array in a scalar context will give you the number of elements: if (@animals < 5) { ... }
  • 53. if ( scalar( @animals ) < 5) { ... }
  • 54. Associative Array Type Hash table A hash represents a set of key/value pairs: my % fruit_color = ( 'apple', 'red', 'banana', 'yellow' ); You can use whitespace and the => operator to lay them out more nicely: my % fruit_color = ( apple => 'red', ' banana ' => 'yellow' );
  • 55. Associative Array Type Hash table To get at hash elements: $ fruit_color{ ' apple ' }; # &quot;red&quot;
  • 56. $ fruit_color{banana}; # &quot;yellow&quot; You can get at lists of keys and values with keys() and values() . my @fruits = keys (%fruit_colors);
  • 57. my @colors = values (%fruit_colors); Hashes have no particular internal order, though you can sort the keys and loop through them.
  • 58. Hash Slice To get/set multiple values from an hash @ fruit_color { 'watermelon', 'orange' } = ('green', 'orange'); Removing repetition from an array my @array = (1,2,9,5,2,5); my %hash ; @hash{@array} = (); @array = keys(%hash) ; say &quot;@array&quot;; # 1 9 2 5
  • 59. Variable Reference Scalar references \$ scalar, \ '' , \ 123 Array references \@ array, [ 'camel', 42, 1.23 ] Hash references \% hash, { apple => 'red', banana => 'yellow' } Unref a variable $$ scalar_ref, @$ array_ref, %$ hash_ref
  • 60. Complex data types More complex data types can be constructed using references: my $cities = { IT => [ 'Milan', 'Rome', ... ] , PT => [ 'Lisbon', 'Oporto', ... ] , ... } ; You can access it throw: print($cities ->{ ' IT' } -> [ 1 ] ); print($cities ->{ ' IT' }[ 1 ] );
  • 61. my @citiesPT = @{ $cities->{PT} } ;
  • 62. Lexical Variables Throughout the previous slides the examples have used the syntax: my $var = 'value'; The my is actually not required, you could use: $var = 'value'; However, the above usage will create global variables throughout your program. my creates lexically scoped variables instead. The variables are scoped to the block in which they are defined.
  • 63. Lexical Variables Not having the variables scoped is usually a bad programming practice. my $a = 'foo'; if ($some_condition) { my $b = 'bar'; print($a); # prints &quot;foo&quot; print($b); # prints &quot;bar&quot; } print($a); # prints &quot;foo&quot; print($b); # prints nothing; $b has # fallen out of scope
  • 64. Lexical Variables It’s recommended to use my in combination with a use strict at the top of your programs. Using the pragma strict is highly recommended. use strict; my $first = &quot;first&quot;; # OK $second = &quot;second&quot;; # NOT OK Also highly recommended is the pragma warnings for having aditional warnings. You can enable or deseable this warnings.
  • 65. Magic variables There are some ‘magic’ variables. These special variables are used for all kinds of purposes. $_ : is the ‘default variable’.
  • 66. @ARGV : the command line arguments to your script.
  • 67. $ARGV : contains the name of the current file when reading from <> or readline() .
  • 68. @_ : the arguments passed to a subroutine.
  • 69. $a , $b : Special package variables when using sort() .
  • 70. $/ : The input record separator, newline by default.
  • 71. Most common operators Arithmetic + += addition
  • 72. - -= subtraction
  • 73. * *= multiplication
  • 74. / /= division Boolean logic && and
  • 75. || or
  • 76. ! not
  • 77. Most common operators Miscellaneous = assignment
  • 78. . string concatenation
  • 79. x string multiplication
  • 80. .. range operator (creates a list of numbers)
  • 82. Conditional constructs Perl has most of the usual conditional if COND BLOCK
  • 83. if COND BLOCK else BLOCK
  • 84. if COND BLOCK elsif COND BLOCK
  • 85. if COND BLOCK elsif COND BLOCK else BLOCK The COND shall be conditional statement surround by ( and ) , and BLOCK shall be one ore more statements surround by { and } . if ( is_valid( $value ) ) { … }
  • 86. Conditional constructs There's also a negated version of if (don't use it): unless ( is_valid( $value ) ) { ... }
  • 87. This is provided as a 'more readable' version of if ( not( is_valid( $value ) ) ) { ... }
  • 88. 0, '0', '', () and undef are all false in a boolean context. All other values are true.
  • 89. Conditional constructs Note that the braces are required in Perl, even if you've only got one line in the block. However, there is a clever way of making your one-line conditional blocks more English like: The traditional way if ($zippy) { print(&quot;Yow!&quot;); }
  • 90. The Perlish post-condition way print(&quot;Yow!&quot;) if $zippy; print(&quot;No cubes&quot;) unless $cubes;
  • 91. while looping constructs Perl has most of the usual loop constructs. While Loop: while ( is_valid( $value ) ) { … }
  • 92. There's also a negated version (don't use it): until ( is_valid( $value ) ) { … }
  • 93. You can also use while in a post-condition: print(&quot;xpto\n&quot;) while condition;
  • 94. Going throw a hash: while (my ($key,$value) = each (%ENV)){ print &quot;$key=$value\n&quot;; }
  • 95. for looping constructs for (my $i=0; $i <= $max; $i++) { ... } The C style for loop is rarely needed in Perl since Perl provides the more friendly foreach loop. foreach my $i (0 .. $max) { ... }
  • 96. foreach looping constructs Passing all elements of an array, foreach is an alias to for . foreach my $var (@array) { ... }
  • 97. for my $value (values(%hash)) { ... } By default the value goes on $_ foreach (@array) { print &quot; $_ \n&quot; } Changing the variable, changes the value inside the array. $var is an alias. for my $var (@array) { $var += $var }
  • 98. Jump on loops LINE: while ( defined(my $line = <>) ) { next LINE if not is_valid( $line ); #... }
  • 99. Jump on loops: last LABEL : immediately exits the loop in question
  • 100. next LABEL : starts the next iteration of the loop
  • 101. redo LABEL : restarts the loop block without evaluating the conditional again If the LABEL is omitted, the command refers to the innermost enclosing loop.
  • 102. Inverting the cycle With a single command print foreach (0..9) With multiply commands do { ... } while ($true)
  • 103. Warning: last , next , and redo don't work in this case.
  • 104. Exercises 1 - Scalars 1) Implement the ‘Guess the lucky number’. The program shall chose a random number between 0 and 100 and ask the user for a guess. Notice the user if the guess is bigger, smaller or equal to the random number. If the guess is correct the program shall leave otherwise re-ask for a number.
  • 105. Exercises 1 - Array 2) Create an array that contains the names of 5 students of this class. 2a) Print the array. 2b) Create a new Array shifting the elements left by one positions (element 1 goes to 0, …) and setting the first element in the last position. Print the array. 2c) Ask a user to input a number. Print the name with that index.
  • 106. Exercises 1 - Hash 3) Homer Family my %relatives = ( Lisa => 'daughter', Bart => 'son', Maggie => 'daughter', Marge => 'mother', Homer => 'father', Santa => 'dog'); 3a) Print all the characters name’s. 3b) Demand for a name and print is position. 3c) Print all the characters position’s, no repetitions. 3d) Demand for a position and print the name.
  • 108. Subroutines The Perl model for subroutine call and return values is simple: all subroutine are passed as parameters one single flat list of scalars (goes to @_ )
  • 109. all subroutines likewise return to their caller one scalar or a list of scalars.
  • 110. lists or hashes in these lists will collapse, losing their identities - but you may always pass a reference.
  • 111. The subroutine name start with a & , for simplification can be omitted.
  • 112. Subroutine - example sub max { my $mval = shift(@_) ; # my ($mval, @rest) = @_ ; # big copy foreach my $foo ( @_ ) { if ( $mval < $foo ) { $mval = $foo; } } return $mval; }
  • 113. my $my_max = max( 1, 9, 3, 7 ) ; print $my_max; # prints 9
  • 114. Subroutine input and output The parameters are passed to the subroutine in the array @_ , changing the values of the array will change the values outside the subroutine call. sub double { $_[0] *= 2 ; } my $b = 5; double($b); print($b); # prints 10 The last statement value is returned by default. print(double($b)); # prints 20
  • 115. Subroutine input and output Using @_ is dangerouse and shall be carefully considered. It's always possible to do a copy. sub double { my ($a) = @_; $a *= 2; return $a; } my $b = 5; print( double( $b ) ); # prints 10 print($b); # prints 5
  • 116. Persistent Private Variables Just because a lexical variable is statically scoped to its enclosing block, eval, or file. { my $secretVal = 0; sub gimmeAnother { return ++$secretVal; } } print gimmeAnother; # OK ++$secretVal; # NOT OK
  • 117. state variables From perl 5.10 you may use static variables. use feature 'state'; sub gimmeAnother { state $secretVal = 0; return ++$secretVal; } print gimmeAnother; # OK Some features in perl 5.10 have to be activated to avoid colisions with old code. Activating all them: use feature ':5.10'; use v5.10; # use perl 5.10
  • 118. Named Parameters We can implement named parameters using a hash. This provides an elegant way to pass in parameters without having to define them formally. sub login { my %param = @_; ... } login( user=>'User', pass=>'Pass' );
  • 119. Named Parameters We may pass the hash directly. sub login { my ($param) = @_; ... } login({ user=>'User', pass=>'Pass' });
  • 120. Named Parameters We can easily give default values by checking the hash. sub login { my ($param) = @_; $param->{user} = $DEFAULT_USER if not exists $param->{user}; $param -> {pass} = $DEFAULT_PASS if not exists $param->{pass}; $param->{host} = $DEFAULT_HOST if not exists $param->{host}; ... }
  • 121. Named Parameters We can easily give default values by checking the hash. sub login { my $param = { 'user' => $DEFAULT_USER, 'pass' => $DEFAULT_PASS, 'host' => $DEFAULT_HOST, %{shift(@_)} }; ... }
  • 122. Named Parameters We can also write the subroutine so that it accepts both named parameters and a simple list. sub login { my $param; if ( ref($_[0]) eq 'HASH' ) { $param = $_[0]; } else { @{$param}{qw(user pass host)}=@_; } ... } login('Login', 'Pass'); login({user => 'Login', pass => 'Pass'});
  • 123. Exercises 2 1) Create a new subroutine that calculates the Fibonacci series. Using this subroutine, do a program that receives multiple numbers as argument and prints the Fibonacci value. F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) 1a) with presistent variable 1b) with state variable
  • 124. IO
  • 125. Read a File open( FH, '<', 'path/to/file' ) or die &quot;can't open file: $! &quot;; while ( defined( my $line = <FH> ) ) { chomp($line); … } close( FH ) ;
  • 126. Open a Filehandler Opening a file for input. open (INFH, &quot; < &quot;, &quot;input.txt&quot;) or die $!;
  • 127. open (INFH, &quot; < input.txt&quot;) or die $!;
  • 128. open (INFH, &quot;input.txt&quot;) or die $!; Opening a file for output. open (OUTFH, &quot; > &quot;, &quot;output.txt&quot;) or die $!;
  • 129. open (OUTFH, &quot; > output.txt&quot;) or die $!; Opening a file for appending open (LOGFH, &quot; >> &quot;, &quot;my.log&quot;) or die $!;
  • 130. open (LOGFH, &quot; >> my.log&quot;) or die $!;
  • 131. Open a Filehandler You can also use a scalar variable as filehandle: open( my $inFH , &quot;input.txt&quot;) or die $!;
  • 132. a lexical scalar variable closes at the end of the block if it wasn't closed before It's possible to pipe from a command: open(my $net, &quot;netstat | &quot;) or die &quot;Can't netstat: $!&quot;; It's possible to pipe to a command: open(my $sendmail, &quot; | sendmail -t&quot;) or die &quot;Can't open sendmail: $!&quot;;
  • 133. Write to a Filehandle We've already seen how to print to standard output using print(). However, print() can also take an optional first argument specifying which filehandle to print to: print STDERR ('Are you there?');
  • 134. print OUTFH $record;
  • 135. print { $FH } $logMessage;
  • 136. Note: There is no ‘,’ between filehandle and the text. Closing File Handles: close($inFH);
  • 137. Read from a Filehandle You can read from an open filehandle using the <> operator or the readline() subroutine. Line by line: my $line = <$fh> ; my $line = readline($fh) ;
  • 138. Slurp: my @lines = <$fh> ; my @lines = readline($fh) ;
  • 139. Read from a Filehandle Slurping a file can be useful but it may be a memory hog (usually if you are programming in Perl that is not a problem, the actual computers already have a lot of memory). Most text file processing can be done a line at a time with Perl's looping constructs. while ( defined(my $line = <$fh>) ) { print &quot;Just read: $line&quot;; }
  • 140. foreach my $line (<$fh>) { # slurps print &quot;Just read: $line&quot;; }
  • 141. Input Record Separator $/ is the input record separator (newline by default). You may set it to a multi-character string to match a multi-character terminator or to undef to read through the end of file. open(my $fh, '<', $myfile) or die $!; my $txt = do{ local $/ = undef ; <$fh>}; close($fh);
  • 142. my $txt = do { local (@ARGV, $/) = ($myfile); readline(); };
  • 143. Read from @ARGV file list The magic filehandle * ARGV can be used to eseally process the input. Perl actually does an implicit open on each file in @ARGV (process parameters). Usually <ARGV> is simplyfied to <> . In this case: while (my $line = <> ) { # use $line }
  • 144. while ( <> ) { # use $_ } If @ARGV is empty when the loop first begins, Perl pretends you've opened STDIN .
  • 145. $ARGV has the open file, or '-' if reading STDIN .
  • 146. *DATA Filehandle This special filehandle refers to anything following either the __END__ token or the __DATA__ token in the current file. The __END__ token always opens the main::DATA filehandle, and so is used in the main program.
  • 147. The __DATA__ token opens the DATA handle in whichever package is in effect at the time, so different modules can each have their own DATA filehandle, since they (presumably) have different package names.
  • 148. Exercises 3 Create a program to print a file to the stdout. The program shall receive two flags (--file, --line) followed by a list of file names: --file : printing the file name before the line.
  • 149. --line : printing the line number before the line.
  • 150. Create a version with open/close and one without. The program shall be called like: perl -w printFile.pl (--line|--file)* files Note: $. is the line number for the last filehandle accessed.
  • 151. Exercises 3 Create a program that has a mapping of integer to integer in the DATA section, 2 integer in each line separated by a space. From the ARGV/STDIN will get a TSV file. The program shall print the second file adding a second column with: If the first column value is a key in the DATA section, set the value in the DATA section .
  • 152. Otherwise, set 'MAPPING NOT FOUND'. To split a string use: split(/\t/, $str)
  • 154. Regular Expressions The simplest regex is simply a word. &quot;Hello World&quot; =~ m/ World /
  • 155. Or simply: &quot;Hello World&quot; =~ / World / Expressions like this are useful in conditionals: print &quot;Matches&quot; if $str =~ / World / ; The sense of the match can be reversed by using !~ operator: print &quot;No match&quot; if $str !~ / Word / ;
  • 156. Regular Expressions Regexps are treated mostly as double quoted strings, so variable substitution works: $foo = 'house'; 'cathouse' =~ /cat $foo /; # matches 'housecat' =~ / ${foo} cat/; # matches qr// compiles the Regular Expression. foreach my $regexp (@regexps) { my $comp = qr/ $regexp / ; foreach my $str (@strs) { print '$str\n' if $str =~ / $comp /; } }
  • 157. To specify where the regex should match, we would use the anchor metacharacters ^ and $ . The ^ means match at the beginning of the string. print 'Starts with Hello' if /^Hello/;
  • 158. The $ means match at the end of the string or before a newline at the end of the string. print 'Ends with World!' if /World!$/;
  • 159. Character Classes A character class allows a set of possible characters, rather than just a single character. Character classes are denoted by brackets [] . / [bcr] at/ - matches 'bat', 'cat', or 'rat‘
  • 160. / [yY][eE][sS] / - match 'yes' case-insensitively.
  • 161. /yes/ i - also match 'yes' in a case-insensitive way. The special character '-' acts as a range operator, so [0123456789] become [0-9] : /item [0-9] / - matches 'item0' or ... or 'item9‘
  • 162. / [0-9a-f] / i - matches a hexadecimal digit
  • 163. Character Classes The special character ^ in the first position of a character class denotes a negated character class, which matches any character but those in the brackets. Both [...] and [^...] must match one character, or the match fails. Then / [^a] at/ - doesn't match 'aat' or 'at', but matches all other 'bat', 'cat', ....
  • 164. / [^0-9] / - matches a non-numeric character
  • 165. / [a^] at/ - matches 'aat' or '^at'; here '^' is ordinary
  • 166. Character Classes Character classes also have ordinary and special characters, but the sets of ordinary and special characters inside a character class are different than those outside a character class. The special characters for a character class are -]\^$ and are matched using an escape: / [\]c] def/ - matches ‘]def’ or ‘cdef’
  • 167. $x = 'bcr'; / [$x] at/ - matches ‘bat’, ‘cat’, or ‘rat’ / [\$x] at/ - matches ‘$at’ or ‘xat’ and not ‘bat’, … / [\\$x] at/ - matches ‘\at’, ‘bat’, ‘cat’, or ‘rat’
  • 168. Character Classes Perl has several abbreviations for common character classes: \d is a digit character [0-9]
  • 169. \D is a non-digit character [^\d]
  • 170. \s is a whitespace character [\ \t\r\n\f]
  • 171. \S is non-whitespace character [^\s]
  • 172. \w is a word character [0-9a-zA-Z_]
  • 173. \W is a non-word character [^\w]
  • 174. The period '.' matches any character but [^\n]
  • 175. Character Classes The \d\s\w\D\S\W abbreviations can be used both inside and outside of character classes. Here are some in use: / \d\d : \d\d : \d\d / - matches a hh:mm:ss time format
  • 176. /[ \d\s ]/ - matches any digit or whitespace character
  • 177. / .. rt/ - matches any two chars, followed by 'rt'
  • 178. /end \. / - matches 'end.'
  • 179. /end [.] / - same thing, matches 'end.‘
  • 180. Exercises 4 Create a Regular Expression that match with a string that contains ‘a’ or ‘b’ followed by any 2 characters followed by a digit. The strings ‘(adc34)’ and ‘rdb850’ matches, but ‘alfa’ doesn’t match.
  • 181. Create a Regular Expression that match a 5 digit integer in octal format.
  • 182. Create a program that receives one regexp as the first parameter and a file list and prints the lines matching the regexp.
  • 183. Quantifiers Quantifiers can be used to specify how many of the previous thing you want to match on, where “thing” means either a literal character, one of the metacharacters listed above, or a group of characters or metacharacters in parentheses. * - zero or more
  • 184. + - one or more
  • 185. ? - zero or one
  • 186. {3} - matches exactly 3
  • 187. {3,6} - matches between 3 and 6
  • 188. {3,} - matches 3 or more
  • 189. Quantifiers If you want it to match the minimum number of times possible, follow the quantifier with a ?. 'a,b,c,d' =~ /,( .+ ),/; # match 'b,c'
  • 190. 'a,b,c,d' =~ /,( .+? ),/; # match 'b' Avoid unnecesary backtracing : '[1234567890]' =~ /\[ \d++ \] /
  • 191. Matching this or that We can match different character strings with the alternation metacharacter '|'. Perl will try to match the regex at the earliest possible point in the string. Even though dog is the first alternative in the second regex, cat is able to match earlier in the string. 'cats or dogs' =~ /cat | dog/; # matches 'cat' 'cats or dogs' =~ /dog | cat/; # matches 'cat'
  • 192. At a given character position, the first alternative that allows the regex match to succeed will be the one that matches. 'cats' =~ /c | ca | cat | cats/; # matches 'c' 'cats' =~ /cats | cat | ca | c/; # matches 'cats'
  • 193. Grouping things and hierarchical matching The grouping metacharacters () allow a part of a regex to be treated as a single unit. Parts of a regex are grouped by enclosing them in parentheses. / ( a|b ) b/ - matches 'ab' or 'bb'.
  • 194. / ( ^a|b ) c/ - matches 'ac' at start of string or 'bc' anywhere.
  • 195. /house ( cat| ) / - matches either 'housecat' or 'house'.
  • 196. /house ( cat ( s| ) | ) / - matches either 'housecats' or 'housecat' or 'house'.
  • 197. Extracting matches The grouping metacharacters () also allow the extraction of the parts of a string that matched. For each grouping, the part that matched inside goes into the special variables $1 , $2 , etc. # extract time in hh:mm:ss format $time =~ /(\d\d):(\d\d):(\d\d)/; my ($hour, $min, $sec) = ( $1 , $2 , $3 ); In list context, a match /regex/ with groupings will return the list of matched values ($1, $2, ...) . my ($hour, $min, $sec) = ($time =~ /(\d\d):(\d\d):(\d\d)/);
  • 198. Extracting matches To get a list of matches we can use: my @listOfNumber = ($txt =~ /(\d+)/g); If the groupings in a regex are nested, $1 gets the group with the leftmost opening parenthesis, $2 the next opening parenthesis, etc. For example, here is a complex regex and the matching variables indicated below it: / ( ab ( cd|ef ) (( gi)|j))/; 1 2 34
  • 199. Named capture Identical to normal capturing parentheses () but %+ or %- may be used after a successful match to refer to a named buffer. 'Michael Jackson' =~ /( ?<NAME> \w+)\s+( ?<NAME> \w+)/
  • 200. %+ is ('NAME' => 'Michael')
  • 201. %- is ('NAME' => ['Michael','Jackson'])
  • 202. $1 is 'Michael'
  • 203. $2 is 'Jackson'
  • 204. Search and replace Search and replace is performed using s/regex/replacement/. The replacement replaces in the string whatever is matched with the regex. $x = &quot;'Good cat!'&quot;; $x =~ s/ cat / dog / ; # &quot;'Good dog!'&quot; $x =~ s/ '(.*)' / $1 / ; # &quot;Good dog!&quot; With the global modifier, s///g will search and replace all occurrences of the regex in the string: $x = $y = '4 by 4'; $x =~ s/4/four/; # 'four by 4' $y =~ s/4/four/ g ; # 'four by four'
  • 205. The split operator split(/regex/, string) - splits the string into a list of substrings and returns that list. The regex determines the character sequence that string is split with respect to. split(/\s+/, 'Calvin and Hobbes'); # ('Calvin', 'and', 'Hobbes') If the empty regex // is used, the string is split into individual characters. split(//, 'abc'); # ('a', 'b', 'c')
  • 206. The split operator If the regex has groupings, then the list produced contains the matched substrings from the groupings as well: split(m!(/)!, '/usr/bin'); # ('', '/', 'usr', '/', 'bin') Since the first character of string matched the regex, split prepended an empty initial element to the list.
  • 207. Magic Variables We have already seen $1 , $2 , ... There is also: $` - The string preceding whatever was matched.
  • 208. $& - The string matched.
  • 209. $' - The string following whatever was matched. These variables are read-only and dynamically scoped to the current BLOCK. 'abcdef' =~ /cd/; print(&quot; $` : $& : $' \n&quot;); # prints ab:cd:ef Make the Regexp slow 'abcdef'=~/^(.*?)(cd)(.*)$/; # $1, $2, $3
  • 210. Switch use qw(switch say); given ($foo) { when (undef) {say '$foo is undefined'} when ('foo') {say '$foo is the str &quot;foo&quot;'} when (/Milan/) {say '$foo matches /Milan/'} when ([1,3,5,7,9]) { say '$foo is an odd digit'; continue ; # Fall through } when ($_ < 100) {say '$foo less than 100'} when (\&check) {say 'check($foo) is true'} default {die 'what shall I do with $foo?'} }
  • 211. Smart Matching given(EXPR) will assign the value of EXPR to $_ within the lexical scope of the block.
  • 212. when($foo) is equivalent to when($_ ~~ $foo)
  • 213. Smart Matching $a $b Matching Code Code Code $a == $b # not empty prototype if any Any Code $b->($a) # not empty prototype if any Hash Hash [sort keys %$a]~~[sort keys %$b] Hash Array grep {exists $a->{$_}} @$b Hash Regex grep /$b/, keys %$a Hash Any exists $a->{$b} Array Array arrays are identical, value by value Array Regex grep /$b/, @$a Array Num grep $_ == $b, @$a Array Any grep $_ eq $b, @$a Any undef !defined $a Any Regex $a =~ /$b/ Code() Code() $a->() eq $b->() Any Code() $b->() # ignoring $a Num numish $a == $b Any Str $a eq $b Any Num $a == $b Any Any $a eq $b
  • 214. Exercises 5 Create a program that printout the title inside a html file.
  • 215. Create a program that printout all the lines in a file substituting the numbers by #.
  • 216. In the printfile.pl add one adicional flag like: --regexp=REGEXP - the program shall only print the lines that match with the REGEXP.
  • 217. Exercises 5 For each of the strings , say which of the patterns it matches. Where there is a match, what would be the values of $MATCH , $1 , $2 , etc.? 'The Beatles (White Album) - Ob-La-Di, Ob-La-Da' 'Tel: 212945900' '(c) (.+)\s*\1'
  • 218. RegExp /(\(.*\))/ and /(\(.*?\))/ /\d{4,}/ /(\w\w)-(\w\w)-(\w\w)/ /\W+/
  • 220. Useful scalar subroutines lc(EXPR), lcfirst(EXPR), uc(EXPR), ucfirst(EXPR) - lower case, lower case first, upper case and uppercase first.
  • 221. length(EXPR) - Returns the length in characters of the value of EXPR.
  • 222. sprintf(FORMAT, LIST) - Returns a string formatted by the usual printf conventions of C.
  • 223. abs(EXPR), cos(EXPR), int(EXPR), log(EXPR), sin(EXPR), sqrt(EXPR) - normal numeric subroutines.
  • 224. chop/chomp chop(VARIABLE), chop(LIST) : Chops off the last character of a string and returns the character chopped.
  • 225. chomp(VARIABLE), chomp(LIST) : Removes any trailing string that corresponds to the current value of $/ . chomp( $line ); chomp( $line = <> ); chomp( @lines = <> );
  • 226. substr substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET
  • 227. Extracts a substring out of EXPR and returns it my $var = 'Good dog'; say substr($var, 5); # 'dog' substr($var, 5) = 'cat'; # 'Good cat' substr($var, 5, 5, 'cow'); # 'Good cow'
  • 228. Useful scalar subroutines oct(EXPR) - Interprets EXPR as an octal string and returns the corresponding value. If EXPR happens to start off with: 0x - interprets it as a hex string.
  • 229. 0b - interpreted it as a binary string. defined(EXPR) - Returns a boolean value telling whether EXPR has a value other than the undefined value undef.
  • 230. Note: If EXPR is omitted, uses $_ .
  • 231. Useful list subroutines pop(ARRAY), push(ARRAY, LIST) - Pops/Pushes a value from/to the end of the ARRAY .
  • 232. shift(ARRAY), unshift(ARRAY, LIST) - Pops/Pushes a value from/to the start of the ARRAY .
  • 233. Note: in the pop and shift if ARRAY is omitted, pops the @ARGV array in the main program, and the @_ array in subroutines. Avoid to use it.
  • 234. join join(EXPR,LIST) - Joins the separate strings of LIST separated by the value of EXPR . join (':', (1..5)) eq '1:2:3:4:5'
  • 235. reverse reverse(LIST) In list context, returns a list value consisting of the elements of LIST in the opposite order. print reverse (1,'ab'); # prints &quot;ab1&quot; In scalar context, concatenates the elements of LIST and returns a string value with all characters in the opposite order. print reverse (1,&quot;ab&quot;).&quot;&quot;; # prints &quot;ba1&quot;
  • 236. print scalar( reverse (1,&quot;ab&quot;)); Inverting the keys/values in a hash. %by_name = reverse (%by_address);
  • 237. map map BLOCK LIST or map(EXPR,LIST) - Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation. @double = map {$_*2} @nums;
  • 238. my @double; foreach (@nums) { push(@double, $_*2) } Note: $_ is an alias to the list value.
  • 239. grep grep BLOCK LIST or grep(EXPR,LIST) – Filters the elements in LIST using the BLOCK or EXPR for each element of LIST (locally setting $_ to each element). In scalar context, returns the number of filtered elements. my @even = grep {$_ % 2 == 0} (1..100);
  • 240. my @even; foreach (1..100) { push(@even, $_) if $_ % 2 == 0; } Note: $_ is an alias to the list value.
  • 241. sort sort(LIST) - In list context, this sorts the LIST and returns the sorted list value. By default compares the elements as strings. sort (10,9,20) #10, 20, 9
  • 242. Providing a closure, the elements come in $a and $b . sort {$a <=> $b} (10,9,20) # 9, 10, 20
  • 243. Schwartzian transform @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, foo($_) ] } @unsorted;
  • 244. each each(HASH) - When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, so that you can iterate over it. When called in scalar context, returns only the key for the next element in the hash. while (my ($key,$val) = each (%ENV)) { print(&quot;$key=$val\n&quot;); }
  • 245. exists exists EXPR - Given an expression that specifies a hash or an array, returns true if the specified element has ever been initialized, even if the corresponding value is undefined. my @a = (1, undef); $a[3] = undef; exists ($a[1]) # true exists ($a[3]) # true exists ($a[2]) # false
  • 246. my %a = ('a' => 1); exists ($a{'a'}) # true exists ($a{'b'}) # false
  • 247. delete delete(EXPR) - Given an expression that specifies a hash element or array element deletes the specified element(s) from the hash or array. my @array = (a => 1, b => 2, c => 3); delete($array[2]); # ('a',1,2,'c',3);
  • 248. my %hash = (a => 1, b => 2, c => 3); delete($hash{b}); # (a => 1, c => 3); In the case of an array, if the array elements happen to be at the end, the size of the array will shrink to the highest element that tests true for exists().
  • 249. eval / die eval EXPR : compiles and evaluates the expression and catch's the exception.
  • 250. eval BLOCK : evaluates the expression and catch's the exception.
  • 251. die EXPR : if out of eval prints the error and exits with the value of $!, otherwise sets the value in $@ and exits eval with undef.
  • 252. eval { $answer = $a / $b; }; warn $@ if $@;
  • 253. Exercises 6 Create a subroutine that receives an array and returns a hash reference with the elements as keys and the inversed string as values.
  • 254. Create a subroutine that receives an array of scalars and return a new one just with the strings with length smaller than 20.
  • 255. Create a program that reads a file and prints all the strings capitalized. This means: First character in upper-case.
  • 256. The remaining in lower-case.
  • 258. Package The idea is to protected each package variables. package Dog; our $c = 1; my $d = 1; sub inc {$c++; $d++} package main; our $c = 0; my $d = 0; sub inc {$c++; $d++} print(&quot;$d-$c-$Dog::d-$Dog::c\n&quot;);# &quot;0-0--1&quot; Dog::inc(); print(&quot;$d-$c-$Dog::d-$Dog::c\n&quot;);# &quot;0-0--2&quot; inc(); print(&quot;$d-$c-$Dog::d-$Dog::c\n&quot;);# &quot;1-1--2&quot;
  • 259. use Modules The module is just a package defined in a file of the same name with .pm on the end.
  • 260. Perl modules are typically included by saying: use MODULE LIST;
  • 261. BEGIN { require MODULE; MODULE->import( LIST ); } use Data::Dumper; # exports Dumper print(Dumper({1 => 3}));
  • 262. use Modules Any double colons in the module name are translated into your system's directory separator, so if your module is named My::Mod , Perl might look for it as My/Mod.pm .
  • 263. Perl will search for modules in each of the directories listed in the @INC array. If it is need new directories you can use the lib pragma. use lib '/path/to/modules';
  • 264. Write Modules To start a traditional module create a file called Some/Module.pm and start with this template: package Some::Module; # package name use strict; use warnings; # use always use Exporter; our @ISA = qw(Exporter); our $VERSION = 05.22; our @EXPORT = qw($var1 &func1); our @EXPORT_OK = qw($var2 &func2); our ( $var1, $var2 ) = ( 1, 2 ); sub func1() {print(&quot;func1\n&quot;);} sub func2() {print(&quot;func2\n&quot;);} 1; # has to finnish with a true value
  • 265. Write Modules use Exporter; our @ISA = qw(Exporter); Import Exporter module. Derive the methods of Exporter module. Each element of the @ISA array is just the name of another package, the packages are searched for missing methods in the order that they occur. use base qw(Exporter); # is similar
  • 266. our $VERSION = 05.22; Sets the version number. Importing like: use Some::Module 6.15;
  • 267. Write Modules our @EXPORT = qw($var1 &func1); Lists of symbols that are going to be exported by default (avoid to use it).
  • 268. our @EXPORT_OK = qw($var2 &func2); Lists of symbols that are going to be exported by request (better practice).
  • 269. our ( $var1, $var2 ) = ( 1, 2 ); sub func1() {print(&quot;func1\n&quot;);} sub func2() {print(&quot;func2\n&quot;);} Definition of the symbols.
  • 270. Write Modules This is ugly but can be used to call subroutine as well. my $name = &quot;Some::Module&quot;;
  • 271. The package name pass as the first parameter of the subroutine. Some::Module->func(); $name->func();
  • 272. This will not pass the module name inside the subroutine Some::Module::func(); &{&quot;${name}::func&quot;}();
  • 273. Perl Objects There are three very simple definitions. An object is simply a reference that happens to know which class it belongs to.
  • 274. A class is simply a package that happens to provide methods to deal with object references.
  • 275. A method is simply a subroutine that expects an object reference (or a package name, for class methods) as the first argument.
  • 276. Object constructor Perl doesn't provide any special syntax for constructors. A constructor is merely a subroutine that returns a reference to something ‘blessed’ into a class. Usually the same class the subroutine is defined on. package Animal; sub new { bless({}) } That word new isn't special. package Animal; sub Animal { bless({}) }
  • 277. Objects Inheritance If you care about inheritance then you want to use the two-arg form of bless so that your constructors may be inherited. package Animal; sub new {return bless({}, shift @_ );} package Dog; use base qw(Animal); # use 'Animal'; # +- true # push @ISA, 'Animal'; This would be called like: my $dog = Dog->new();
  • 278. get/put method The get/put method in perl sub property { my ($self, $value) = @_; $self->{property} = $value if @_>1; return $self->{property}; } Use like: $obj->property(1); print $obj->property();
  • 279. Method overwriting The most common way to to call a method from an object is: print(&quot;Dog: &quot;.$dog->fly().&quot;\n&quot;); print(&quot;Bat: &quot;.$bat->fly().&quot;\n&quot;); Perl will look to the scalar reference and see the package name of the blessed reference. Method Implementation package Animal; sub fly { return 0; }
  • 280. package Bat; use base qw(Animal); sub fly { return 1; }
  • 281. Method overwriting If you need to, you can force Perl to start looking in some other package $bat->Insect::fly(); # dangerous As a special case of the above, you may use the SUPER pseudo-class to tell Perl to start looking for the method in the packages named in the current class's @ISA list: $bat->SUPER::fly();
  • 282. Object Destroy The object is automatically destroyed when the last reference to an object goes away. If you want to capture control just before the object is freed, you may define a DESTROY method in your class. It will automatically be called and you can do any extra cleanup you need to do. Perl passes a reference to the object under destruction as the first argument.
  • 283. Exercises 7 Create a module with the subroutines min , max and in .
  • 284. Create a set of classes to represent the animal fly capabilities. Shall have two methods fly and name(get/put), the constructor receives the animal name. Consider the following rules: dog is a animal animal doesn’t fly bird is a animal bird flies penguin is a bird penguin doesn’t fly
  • 285. Create a program to test them.
  • 287. pragma The usual ones strict - Restricts unsafe constructs
  • 288. warnings - Control optional warnings
  • 289. lib - Manipulate @INC at compile time
  • 290. base - Establish an ISA relationship with base class
  • 291. constant - Declare constants (consider Readonly)
  • 292. subs - Predeclare sub names
  • 293. integer - Forces integer math Perl 5.10 gives new features, see perlpragma
  • 294. Usual guilties Data::Dumper - stringified perl data structures
  • 295. Carp - better die() and warn()
  • 296. Cwd - pathname of current working directory
  • 297. Exporter - Implements default import method for modules
  • 298. POSIX
  • 299. IPC::Open3 - open a process for reading, writing, and error handling
  • 300. Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
  • 301. World-Wide Web LWP - The World-Wide Web library
  • 302. LWP::UserAgent - Web user agent class
  • 303. HTTP::Request - HTTP style request message
  • 304. HTTP::Response - HTTP style response message
  • 305. LWP::Simple - Simple procedural interface to LWP
  • 306. LWP::Simple::Post - Single-method POST requests
  • 307. HTTP::Async - process multiple HTTP requests in parallel without blocking.
  • 308. World-Wide Web WWW::Mechanize - Handy web browsing in a Perl object
  • 309. Net::SLP - accessing the Service Location Protocol
  • 310. Net::POP3 - Post Office Protocol 3 Client
  • 311. Net::SMTP - Simple Mail Transfer Protocol Client
  • 312. MIME::Lite - low-calorie MIME generator
  • 313. JSON - JSON encoder/decoder
  • 314. JSON::XS – fast JSON serialising/deserialising
  • 315. Apache/mod_perl packages Apache2::Const - Perl Interface for Apache Constants
  • 316. Apache2::RequestIO - Perl API for Apache request record IO
  • 317. Apache2::RequestRec - Perl API for Apache request record accessors
  • 318. Apache2::RequestUtil - Perl API for Apache request record utils
  • 319. CGI - Handle Common Gateway Interface requests and responses
  • 320. Security Data::UUID - Generating GUIDs/UUIDs
  • 321. MIME::Base64 - Encoding and decoding of base64 strings
  • 322. Digest::SHA - Perl extension for SHA-1/224/256/384/512
  • 323. Digest::MD5 - Perl interface to the MD5 Algorithm
  • 324. Crypt::DES - Perl DES encryption module
  • 325. Net::SSH - Perl extension for secure shell
  • 326. Test Test - provides a simple framework for writing test scripts
  • 327. Test::More - yet another framework for writing test scripts
  • 328. Test::Exception - Test exception based code
  • 329. Test::Output - Utilities to test STDOUT and STDERR messages.
  • 330. Other DBI - Database independent interface for Perl
  • 331. DBD::* - Perl DBI Database Driver DBD::SQLite, DBD::CSV, DBD::Google, ...
  • 332. Template – Template toolkit
  • 333. HTML::Template - Perl module to use HTML Templates
  • 335. DBI use DBI ; my $dsn = &quot;DBI:mysql:database=$database;&quot; . &quot;host=$hostname;port=$port&quot;; my $dbh = DBI->connect ($dsn, $user, $password); my $sth = $dbh-> prepare ( &quot;SELECT * FROM person WHERE name = ?&quot;) or die $dbh->errstr ; $sth-> execute ('oleber') or die $dbh->errstr ; while (my $ref = $sth-> fetchrow_hashref ()) { print Dumper $ref; } $sth-> finish (); $dbh-> disconnect ;
  • 336. AUTOLOAD a method When the method isn't found, the AUTOLOAD will be called. sub AUTOLOAD { my ($self, @params) = @_; my $name = $AUTOLOAD ; $name =~ s/.*://; # strip name die &quot;Can't access '$name' field&quot; if not exists $self->{_p}->{$name}; ( $self->{$name} ) = @params if @params; return $self->{$name}; }
  • 337. tie variable package SConst; sub TIESCALAR { my ($pkg, $val) = @_; bless \$val, $pkg; return \$val; } sub FETCH { # read return ${shift()}; } sub STORE { # write die &quot;No way&quot;; } 1; use SConst; my $var; tie $var, 'SConst', 5; print &quot;$var\n&quot;; $var = 6; # dies
  • 338. Q/A

Editor's Notes

  • #4: Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, code generation and more. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are: it&apos;s easy to use. Supports both procedural and object-oriented (OO) programming. It has powerful built-in support for text processing. it has one of the world&apos;s most impressive collections of third-party modules (http://www.cpan.org).
  • #20: $ perldoc -f keys $ perldoc -f values
  • #23: $ perldoc perlref Array reference: $refArray1 = @array; $refArray2 = [ &amp;quot;Milan&amp;quot;, &amp;quot;Rome&amp;quot;]; Hash reference $refHash1 = \%hash; $refHash2 = { apple =&gt; &amp;quot;red&amp;quot;, banana =&gt; &amp;quot;yellow&amp;quot; };
  • #24: $ perldoc -f my Check the local and our manpages as well. $ perldoc -f our $ perldoc -f local
  • #26: $ perldoc strict $ perldoc warnings Good practice Start your script files with: use strict; use warnings
  • #27: $ perldoc perlvar
  • #28: and , or and not are also supported as operators in their own right. They&apos;re more readable than the C-style operators, but have different precedence to &amp;&amp; and friends.
  • #29: ” ab” . ”cd” is ”abcd” ” ab” x 3 is ”ababab” ( 1 .. 5 ) is (1, 2, 3, 4, 5)
  • #30: $ perldoc perlop Why do we have separate numeric and string comparisons? Because we don&apos;t have special variable types and Perl needs to know whether to sort numerically (where 99 is less than 100) or alphabetically (where 100 comes before 99).
  • #34: $ perldoc -f each
  • #35: $ perldoc perlsyn In th foreach loop the value in $var is really the value in the array, so something like $var += 2; would change the value in the @array. Using the magic variable $_ foreach ( 0 .. $max-1) { print(&amp;quot;This element is $_ &amp;quot;); }
  • #39: Read from the STDIN my $line = &lt;&gt;; chomp($line); Random Number $value = int(rand(1000));
  • #54: Remember: The Fibonacci series is formed by starting with 0 and 1 and then adding the latest two numbers to get the next one: fib(0) = 0 # definition fib(1) = 1 # definition fib(2) = fib(0) + fib(1) = 1 fib(3) = fib(1) + fib(2) = 2 fib(4) = fib(2) + fib(3) = 3 ...
  • #57: $ perldoc -f open $ perldoc perlfunc $ perldoc perlopentut open() is documented in extravagant detail in the perlfunc manpage and the perlopentut manpage.
  • #59: $ perldoc -f close When you&apos;re done with your filehandles, you should close() them. When using scalar variables, file handls will be closed automaticaly when you come out of the variable scope.
  • #62: $ perldoc -f local $/ works like awk&apos;s RS variable. The value of $/ is a string, not a regex. awk has to be better for something.
  • #69: Some examples: &amp;quot;hotdog&amp;quot; =~ /dog/; # matches &amp;quot;hotdog&amp;quot; =~ /^dog/; # doesn&apos;t match &amp;quot;hotdog&amp;quot; =~ /dog$/; # matches &amp;quot;hotdog &amp;quot; =~ /dog$/; # matches &amp;quot;hotdog&amp;quot; =~ /^hotdog$/; # matches
  • #86: $ perldoc -f split
  • #87: The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. Most punctuation names have reasonable mnemonics. Nevertheless, if you wish to use long variable names, you need only say use English; at the top of your program. For $` , $&amp; and $&apos; you could use $PREMATCH , $MATCH or $POSTMATCH
  • #97: $perldoc -f oct $perldoc -f defined
  • #98: $perldoc -f pop $perldoc -f push $perldoc -f shift $perldoc -f unshift
  • #99: $perldoc -f join $perldoc -f sort
  • #100: $perldoc -f reverse
  • #101: $perldoc -f map
  • #102: $perldoc -f grep
  • #104: $perldoc -f each
  • #105: $perldoc -f exists The element is not autovivified if it doesn’t exist.
  • #110: $ perldoc -f our An our declares the variables to be valid globals within the enclosing block, file or eval. That is, it has the same scoping rules as a my declaration, but does not create a local variable.
  • #113: $ perldoc base $ perldoc Exporter
  • #116: $ perldoc perlmod $ perldoc perlmodlib $ perldoc perlmodinstall
  • #118: $ perldoc -f bless
  • #123: $ perldoc perlboot $ perldoc perltoot $ perldoc perlobj