Introduction to Perl An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
What We Will Cover <ul><li>What is Perl?
Creating and running a Perl program
Input and Output
Perl variables
Operators and Functions
Conditional Constructs </li></ul>
What We Will Cover <ul><li>Subroutines
Regular Expressions
References
Smart Matching
Finding and using Modules
Further Information </li></ul>
Schedule <ul><li>09:45 – Begin
11:15 – Coffee break
13:00 – Lunch
14:00 – Begin
15:30 – Coffee break
17:00 – End </li></ul>
Resources <ul><li>Slides available on-line </li><ul><li>http://mag-sol.com/train/public/2009-02/begin </li></ul><li>Also s...
What is Perl?
Perl's Name <ul><li>Practical Extraction and Reporting Language
Pathologically Eclectic Rubbish Lister
&quot;Perl&quot; is the language
&quot;perl&quot; is the compiler
Never &quot;PERL&quot; </li></ul>
Typical uses of Perl <ul><li>Text processing
System administration tasks
CGI and web programming
Database interaction
Other Internet programming </li></ul>
Less typical uses of Perl <ul><li>Human Genome Project
NASA </li></ul>
What is Perl Like? <ul><li>General purpose programming language
Free (open source)‏
Fast
Flexible
Secure
Fun </li></ul>
The Perl Philosophy <ul><li>There's more than one way to do it
Three virtues of a programmer </li><ul><li>Laziness
Impatience
Hubris </li></ul><li>Share and enjoy! </li></ul>
Creating and Running a Perl Program
Creating a Perl Program <ul><li>Our first Perl program print &quot;Hello world
&quot;;
Put this in a file called hello.pl </li></ul>
Running a Perl Program <ul><li>Running a Perl program from the command line
$ perl hello.pl </li></ul>
Running a Perl Program <ul><li>The &quot;shebang&quot; line (Unix, not Perl) #!/usr/bin/perl
Make program executable $ chmod +x hello.pl
Run from command line $ ./hello.pl </li></ul>
Perl Comments <ul><li>Add comments to your code
Start with a hash ( # )‏
Continue to end of line
# This is a hello world program print &quot;Hello, world!
&quot;; # print  </li></ul>
Command Line Options <ul><li>Many options to control execution of the program
For example,  -w  turns on warnings
Use on command line perl -w hello.pl
Or on shebang line #!/usr/bin/perl -w </li></ul>
Perl variables
What is a Variable? <ul><li>A place where we can store data
A variable needs a name to </li></ul><ul><ul><li>retrieve the data stored in it
put new data in it </li></ul></ul>
Variable Names <ul><li>Contain alphanumeric characters and underscores
User variable names may not start with numbers
Variable names are preceded by a punctuation mark indicating the type of data </li></ul>
Types of Perl Variable <ul><li>Different types of variables start with a different symbol </li></ul><ul><ul><li>Scalar var...
Array variables start with @
Hash variables start with % </li></ul></ul><ul><li>More on these types soon </li></ul>
Declaring Variables <ul><li>You don't need to declare variables in Perl
But it's a very good idea </li></ul><ul><ul><li>typos
scoping </li></ul></ul><ul><li>Using the strict pragma use strict; my $var; </li></ul>
Scalar Variables <ul><li>Store a single item of data
my $name = &quot;Arthur&quot;;
my $whoami = 'Just Another Perl Hacker';
my $meaning_of_life = 42;
my $number_less_than_1 = 0.000001;
my $very_large_number = 3.27e17;    # 3.27 times 10 to the power of 17 </li></ul>
Type Conversions <ul><li>Perl converts between strings and numbers whenever necessary
Add int to a floating point number my $sum = $meaning_of_life +   $number_less_than_1;
Putting a number into a string   print &quot;$name says, 'The meaning of life is $sum.'
&quot;; </li></ul>
Quoting Strings <ul><li>Single quotes don't expand variables or escape sequences my $price = '$9.95';
Double quotes do my $invline =   &quot;24 widgets @ $price each
&quot;;
Use a backslash to escape special characters in double quoted strings print &quot;He said &quot;The price is $300&quot;&qu...
Better Quotes <ul><li>This can look ugly   print &quot;He said &quot;The price is $300&quot;&quot;;
This is a tidier alternative   print qq(He said &quot;The price is $300&quot;);
Also works for single quotes   print q(He said &quot;That's too expensive&quot;); </li></ul>
Undefined Values <ul><li>A scalar variable that hasn't had data put into it will contain the special value “undef”
Test for it with “defined()” function
if (defined($my_var)) { ... } </li></ul>
Array Variables <ul><li>Arrays contain an ordered list of scalar values
my @fruit = ('apples', 'oranges',   'guavas', 'passionfruit',   'grapes');
my @magic_numbers = (23, 42, 69);
my @random_scalars = ('mumble', 123.45,   'dave cross',   -300, $name); </li></ul>
Array Elements <ul><li>Accessing individual elements of an array
print $fruits[0]; # prints &quot;apples&quot;
Note: Indexes start from zero
print $random_scalars[2]; # prints &quot;dave cross&quot;
Note use of $ as individual element of an array is a scalar </li></ul>
Array Slices <ul><li>Returns a list of elements from an array
print @fruits[0,2,4]; # prints &quot;apples&quot;, &quot;guavas&quot;, # &quot;grapes&quot;
print @fruits[1 .. 3]; # prints &quot;oranges&quot;, &quot;guavas&quot;, # &quot;passionfruit&quot;
Note use of @ as we are accessing more than one element of the array  </li></ul>
Setting Array Values <ul><li>$array[4] = 'something'; $array[400] = 'something else';
Also with slices
@array[4, 7 .. 9] = ('four', 'seven',    'eight', 'nine');
@array[1, 2] = @array[2, 1];
Doesn't need to be an array! </li><ul><li>($x, $y) = ($y, $x); </li></ul></ul>
Array Size <ul><li>$#array  is the index of the last element in  @array
Therefore  $#array + 1  is the number of elements
$count = @array;  does the same thing and is easier to understand </li></ul>
Hash Variables <ul><li>Hashes implement “look-up tables” or “dictionaries”
Initialised with a list %french = ('one', 'un', 'two', 'deux',   'three', 'trois');
&quot;fat comma&quot; (=>) is easier to understand %german = (one  => 'ein',   two  => 'zwei',    three => 'drei'); </li><...
Accessing Hash Values <ul><li>$three = $french{three};
print $german{two};
As with arrays, notice the use of $ to indicate that we're accessing a single value </li></ul>
Hash Slices <ul><li>Just like array slices
Returns a list of elements from a hash print @french{'one','two','three'}; # prints &quot;un&quot;, &quot;deux&quot; & &qu...
Again, note use of @ as we are accessing more than one value from the hash </li></ul>
Setting Hash Values <ul><li>$hash{foo} = 'something';
$hash{bar} = 'something else';
Also with slices
@hash{'foo', 'bar'} =  ('something', 'else');
@hash{'foo', 'bar'} =  @hash{'bar', 'foo'}; </li></ul>
More About Hashes <ul><li>Hashes are not sorted
There is no equivalent to  $#array
print %hash  is unhelpful
We'll see ways round these restrictions later </li></ul>
Special Perl Variables <ul><li>Perl has many special variables
Many of them have punctuation marks as names
Others have names in ALL_CAPS
They are documented in perlvar </li></ul>
The Default Variable <ul><li>Many Perl operations either set  $_  or use its value if no other is given
print; # prints the value of $_
If a piece of Perl code seems to be missing a variable, then it's probably using  $_
Think of “it” or “that” in English </li></ul>
Using $_ <ul><li>while (<FILE>) {   if (/regex/) {   print;   } }
Three uses of $_ </li></ul>
A Special Array <ul><li>@ARGV
Contains your programs command line arguments
perl printargs.pl foo bar baz
my $num = @ARGV; print &quot;$num arguments: @ARGV
&quot;; </li></ul>
A Special Hash <ul><li>%ENV
Contains the  environment variables  that your script has access to.
Keys are the variable names Values are the… well… values!
print $ENV{PATH}; </li></ul>
Input and Output
Input and Output <ul><li>Programs become more useful with input and output
We'll see more input and output methods later in the day
But here are some simple methods </li></ul>
Output <ul><li>The easiest way to get data out of a Perl program is to use  print
print “Hello world
”; </li></ul>
Input <ul><li>The easiest way to get data into a Perl program is to read from  STDIN
$input = <STDIN>;
< ... >  is the “read from filehandle” operator
STDIN  is the standard input filehandle </li></ul>
A Complete Example <ul><li>#!/usr/bin/perl print 'What is your name: '; $name = <STDIN>; print “Hello $name”; </li></ul>
Operators and Functions
Operators and Functions <ul><li>What are operators and functions?
&quot;Things&quot; that do &quot;stuff&quot;
Routines built into Perl to manipulate data
Other languages have a strong distinction between operators and functions </li><ul><li>in Perl that distinction can be a b...
Arithmetic Operators <ul><li>Standard arithmetic operations add (+), subtract (-), multiply (*), divide (/)‏
Less standard operations modulus (%), exponentiation (**)‏
$speed = $distance / $time; $vol = $length * $breadth * $height; $area = $pi * ($radius ** 2); $odd = $number % 2; </li></ul>
Shortcut Operators <ul><li>Often need to do things like $total = $total + $amount;
Can be abbreviated to $total += $amount;
Even shorter $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1
Upcoming SlideShare
Loading in...5
×

Beginning Perl

8,163

Published on

The Beginning Perl course that I ran for the UKUUG in Feb 2009.

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
  • the slides are good and educative I would like to have more of your slides on high shool syllabus in case you have any. this is george from Kenya
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
8,163
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
686
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Beginning Perl

  1. 1. Introduction to Perl An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
  2. 2. What We Will Cover <ul><li>What is Perl?
  3. 3. Creating and running a Perl program
  4. 4. Input and Output
  5. 5. Perl variables
  6. 6. Operators and Functions
  7. 7. Conditional Constructs </li></ul>
  8. 8. What We Will Cover <ul><li>Subroutines
  9. 9. Regular Expressions
  10. 10. References
  11. 11. Smart Matching
  12. 12. Finding and using Modules
  13. 13. Further Information </li></ul>
  14. 14. Schedule <ul><li>09:45 – Begin
  15. 15. 11:15 – Coffee break
  16. 16. 13:00 – Lunch
  17. 17. 14:00 – Begin
  18. 18. 15:30 – Coffee break
  19. 19. 17:00 – End </li></ul>
  20. 20. Resources <ul><li>Slides available on-line </li><ul><li>http://mag-sol.com/train/public/2009-02/begin </li></ul><li>Also see Slideshare </li><ul><li>http://www.slideshare.net/davorg/slideshows </li></ul><li>Mailing List </li><ul><li>http://lists.mag-sol.com/mailman/listinfo/beg2009 </li></ul><li>Get Satisfaction </li><ul><li>http://getsatisfaction.com/magnum </li></ul></ul>
  21. 21. What is Perl?
  22. 22. Perl's Name <ul><li>Practical Extraction and Reporting Language
  23. 23. Pathologically Eclectic Rubbish Lister
  24. 24. &quot;Perl&quot; is the language
  25. 25. &quot;perl&quot; is the compiler
  26. 26. Never &quot;PERL&quot; </li></ul>
  27. 27. Typical uses of Perl <ul><li>Text processing
  28. 28. System administration tasks
  29. 29. CGI and web programming
  30. 30. Database interaction
  31. 31. Other Internet programming </li></ul>
  32. 32. Less typical uses of Perl <ul><li>Human Genome Project
  33. 33. NASA </li></ul>
  34. 34. What is Perl Like? <ul><li>General purpose programming language
  35. 35. Free (open source)‏
  36. 36. Fast
  37. 37. Flexible
  38. 38. Secure
  39. 39. Fun </li></ul>
  40. 40. The Perl Philosophy <ul><li>There's more than one way to do it
  41. 41. Three virtues of a programmer </li><ul><li>Laziness
  42. 42. Impatience
  43. 43. Hubris </li></ul><li>Share and enjoy! </li></ul>
  44. 44. Creating and Running a Perl Program
  45. 45. Creating a Perl Program <ul><li>Our first Perl program print &quot;Hello world &quot;;
  46. 46. Put this in a file called hello.pl </li></ul>
  47. 47. Running a Perl Program <ul><li>Running a Perl program from the command line
  48. 48. $ perl hello.pl </li></ul>
  49. 49. Running a Perl Program <ul><li>The &quot;shebang&quot; line (Unix, not Perl) #!/usr/bin/perl
  50. 50. Make program executable $ chmod +x hello.pl
  51. 51. Run from command line $ ./hello.pl </li></ul>
  52. 52. Perl Comments <ul><li>Add comments to your code
  53. 53. Start with a hash ( # )‏
  54. 54. Continue to end of line
  55. 55. # This is a hello world program print &quot;Hello, world! &quot;; # print </li></ul>
  56. 56. Command Line Options <ul><li>Many options to control execution of the program
  57. 57. For example, -w turns on warnings
  58. 58. Use on command line perl -w hello.pl
  59. 59. Or on shebang line #!/usr/bin/perl -w </li></ul>
  60. 60. Perl variables
  61. 61. What is a Variable? <ul><li>A place where we can store data
  62. 62. A variable needs a name to </li></ul><ul><ul><li>retrieve the data stored in it
  63. 63. put new data in it </li></ul></ul>
  64. 64. Variable Names <ul><li>Contain alphanumeric characters and underscores
  65. 65. User variable names may not start with numbers
  66. 66. Variable names are preceded by a punctuation mark indicating the type of data </li></ul>
  67. 67. Types of Perl Variable <ul><li>Different types of variables start with a different symbol </li></ul><ul><ul><li>Scalar variables start with $
  68. 68. Array variables start with @
  69. 69. Hash variables start with % </li></ul></ul><ul><li>More on these types soon </li></ul>
  70. 70. Declaring Variables <ul><li>You don't need to declare variables in Perl
  71. 71. But it's a very good idea </li></ul><ul><ul><li>typos
  72. 72. scoping </li></ul></ul><ul><li>Using the strict pragma use strict; my $var; </li></ul>
  73. 73. Scalar Variables <ul><li>Store a single item of data
  74. 74. my $name = &quot;Arthur&quot;;
  75. 75. my $whoami = 'Just Another Perl Hacker';
  76. 76. my $meaning_of_life = 42;
  77. 77. my $number_less_than_1 = 0.000001;
  78. 78. my $very_large_number = 3.27e17; # 3.27 times 10 to the power of 17 </li></ul>
  79. 79. Type Conversions <ul><li>Perl converts between strings and numbers whenever necessary
  80. 80. Add int to a floating point number my $sum = $meaning_of_life + $number_less_than_1;
  81. 81. Putting a number into a string print &quot;$name says, 'The meaning of life is $sum.' &quot;; </li></ul>
  82. 82. Quoting Strings <ul><li>Single quotes don't expand variables or escape sequences my $price = '$9.95';
  83. 83. Double quotes do my $invline = &quot;24 widgets @ $price each &quot;;
  84. 84. Use a backslash to escape special characters in double quoted strings print &quot;He said &quot;The price is $300&quot;&quot;; </li></ul>
  85. 85. Better Quotes <ul><li>This can look ugly print &quot;He said &quot;The price is $300&quot;&quot;;
  86. 86. This is a tidier alternative print qq(He said &quot;The price is $300&quot;);
  87. 87. Also works for single quotes print q(He said &quot;That's too expensive&quot;); </li></ul>
  88. 88. Undefined Values <ul><li>A scalar variable that hasn't had data put into it will contain the special value “undef”
  89. 89. Test for it with “defined()” function
  90. 90. if (defined($my_var)) { ... } </li></ul>
  91. 91. Array Variables <ul><li>Arrays contain an ordered list of scalar values
  92. 92. my @fruit = ('apples', 'oranges', 'guavas', 'passionfruit', 'grapes');
  93. 93. my @magic_numbers = (23, 42, 69);
  94. 94. my @random_scalars = ('mumble', 123.45, 'dave cross', -300, $name); </li></ul>
  95. 95. Array Elements <ul><li>Accessing individual elements of an array
  96. 96. print $fruits[0]; # prints &quot;apples&quot;
  97. 97. Note: Indexes start from zero
  98. 98. print $random_scalars[2]; # prints &quot;dave cross&quot;
  99. 99. Note use of $ as individual element of an array is a scalar </li></ul>
  100. 100. Array Slices <ul><li>Returns a list of elements from an array
  101. 101. print @fruits[0,2,4]; # prints &quot;apples&quot;, &quot;guavas&quot;, # &quot;grapes&quot;
  102. 102. print @fruits[1 .. 3]; # prints &quot;oranges&quot;, &quot;guavas&quot;, # &quot;passionfruit&quot;
  103. 103. Note use of @ as we are accessing more than one element of the array </li></ul>
  104. 104. Setting Array Values <ul><li>$array[4] = 'something'; $array[400] = 'something else';
  105. 105. Also with slices
  106. 106. @array[4, 7 .. 9] = ('four', 'seven', 'eight', 'nine');
  107. 107. @array[1, 2] = @array[2, 1];
  108. 108. Doesn't need to be an array! </li><ul><li>($x, $y) = ($y, $x); </li></ul></ul>
  109. 109. Array Size <ul><li>$#array is the index of the last element in @array
  110. 110. Therefore $#array + 1 is the number of elements
  111. 111. $count = @array; does the same thing and is easier to understand </li></ul>
  112. 112. Hash Variables <ul><li>Hashes implement “look-up tables” or “dictionaries”
  113. 113. Initialised with a list %french = ('one', 'un', 'two', 'deux', 'three', 'trois');
  114. 114. &quot;fat comma&quot; (=>) is easier to understand %german = (one => 'ein', two => 'zwei', three => 'drei'); </li></ul>
  115. 115. Accessing Hash Values <ul><li>$three = $french{three};
  116. 116. print $german{two};
  117. 117. As with arrays, notice the use of $ to indicate that we're accessing a single value </li></ul>
  118. 118. Hash Slices <ul><li>Just like array slices
  119. 119. Returns a list of elements from a hash print @french{'one','two','three'}; # prints &quot;un&quot;, &quot;deux&quot; & &quot;trois&quot;
  120. 120. Again, note use of @ as we are accessing more than one value from the hash </li></ul>
  121. 121. Setting Hash Values <ul><li>$hash{foo} = 'something';
  122. 122. $hash{bar} = 'something else';
  123. 123. Also with slices
  124. 124. @hash{'foo', 'bar'} = ('something', 'else');
  125. 125. @hash{'foo', 'bar'} = @hash{'bar', 'foo'}; </li></ul>
  126. 126. More About Hashes <ul><li>Hashes are not sorted
  127. 127. There is no equivalent to $#array
  128. 128. print %hash is unhelpful
  129. 129. We'll see ways round these restrictions later </li></ul>
  130. 130. Special Perl Variables <ul><li>Perl has many special variables
  131. 131. Many of them have punctuation marks as names
  132. 132. Others have names in ALL_CAPS
  133. 133. They are documented in perlvar </li></ul>
  134. 134. The Default Variable <ul><li>Many Perl operations either set $_ or use its value if no other is given
  135. 135. print; # prints the value of $_
  136. 136. If a piece of Perl code seems to be missing a variable, then it's probably using $_
  137. 137. Think of “it” or “that” in English </li></ul>
  138. 138. Using $_ <ul><li>while (<FILE>) { if (/regex/) { print; } }
  139. 139. Three uses of $_ </li></ul>
  140. 140. A Special Array <ul><li>@ARGV
  141. 141. Contains your programs command line arguments
  142. 142. perl printargs.pl foo bar baz
  143. 143. my $num = @ARGV; print &quot;$num arguments: @ARGV &quot;; </li></ul>
  144. 144. A Special Hash <ul><li>%ENV
  145. 145. Contains the environment variables that your script has access to.
  146. 146. Keys are the variable names Values are the… well… values!
  147. 147. print $ENV{PATH}; </li></ul>
  148. 148. Input and Output
  149. 149. Input and Output <ul><li>Programs become more useful with input and output
  150. 150. We'll see more input and output methods later in the day
  151. 151. But here are some simple methods </li></ul>
  152. 152. Output <ul><li>The easiest way to get data out of a Perl program is to use print
  153. 153. print “Hello world ”; </li></ul>
  154. 154. Input <ul><li>The easiest way to get data into a Perl program is to read from STDIN
  155. 155. $input = <STDIN>;
  156. 156. < ... > is the “read from filehandle” operator
  157. 157. STDIN is the standard input filehandle </li></ul>
  158. 158. A Complete Example <ul><li>#!/usr/bin/perl print 'What is your name: '; $name = <STDIN>; print “Hello $name”; </li></ul>
  159. 159. Operators and Functions
  160. 160. Operators and Functions <ul><li>What are operators and functions?
  161. 161. &quot;Things&quot; that do &quot;stuff&quot;
  162. 162. Routines built into Perl to manipulate data
  163. 163. Other languages have a strong distinction between operators and functions </li><ul><li>in Perl that distinction can be a bit blurred </li></ul><li>See perlop and perlfunc </li></ul>
  164. 164. Arithmetic Operators <ul><li>Standard arithmetic operations add (+), subtract (-), multiply (*), divide (/)‏
  165. 165. Less standard operations modulus (%), exponentiation (**)‏
  166. 166. $speed = $distance / $time; $vol = $length * $breadth * $height; $area = $pi * ($radius ** 2); $odd = $number % 2; </li></ul>
  167. 167. Shortcut Operators <ul><li>Often need to do things like $total = $total + $amount;
  168. 168. Can be abbreviated to $total += $amount;
  169. 169. Even shorter $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1
  170. 170. Subtle difference between $x++ and ++$x </li></ul>
  171. 171. String Operators <ul><li>Concaternation (.) $name = $firstname . ' ' . $surname;
  172. 172. Repetition (x) $line = '-' x 80; $police = 'hello ' x 3;
  173. 173. Shortcut versions available $page .= $line; # $page = $page . $line $thing x= $i; # $thing = $thing x $i </li></ul>
  174. 174. File Test Operators <ul><li>Check various attributes of a file
  175. 175. -e $file does the file exist
  176. 176. -r $file is the file readable
  177. 177. -w $file is the file writeable
  178. 178. -d $file is the file a directory
  179. 179. -f $file is the file a normal file
  180. 180. -T $file is a text file
  181. 181. -B $file is a binary file </li></ul>
  182. 182. Functions <ul><li>Have longer names than operators
  183. 183. Can take more arguments than operators
  184. 184. Arguments follow the function name
  185. 185. See perlfunc for a complete list of Perl's built-in functions </li></ul>
  186. 186. Function Return Values <ul><li>Functions can return scalars or lists (or nothing)‏
  187. 187. $age = 29.75; $years = int($age);
  188. 188. @list = ('a', 'random', 'collection', 'of', 'words'); @sorted = sort(@list); # a collection of random words </li></ul>
  189. 189. String Functions <ul><li>length returns the length of a string $len = length $a_string;
  190. 190. uc and lc return upper and lower case versions of a string $string = 'MiXeD CaSe'; print &quot;$string &quot;, uc $string, &quot; &quot;, lc $string;
  191. 191. See also ucfirst and lcfirst </li></ul>
  192. 192. More String Functions <ul><li>chop removes the last character from a string and returns it $word = 'word'; $letter = chop $word;
  193. 193. chomp removes the last character only if it is a newline and returns true or false appropriately </li></ul>
  194. 194. Substrings <ul><li>substr returns substrings from a string $string = 'Hello world'; print substr($string, 0, 5); # prints 'Hello'
  195. 195. Unlike many other languages you can assign to a substring substr($string, 0, 5) = 'Greetings'; print $string; # prints 'Greetings world' </li></ul>
  196. 196. Numeric Functions <ul><li>abs returns the absolute value
  197. 197. cos , sin , tan standard trigonometric functions
  198. 198. exp exponentiation using e
  199. 199. log logarithm to base e
  200. 200. rand returns a random number
  201. 201. sqrt returns the square root </li></ul>
  202. 202. Array Manipulation <ul><li>push adds a new element to the end of an array push @array, $value;
  203. 203. pop removes and returns the last element in an array $value = pop @array;
  204. 204. shift and unshift do the same for the start of an array </li></ul>
  205. 205. Array Manipulation <ul><li>sort returns a sorted list (it does not sort the list in place) @sorted = sort @array;
  206. 206. sort does a lot more besides, see the docs (perldoc -f sort)‏
  207. 207. reverse returns a reversed list @reverse = reverse @array; </li></ul>
  208. 208. Arrays and Strings <ul><li>join takes an array and returns a string @array = (1 .. 5); $string = join ' ', @array; # $string is '1 2 3 4 5'
  209. 209. split takes a string and converts it into an array $string = '1~2~3~4~5'; @array = split(/~/, $string); # @array is (1, 2, 3, 4, 5)‏ </li></ul>
  210. 210. Hash Functions <ul><li>delete removes a key/value pair from a hash
  211. 211. exists tells you if an element exists in a hash
  212. 212. keys returns a list of all the keys in a hash
  213. 213. values returns a list of all the values in a hash </li></ul>
  214. 214. File Operations <ul><li>open opens a file and associates it with a filehandle open(FILE, 'in.dat');
  215. 215. You can then read the file with <FILE> $line = <FILE>; # one line @lines = <FILE>; # all lines
  216. 216. Finally, close the file with close close(FILE); </li></ul>
  217. 217. Other File Functions <ul><li>read to read a fixed number of bytes into a buffer $bytes = read(FILE, $buffer, 1024);
  218. 218. seek to move to a random position in a file seek(FILE, 0, 0);
  219. 219. tell to get current file position $where = tell FILE;
  220. 220. truncate to truncate file to given size truncate FILE, $where; </li></ul>
  221. 221. Time Functions <ul><li>time returns the number of seconds since Jan 1st 1970
  222. 222. $now = time;
  223. 223. localtime converts that into more usable values
  224. 224. ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($now); </li></ul>
  225. 225. localtime Caveats <ul><li>$mon is 0 to 11
  226. 226. $year is years since 1900
  227. 227. $wday is 0 (Sun) to 6 (Sat)‏ </li></ul>
  228. 228. localtime Shortcuts <ul><li>It's common to use localtime on the current time
  229. 229. @time_bits = localtime(time);
  230. 230. Call to time can be omitted
  231. 231. @time_bits = localtime;
  232. 232. Use array slices
  233. 233. ($d, $m, $y) = (localtime)[3 .. 5]; </li></ul>
  234. 234. Date & Time Formats <ul><li>You can get formatted dates by fiddling the return values from localtime
  235. 235. Easier to use strftime (from POSIX.pm)
  236. 236. print strftime('%Y-%m-%d', localtime);
  237. 237. Format followed by list of date/time values
  238. 238. Format is POSIX standard </li></ul><ul><ul><li>Like UNIX date command </li></ul></ul><ul><li>print strftime ('%d %B %y', localtime); </li></ul>
  239. 239. Conditional Constructs
  240. 240. Conditional Constructs <ul><li>Conditional constructs allow us to choose different routes of execution through the program
  241. 241. This makes for far more interesting programs
  242. 242. The unit of program execution is a block of code
  243. 243. Blocks are delimited with braces { … } </li></ul>
  244. 244. Conditional Constructs <ul><li>Conditional blocks are controlled by the evaluation of an expression to see if it is true or false
  245. 245. But what is truth? </li></ul>
  246. 246. What is Truth? <ul><li>In Perl it's easier to answer the question &quot;what is false?&quot; </li></ul><ul><ul><li>0 (the number zero)‏
  247. 247. '' (the empty string)‏
  248. 248. undef (an undefined value)‏
  249. 249. () (an empty list)‏ </li></ul></ul><ul><li>Everything else is true </li></ul>
  250. 250. Comparison Operators <ul><li>Compare two values in some way </li></ul><ul><ul><li>are they equal $x == $y or $x eq $y $x != $y or $x ne $y
  251. 251. Is one greater than another $x > $y or $x gt $y $x >= $y or $x ge $y
  252. 252. Also < ( lt ) and <= ( le )‏ </li></ul></ul>
  253. 253. Comparison Examples <ul><li>62 > 42 # true
  254. 254. '0' == (3 * 2) - 6 # true
  255. 255. 'apple' gt 'banana' # false
  256. 256. 'apple' == 'banana' # true(!)‏
  257. 257. 1 + 2 == '3 bears' # true
  258. 258. 1 + 3 == 'three' # false </li></ul>
  259. 259. Boolean Operators <ul><li>Combine two or more conditional expressions into one
  260. 260. EXPR_1 and EXPR_2 true if both EXPR_1 and EXPR_2 are true
  261. 261. EXPR_1 or _EXPR_2 true if either EXPR_1 or _EXPR_2 are true
  262. 262. Alternative syntax && for and and || for or
  263. 263. Different precedence though </li></ul>
  264. 264. Short-Circuit Operators <ul><li>EXPR_1 or EXPR_2 Only need to evaluate EXPR_2 if EXPR_1 evaluates as false
  265. 265. We can use this to make code easier to follow open FILE, 'something.dat' or die &quot;Can't open file: $!&quot;;
  266. 266. @ARGV == 2 or print $usage_msg; </li></ul>
  267. 267. if <ul><li>if - our first conditional
  268. 268. if (EXPR) { BLOCK }
  269. 269. Only executes BLOCK if EXPR is true if ($name eq 'Doctor') { regenerate(); } </li></ul>
  270. 270. if ... else ... <ul><li>if … else ... - an extended if if (EXPR) { BLOCK1 } else { BLOCK2}
  271. 271. If EXPR is true, execute BLOCK1, otherwise execute BLOCK2
  272. 272. if ($name eq 'Doctor') { regenerate(); } else { die &quot;Game over! &quot;; } </li></ul>
  273. 273. if ... elsif ... else ... <ul><li>if … elsif … else ... - even more control if (EXPR1) { BLOCK1 } elsif (EXPR2) { BLOCK2 } else { BLOCK3 }
  274. 274. If EXPR1 is true, execute BLOCK1 else if EXPR2 is true, execute BLOCK2 otherwise execute BLOCK3 </li></ul>
  275. 275. if ... elsif ... else ... <ul><li>An example if ($name eq 'Doctor') { regenerate(); } elsif ($tardis_location eq $here) { escape(); } else { die &quot;Game over! &quot;; } </li></ul>
  276. 276. while <ul><li>while - repeat the same code while (EXPR) { BLOCK }
  277. 277. Repeat BLOCK while EXPR is true while ($dalek_prisoners) { print &quot;Ex-ter-min-ate &quot;; $dalek_prisoners--; } </li></ul>
  278. 278. until <ul><li>until - the opposite of while until (EXPR) { BLOCK }
  279. 279. Execute BLOCK until EXPR is true until ($regenerations == 12) { print &quot;Regenerating &quot;; regenerate(); $regenerations++; } </li></ul>
  280. 280. for <ul><li>for - more complex loops for (INIT; EXPR; INCR) { BLOCK }
  281. 281. Like C
  282. 282. Execute INIT If EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR </li></ul>
  283. 283. for <ul><li>An example for ($i = 1; $i <= 10; $i++) { print &quot;$i squared is &quot;, $i * $i, &quot; &quot;; }
  284. 284. Used surprisingly rarely </li></ul>
  285. 285. foreach <ul><li>foreach - simpler looping over lists foreach VAR (LIST) { BLOCK }
  286. 286. For each element of LIST, set VAR to equal the element and execute BLOCK
  287. 287. foreach $i (1 .. 10) { print &quot;$i squared is &quot;, $i * $i, &quot; &quot;; } </li></ul>
  288. 288. foreach <ul><li>Another example my %months = (Jan => 31, Feb => 28, Mar => 31, Apr => 30, May => 31, Jun => 30, … ); foreach (keys %months) { print &quot;$_ has $months{$_} days &quot;; } </li></ul>
  289. 289. Using while Loops <ul><li>Taking input from STDIN
  290. 290. while (<STDIN>) { print; }
  291. 291. This is the same as while (defined($_ = <STDIN>)) { print $_; } </li></ul>
  292. 292. Breaking Out of Loops <ul><li>next - jump to next iteration of loop
  293. 293. last - jump out of loop
  294. 294. redo - jump to start of same iteration of loop </li></ul>
  295. 295. Subroutines
  296. 296. Subroutines <ul><li>Self-contained &quot;mini-programs&quot; within your program
  297. 297. Make it easy to repeat code
  298. 298. Subroutines have a name and a block of code
  299. 299. sub NAME { BLOCK } </li></ul>
  300. 300. Subroutine Example <ul><li>Simple subroutine example sub exterminate { print &quot;Ex-Ter-Min-Ate!! &quot;; $timelords--; } </li></ul>
  301. 301. Calling a Subroutine <ul><li>&exterminate;
  302. 302. exterminate();
  303. 303. exterminate;
  304. 304. last one only works if function has been predeclared </li></ul>
  305. 305. Subroutine Arguments <ul><li>Functions become far more useful if you can pass arguments to them
  306. 306. exterminate('The Doctor');
  307. 307. Arguments end up in the @_ array within the function
  308. 308. sub exterminate { my ($name) = @_; print &quot;Ex-Ter-Min-Ate $name &quot;; $timelords--; } </li></ul>
  309. 309. Multiple Arguments <ul><li>As @_ is an array it can contain multiple arguments
  310. 310. sub exterminate { foreach (@_) { print &quot;Ex-Ter-Min-Ate $_ &quot;; $timelords--; } } </li></ul>
  311. 311. Calling Subroutines <ul><li>A subtle difference between &my_sub and my_sub()‏
  312. 312. &my_sub passes on the contents of @_ to the called subroutine sub first { &second }; sub second { print @_ }; first('some', 'random', 'data');
  313. 313. You usually don't want to do that </li></ul>
  314. 314. By Value or Reference <ul><li>Passing by value passes the value of the variable into the subroutine. Changing the argument doesn't alter the external variable
  315. 315. Passing by value passes the actual variable. Changing the argument alters the external value
  316. 316. Perl allows you to choose </li></ul>
  317. 317. By Value or Reference <ul><li>Simulating pass by value
  318. 318. my ($arg1, $arg2) = @_;
  319. 319. Updating $arg1 and $arg2 doesn’t effect anything outside the subroutine
  320. 320. Simulating pass by reference
  321. 321. $_[0] = 'whatever';
  322. 322. Updating the contents of @_ updates the external values </li></ul>
  323. 323. Returning Values <ul><li>Use return to return a value from a subroutine sub exterminate { if (rand > .25) { print &quot;Ex-Ter-Min-Ate $_[0] &quot;; $timelords--; return 1; } else { return; } } </li></ul>
  324. 324. Returning a List <ul><li>Returning a list from a subroutine sub exterminate { my @exterminated; foreach (@_) { if (rand > .25) { print &quot;Ex-Ter-Min-Ate $_ &quot;; $timelords--; push @exterminated, $_; } } return @exterminated; } </li></ul>
  325. 325. Regular Expressions
  326. 326. Regular Expressions <ul><li>Patterns that match strings
  327. 327. A bit like wild-cards
  328. 328. A &quot;mini-language&quot; within Perl </li><ul><li>Alien DNA </li></ul><li>The key to Perl's text processing power
  329. 329. Sometimes overused!
  330. 330. Documented in perldoc perlre </li></ul>
  331. 331. Match Operator <ul><li>m/PATTERN/ - the match operator
  332. 332. Works on $_ by default
  333. 333. In scalar context returns true if the match succeeds
  334. 334. In list context returns list of &quot;captured&quot; text
  335. 335. m is optional if you use / characters
  336. 336. With m you can use any delimiters </li></ul>
  337. 337. Match Examples <ul><li>while (<FILE>) { print if /foo/; print if /bar/i; print if m|http://|; } </li></ul>
  338. 338. Substitutions <ul><li>s/PATTERN/REPLACEMENT/ - the substitution operator
  339. 339. Works on $_ by default
  340. 340. In scalar context returns true if substitution succeeds
  341. 341. In list context returns number of replacements
  342. 342. Can choose any delimiter </li></ul>
  343. 343. Substitution Examples <ul><li>while (<FILE>) { s/teh/the/gi; s/freind/friend/gi; s/sholud/should/gi; print; } </li></ul>
  344. 344. Binding Operator <ul><li>If we want m// or s/// to work on something other than $_ then we need to use the binding operator
  345. 345. $name =~ s/Dave/David/; </li></ul>
  346. 346. Metacharacters <ul><li>Matching something other than literal text
  347. 347. ^ - matches start of string
  348. 348. $ - matches end of string
  349. 349. . - matches any character (except )‏
  350. 350. s - matches a whitespace character
  351. 351. S - matches a non-whitespace character </li></ul>
  352. 352. More Metacharacters <ul><li>d - matches any digit
  353. 353. D - matches any non-digit
  354. 354. w - matches any &quot;word&quot; character
  355. 355. W - matches any &quot;non-word&quot; character
  356. 356.  - matches a word boundary
  357. 357. B - matches anywhere except a word boundary </li></ul>
  358. 358. Metacharacter Examples <ul><li>while (<FILE>) { print if m|^http|; print if /perl/; print if /S/; print if /$d.dd/; } </li></ul>
  359. 359. Quantifiers <ul><li>Specify the number of occurrences
  360. 360. ? - match zero or one
  361. 361. * - match zero or more
  362. 362. + - match one or more
  363. 363. {n} - match exactly n
  364. 364. {n,} - match n or more
  365. 365. {n,m} - match between n and m </li></ul>
  366. 366. Quantifier Examples <ul><li>while (<FILE>) { print if /whiske?y/i; print if /so+n/; print if /d*.d+/; print if /Aw{3}/; } </li></ul>
  367. 367. Character Classes <ul><li>Define a class of characters to match
  368. 368. /[aeiou]/ # match any vowel
  369. 369. Use - to define a contiguous set
  370. 370. /[A-Z]/ # match upper case letters
  371. 371. Use ^ to match inverse set
  372. 372. /[^A-Za-z] # match non-letters </li></ul>
  373. 373. Alternation <ul><li>Use | to match one of a set of options
  374. 374. /rose|martha|donna/i;
  375. 375. Use parentheses for grouping
  376. 376. /^(rose|martha|donna)$/i; </li></ul>
  377. 377. Capturing Matches <ul><li>Parentheses are also used to capture parts of the matched string
  378. 378. The captured parts are in $1, $2, etc… while (<FILE>) { if (/^(w+)s+(w+)/) { print &quot;The first word was $1 &quot;; print &quot;The second word was $2&quot;; } } </li></ul>
  379. 379. Returning Captures <ul><li>Captured values are also returned if the match operator is used in list context
  380. 380. my @nums = $text =~ /(d+)/g; print &quot;I found these integers: &quot;; print &quot;@nums &quot;; </li></ul>
  381. 381. More Information <ul><li>perldoc perlre
  382. 382. perldoc perlretut
  383. 383. Mastering Regular Expressions – Jeffrey Freidl </li></ul>
  384. 384. References
  385. 385. Introducing References <ul><li>A reference is a bit like pointer in languages like C and Pascal (but better)
  386. 386. A reference is a unique way to refer to a variable.
  387. 387. A reference can always fit into a scalar variable
  388. 388. A reference looks like SCALAR(0x20026730) </li></ul>
  389. 389. Creating References <ul><li>Put in front of a variable name $scalar_ref = $scalar; $array_ref = @array; $hash_ref = hash;
  390. 390. Can now treat it just like any other scalar $var = $scalar_ref; $refs[0] = $array_ref; $another_ref = $refs[0]; </li></ul>
  391. 391. Creating References <ul><li>[ LIST ] creates anonymous array and returns a reference $aref = [ 'this', 'is', 'a', 'list']; $aref2 = [ @array ];
  392. 392. { LIST } creates anonymous hash and returns a reference $href = { 1 => 'one', 2 => 'two' }; $href = { %hash }; </li></ul>
  393. 393. Creating References <ul><li>@arr = (1, 2, 3, 4); $aref1 = @arr; $aref2 = [ @arr ]; print &quot;$aref1 $aref2 &quot;; </li></ul><ul><li>Output ARRAY(0x20026800) ARRAY(0x2002bc00)
  394. 394. Second method creates a copy of the array </li></ul>
  395. 395. Using Array References <ul><li>Use {$aref} to get back an array that you have a reference to
  396. 396. Whole array
  397. 397. @array = @{$aref};
  398. 398. @rev = reverse @{$aref};
  399. 399. Single elements
  400. 400. $elem = ${$aref}[0];
  401. 401. ${$aref}[0] = 'foo'; </li></ul>
  402. 402. Using Hash References <ul><li>Use {$href} to get back a hash that you have a reference to
  403. 403. Whole hash
  404. 404. %hash = %{$href};
  405. 405. @keys = keys %{$href};
  406. 406. Single elements
  407. 407. $elem = ${$href}{key};
  408. 408. ${$href}{key} = 'foo'; </li></ul>
  409. 409. Using References <ul><li>Use arrow ( -> ) to access elements of arrays or hashes
  410. 410. Instead of ${$aref}[0] you can use $aref->[0]
  411. 411. Instead of ${$href}{key} you can use $href->{key} </li></ul>
  412. 412. Using References <ul><li>You can find out what a reference is referring to using ref
  413. 413. $aref = [ 1, 2, 3 ]; print ref $aref; # prints ARRAY
  414. 414. $href = { 1 => 'one', 2 => 'two' }; print ref $href; # prints HASH </li></ul>
  415. 415. Why Use References? <ul><li>Parameter parsing
  416. 416. Complex data structures </li></ul>
  417. 417. Parameter Passing <ul><li>What does this do?
  418. 418. @arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; } </li></ul>
  419. 419. Why Doesn't It Work? <ul><li>Arrays are combined in @_
  420. 420. All elements end up in @a1
  421. 421. How do we fix it?
  422. 422. Pass references to the arrays </li></ul>
  423. 423. Another Attempt <ul><li>@arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my ($a1, $a2) = @_; print @$a1 == @$a2 ? 'Yes' : 'No'; } </li></ul>
  424. 424. Complex Data Structures <ul><li>Another good use for references
  425. 425. Try to create a 2-D array
  426. 426. @arr_2d = ((1, 2, 3), (4, 5, 6), (7, 8, 9));
  427. 427. @arr_2d contains (1, 2, 3, 4, 5, 6, 7, 8, 9)
  428. 428. This is known a array flattening </li></ul>
  429. 429. Complex Data Structures <ul><li>2D Array using references
  430. 430. @arr_2d = ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
  431. 431. But how do you access individual elements?
  432. 432. $arr_2d[1] is ref to array (4, 5, 6)
  433. 433. $arr_2d[1]->[1] is element 5 </li></ul>
  434. 434. Complex Data Structures <ul><li>Another 2D Array
  435. 435. $arr_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
  436. 436. $arr_2d->[1] is ref to array (4, 5, 6)
  437. 437. $arr_2d->[1]->[1] is element 5
  438. 438. Can omit intermediate arrows
  439. 439. $arr_2d->[1][1] </li></ul>
  440. 440. More Data Structures <ul><li>Imagine the following data file
  441. 441. Jones,Martha,UNIT Harkness,Jack,Torchwood Smith,Sarah Jane,Journalist
  442. 442. What would be a good data structure?
  443. 443. Hash for each record
  444. 444. Array of records
  445. 445. Array of hashes </li></ul>
  446. 446. More Data Structures <ul><li>Building an array of hashes
  447. 447. my @records; my @cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; } </li></ul>
  448. 448. Using an Array of Hashes foreach (@records) { print &quot;$_->{f_name} &quot;, &quot;$_->{s_name} &quot;. &quot;is a $_->{job} &quot;; }
  449. 449. Complex Data Structures <ul><li>Many more possibilities </li><ul><li>Hash of hashes
  450. 450. Hash of lists
  451. 451. Multiple levels (list of hash of hash, etc.) </li></ul><li>Lots of examples in “ perldoc perldsc” (the data structures cookbook) </li></ul>
  452. 452. Smart Matching
  453. 453. Smart Matching <ul><li>Introduced in Perl 5.10
  454. 454. Powerful matching operator
  455. 455. DWIM
  456. 456. Examines operands
  457. 457. Decides which match to apply </li></ul>
  458. 458. Smart Match Operator <ul><li>~~
  459. 459. New operator
  460. 460. Looks a bit like the binding operator ( =~ )
  461. 461. Can be used in place of it
  462. 462. $some_text =~ /some regex/
  463. 463. Can be replaced with
  464. 464. $some_text ~~ /some regex/ </li></ul>
  465. 465. Smarter Matching <ul><li>If one of its operands is a regex
  466. 466. ~~ does a regex match
  467. 467. Cleverer than that though
  468. 468. %hash ~~ /regex/
  469. 469. Regex match on hash keys
  470. 470. @array ~~ /regex/
  471. 471. Regex match on array elements </li></ul>
  472. 472. More Smart Matches <ul><li>@array1 ~~ @array2
  473. 473. Checks that arrays are the same
  474. 474. $scalar ~~ @array
  475. 475. Checks scalar exists in array
  476. 476. $scalar ~~ %hash
  477. 477. Checks scalar is a hash key </li></ul>
  478. 478. Smart Scalar Matches <ul><li>What kind of match does this do?
  479. 479. $scalar1 ~~ $scalar2
  480. 480. It depends
  481. 481. If both look like numbers
  482. 482. ~~ acts like ==
  483. 483. Otherwise
  484. 484. ~~ acts like eq </li></ul>
  485. 485. Finding and Using Modules
  486. 486. Modules <ul><li>A module is a reusuable 'chunk' of code
  487. 487. Perl comes with over 100 modules (see “ perldoc perlmodlib” for list)
  488. 488. Perl has a repository of freely-available modules - the Comprehensive Perl Archive Network (CPAN) </li><ul><li>http://www.cpan.org
  489. 489. http://search.cpan.org </li></ul></ul>
  490. 490. Finding Modules <ul><li>http://search.cpan.org
  491. 491. Search by: </li><ul><li>module name
  492. 492. distribution name
  493. 493. author name </li></ul><li>Note: CPAN also contains newer versions of standard modules </li></ul>
  494. 494. Installing Modules (The Hard Way) <ul><li>Download distribution file </li><ul><li>MyModule-X.XX.tar.gz </li></ul><li>Unzip </li></ul><ul><ul><li>$ gunzip MyModule-X.XX.tar.gz </li></ul></ul><ul><li>Untar </li></ul><ul><ul><li>$ tar xvf MyModule-X.XX.tar </li></ul></ul><ul><li>Change directory </li></ul><ul><ul><li>$ cd MyModule-X.XX </li></ul></ul>
  495. 495. Installing Modules (The Hard Way) <ul><li>Create Makefile </li></ul><ul><ul><li>$ perl Makefile.PL </li></ul></ul><ul><li>Build Module </li></ul><ul><ul><li>$ make </li></ul></ul><ul><li>Test Build </li></ul><ul><ul><li>$ make test </li></ul></ul><ul><li>Install Module </li></ul><ul><ul><li>$ make install </li></ul></ul>
  496. 496. Installing Modules (The Hard Way) <ul><li>Note: May need root permissions for make install
  497. 497. You can have your own personal module library
  498. 498. perl Makefile.PL PREFIX=~/perl </li><ul><li>need to adjust @INC </li></ul></ul>
  499. 499. Installing Modules (The Easy Way) <ul><li>Note: May not work (or may need some configuration) through a firewall
  500. 500. CPANPLUS.pm is included with newer Perls
  501. 501. Automatically carries out installation process
  502. 502. Can also handle required modules
  503. 503. May still need to be root </li><ul><li>Can use 'sudo' </li></ul></ul>
  504. 504. Installing Modules (The Easy Way) <ul><li>cpanp [ ... some stuff ... ] CPAN Terminal> install Some::Module [ ... some more stuff ... ] CPAN Terminal> quit
  505. 505. Or
  506. 506. cpanp -i Some::Module </li></ul>
  507. 507. Using Modules <ul><li>Two types of module: </li><ul><li>Functions vs Objects </li></ul><li>Functional modules export new subroutines and variables into your program
  508. 508. Object modules usually don't
  509. 509. Difference not clear cut (e.g. CGI.pm) </li></ul>
  510. 510. Using Functional Modules <ul><li>Import defaults: use My::Module;
  511. 511. Import optional components: use My::Module qw(my_sub @my_arr);
  512. 512. Import defined sets of components: use My:Module qw(:advanced);
  513. 513. Use imported components: $data = my_sub(@my_arr); </li></ul>
  514. 514. Using Object Modules <ul><li>Use the module: use My::Object;
  515. 515. Create an object: $obj = My::Object->new; </li><ul><li>Note: new is just a convention </li></ul><li>Interact using object's methods $obj->set_name($name); </li></ul>
  516. 516. Useful Standard Modules <ul><li>constant
  517. 517. Time::Local
  518. 518. Text::ParseWords
  519. 519. Getopt::Std
  520. 520. Cwd
  521. 521. File::Basename </li></ul><ul><li>File::Copy
  522. 522. POSIX
  523. 523. CGI
  524. 524. Carp
  525. 525. Benchmark
  526. 526. Data::Dumper </li></ul>
  527. 527. Useful Non-Standard Modules <ul><li>Template
  528. 528. DBI
  529. 529. DBIx::Class
  530. 530. DateTime
  531. 531. HTML::Parser
  532. 532. HTML::Tidy </li></ul><ul><li>LWP
  533. 533. WWW::Mechanize
  534. 534. Email::Simple
  535. 535. XML::LibXML
  536. 536. XML::Feed
  537. 537. Moose </li></ul>
  538. 538. More Information
  539. 539. Perl Websites <ul><li>Perl Home Page </li></ul><ul><ul><li>http://www.perl.org </li></ul></ul><ul><li>CPAN </li></ul><ul><ul><li>http://www.cpan.org
  540. 540. http://search.cpan.org </li></ul></ul><ul><li>Perl Mongers (Perl User Groups)‏ </li></ul><ul><ul><li>http://www.pm.org
  541. 541. http://london.pm.org </li></ul></ul>
  542. 542. Perl Websites <ul><li>use.perl – Perl News & Journals </li><ul><li>http://use.perl.org/ </li></ul><li>Perl Monks – Perl Help and Advice </li><ul><li>http://perlmonks.org/ </li></ul><li>Perl Documentation Online </li><ul><li>http://perldoc.perl.org/ </li></ul></ul>
  543. 543. Perl Conferences <ul><li>The Perl Conference (part of the Open Source Convention)‏ </li></ul><ul><ul><li>July 20-24 2009, San Jose, California
  544. 544. http://conferences.oreilly.com/oscon </li></ul></ul><ul><li>Yet Another Perl Conference </li></ul><ul><ul><li>August 3-5 2009, Lisbon, Portugal
  545. 545. http://www.yapceurope.org </li></ul></ul>
  546. 546. Perl Conferences <ul><li>Other YAPCs </li><ul><li>Pittsburgh, Pennsylvania </li></ul></ul><ul><ul><li>Brazil
  547. 547. Tokyo </li></ul></ul><ul><li>OSDC </li></ul><ul><ul><li>Israel
  548. 548. Australia </li></ul></ul>
  549. 549. Perl Workshops <ul><li>One-day grassroots conferences
  550. 550. Germany, Israel, Pittsburgh, Nordic, Netherlands, France, Belgium, Russia, Minnesota, Austria
  551. 551. Perl Review Calendar </li></ul><ul><ul><li>www.theperlreview.com/community_calendar </li></ul></ul>
  552. 552. Perl Mailing Lists <ul><li>See http://lists.perl.org for full details </li></ul><ul><ul><li>Perl Mongers (social discussion)‏
  553. 553. CGI
  554. 554. DBI
  555. 555. XML
  556. 556. Beginners
  557. 557. Advocacy
  558. 558. Fun with Perl </li></ul></ul>
  559. 559. Perl Books <ul><li>Books for learning Perl </li></ul><ul><ul><li>Learning Perl (5th ed - June 2008) Schwartz, Phoenix & foy (O'Reilly)‏
  560. 560. Intermediate Perl Schwartz, foy & Phoenix (O'Reilly)‏
  561. 561. Beginning Perl Cozens (Wrox) http://www.perl.org/books/beginning-perl/ </li></ul></ul>
  562. 562. Perl Books <ul><li>Books you should have access to </li></ul><ul><ul><li>Programming Perl (3rd edition) Wall, Christiansen & Orwant (O'Reilly)‏
  563. 563. The Perl Cookbook (2 nd edition) Christiansen & Torkington (O'Reilly)‏
  564. 564. Perl Best Practices Conway (O'Reilly)‏
  565. 565. Perl in a Nutshell Siever, Spainhour & Patwardhan (O'Reilly)‏ </li></ul></ul>
  566. 566. Perl Books <ul><li>Books you should probably look at </li></ul><ul><ul><li>Mastering Regular Expressions Friedl (O'Reilly)‏
  567. 567. Data Munging with Perl Cross (Manning)‏
  568. 568. Advanced Perl Programming Cozens (O'Reilly)‏
  569. 569. Perl Medic Scott (Addison Wesley) </li></ul></ul>
  570. 570. Perl Books <ul><li>Specialised Perl books </li></ul><ul><ul><li>Object Oriented Perl Conway (Manning)‏
  571. 571. Programming the Perl DBI Descartes & Bunce (O'Reilly)‏
  572. 572. Writing CGI Applications with Perl Meltzer & Michelski (Addison Wesley)‏
  573. 573. Practical mod_perl Bekman & Cholet (O'Reilly)‏
  574. 574. Perl Template Toolkit Chamberlain, Cross & Wardley </li></ul></ul>
  575. 575. Perl Magazines <ul><li>The Perl Review </li></ul><ul><ul><li>http://www.theperlreview.com </li></ul></ul>
  576. 576. That's All Folks <ul><li>Questions? </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×