Perl Introduction

  • 8,596 views
Uploaded on

A 20/30 hours course of Perl. …

A 20/30 hours course of Perl.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
8,596
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
845
Comments
3
Likes
19

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • 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'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's most impressive collections of third-party modules (http://www.cpan.org).
  • $ perldoc -f keys $ perldoc -f values
  • $ perldoc perlref Array reference: $refArray1 = @array; $refArray2 = [ "Milan", "Rome"]; Hash reference $refHash1 = \%hash; $refHash2 = { apple => "red", banana => "yellow" };
  • $ perldoc -f my Check the local and our manpages as well. $ perldoc -f our $ perldoc -f local
  • $ perldoc strict $ perldoc warnings Good practice Start your script files with: use strict; use warnings
  • $ perldoc perlvar
  • and , or and not are also supported as operators in their own right. They're more readable than the C-style operators, but have different precedence to && and friends.
  • ” ab” . ”cd” is ”abcd” ” ab” x 3 is ”ababab” ( 1 .. 5 ) is (1, 2, 3, 4, 5)
  • $ perldoc perlop Why do we have separate numeric and string comparisons? Because we don'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).
  • $ perldoc -f each
  • $ 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("This element is $_
    "); }
  • Read from the STDIN my $line = <>; chomp($line); Random Number $value = int(rand(1000));
  • 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 ...
  • $ perldoc -f open $ perldoc perlfunc $ perldoc perlopentut open() is documented in extravagant detail in the perlfunc manpage and the perlopentut manpage.
  • $ perldoc -f close When you'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.
  • $ perldoc -f local $/ works like awk's RS variable. The value of $/ is a string, not a regex. awk has to be better for something.
  • Some examples: "hotdog" =~ /dog/; # matches "hotdog" =~ /^dog/; # doesn't match "hotdog" =~ /dog$/; # matches "hotdog
    " =~ /dog$/; # matches "hotdog" =~ /^hotdog$/; # matches
  • $ perldoc -f split
  • 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 $` , $& and $' you could use $PREMATCH , $MATCH or $POSTMATCH
  • $perldoc -f oct $perldoc -f defined
  • $perldoc -f pop $perldoc -f push $perldoc -f shift $perldoc -f unshift
  • $perldoc -f join $perldoc -f sort
  • $perldoc -f reverse
  • $perldoc -f map
  • $perldoc -f grep
  • $perldoc -f each
  • $perldoc -f exists The element is not autovivified if it doesn’t exist.
  • $ 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.
  • $ perldoc base $ perldoc Exporter
  • $ perldoc perlmod $ perldoc perlmodlib $ perldoc perlmodinstall
  • $ perldoc -f bless
  • $ perldoc perlboot $ perldoc perltoot $ perldoc perlobj

Transcript

  • 1. Perl Welcome Pratical Extraction and Reporting Language Marcos Rebelo oleber@gmail.com
  • 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
    • 12. Large experienced active user base
    • 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
    • 19. System administration
    • 20. Most command line applications
    • 21. Batch processing
    • 22. Reports
    • 23. Database access and manipulation
    • 24. Any application involving significant text processing
  • 25. Hello World
    • in a file:
      • print "Hello, World! "
    • on the shell
      • #> perl -e 'print "Hello, World! "'
    • with perl 5.10
      • #> perl -E 'say "Hello, World!"'
  • 26. Basic syntax overview
    • Perl statements end in a semi-colon. say("Hello, world!") ;
    • 27. You can use parentheses for subroutines arguments or omit them according to your personal taste. say "Hello, world!";
    • 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( " Hello, world! " ); say( ' Hello, world! ' );
    • 30. However, only double quotes interpolate variables and special characters such as newlines ( ): print("Hello, $name! "); # interpolate $name print('Hello, $name! '); # prints $name! literally
  • 31. Basic syntax overview
    • Whitespaces are irrelevant: print( "Hello world");
    • 32. ... except inside quoted strings: print("Hello world"); # 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!', " ";
    We will see later that the correct expression is:
      • { local $ = " "; 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("Hello, World! ")'
  • 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 "camel"
      • 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] ;
    • 51. ($scalar1, $scalar2) = @numbers;
  • 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
  • 77. Most common operators
    • Miscellaneous
      • = assignment
      • 78. . string concatenation
      • 79. x string multiplication
      • 80. .. range operator (creates a list of numbers)
  • 81. Most common operators
  • 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 &quot;) while condition;
      • 94. Going throw a hash: while (my ($key,$value) = each (%ENV)){ print &quot;$key=$value &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; $_ &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.
  • 107. Subroutines
  • 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(/ /, $str)
  • 153. Regular Expressions
  • 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 ' 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 [ 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 [^ ]
  • 175. Character Classes
    • The dswDSW abbreviations can be used both inside and outside of character classes. Here are some in use:
      • / dd : dd : dd / - matches a hh:mm:ss time format
      • 176. /[ ds ]/ - 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 =~ /(dd):(dd):(dd)/; 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 =~ /(dd):(dd):(dd)/);
  • 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; $` : $& : $' &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,}/ /(ww)-(ww)-(ww)/ /W+/
  • 219. Core subroutines
  • 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 &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.
  • 257. Modules and OO
  • 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 &quot;);# &quot;0-0--1&quot; Dog::inc(); print(&quot;$d-$c-$Dog::d-$Dog::c &quot;);# &quot;0-0--2&quot; inc(); print(&quot;$d-$c-$Dog::d-$Dog::c &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 &quot;);} sub func2() {print(&quot;func2 &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 &quot;);} sub func2() {print(&quot;func2 &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; &quot;); print(&quot;Bat: &quot;.$bat->fly().&quot; &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.
  • 286. Standard Modules
  • 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
  • 334. Advanced Perl
  • 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 &quot;; $var = 6; # dies
  • 338. Q/A