Advertisement

Introduction to Perl - Day 1

Owner / Senior Developer at Magnum Solutions Ltd
Aug. 1, 2009
Advertisement

More Related Content

Advertisement
Advertisement

Introduction to Perl - Day 1

  1. Introduction to Perl Day 1 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
  2. Creating and running a Perl program
  3. Getting help
  4. Input and Output
  5. Perl variables
  6. Operators and Functions
  7. Subroutines
  8. Regular Expressions
  9. Smart Matching
  10. Finding and using Modules
  11. 11:00 – Coffee break (30 mins)
  12. 13:00 – Lunch (90 mins)
  13. 14:30 – Begin
  14. 16:00 – Coffee break (30 mins)
  15. 18:00 – End
  16. What is Perl?
  17. Pathologically Eclectic Rubbish Lister
  18. “Perl” is the language
  19. “perl” is the compiler
  20. Never “PERL”
  21. System administration tasks
  22. CGI and web programming
  23. Database interaction
  24. Other Internet programming
  25. NASA
  26. Free (open source)‏
  27. Fast
  28. Flexible
  29. Secure
  30. Fun
  31. Impatience
  32. Creating and Running a Perl Program
  33. print "Hello world";
  34. Put this in a file called hello.pl
  35. $ perl hello.pl
  36. #!/usr/bin/perl
  37. Make program executable
  38. $ chmod +x hello.pl
  39. Run from command line
  40. $ ./hello.pl
  41. Start with a hash (#)‏
  42. Continue to end of line
  43. # This is a hello world program print "Hello, world!"; # print
  44. For example, -w turns on warnings
  45. Use on command line
  46. perl -w hello.pl
  47. Or on shebang line
  48. #!/usr/bin/perl -w
  49. More usually use warnings
  50. Getting Help
  51. Accessed through the perldoc command
  52. perldoc perl
  53. perldoc perltoc – table of contents
  54. Also online at http://perldoc.perl.org/
  55. Lots of references through the course
  56. perldata
  57. perlsyn
  58. perlfaq
  59. perlstyle
  60. perlcheat
  61. Many many more
  62. Perl Variables
  63. To retrieve the data stored in it
  64. User variable names may not start with numbers
  65. Variable names are preceded by a punctuation mark indicating the type of data
  66. Array variables start with @
  67. use strict; my $var;
  68. my $name = "Arthur";
  69. my $whoami = 'Just Another Perl Hacker';
  70. my $meaning_of_life = 42;
  71. my $number_less_than_1 = 0.000001;
  72. my $very_large_number = 3.27e17; # 3.27 times 10 to the power of 17
  73. Add int to a floating point number
  74. my $sum = $meaning_of_life + $number_less_than_1;
  75. Putting a number into a string
  76. print "$name says, 'The meaning of life is $sum.'";
  77. my $price = '$9.95';
  78. Double quotes do
  79. my $invline = "24 widgets @ $price each";
  80. Use a backslash to escape special characters in double quoted strings
  81. print "He said amp;quot;The price is 300amp;quot;";
  82. print "He said amp;quot;The price is 300amp;quot;";
  83. This is a tidier alternative
  84. print qq(He said "The price is 300");
  85. Also works for single quotes
  86. print q(He said "That's too expensive");
  87. Test for it with defined() function
  88. if (defined($my_var)) { ... }
  89. my @fruit = ('apples', 'oranges', 'guavas', 'passionfruit', 'grapes');
  90. my @magic_numbers = (23, 42, 69);
  91. my @random_scalars = ('mumble', 123.45, 'dave cross', -300, $name);
  92. print $fruits[0]; # prints "apples"
  93. Note: Indexes start from zero
  94. print $random_scalars[2]; # prints "dave cross"
  95. Note use of $ as individual element of an array is a scalar
  96. print @fruits[0,2,4]; # prints "apples", "guavas", # "grapes"
  97. print @fruits[1 .. 3]; # prints "oranges", "guavas", # "passionfruit"
  98. Note use of @ as we are accessing more than one element of the array
  99. $array[400] = 'something else';
  100. Also with slices
  101. @array[4, 7 .. 9] = ('four', 'seven', 'eight', 'nine');
  102. @array[1, 2] = @array[2, 1];
  103. Therefore $#array + 1 is the number of elements
  104. $count = @array;
  105. Does the same thing and is easier to understand
  106. Initialised with a list
  107. %french = ('one', 'un', 'two', 'deux', 'three', 'trois');
  108. “fat comma” (=>) is easier to understand
  109. %german = (one => 'ein', two => 'zwei', three => 'drei');
  110. print $german{two};
  111. As with arrays, notice the use of $ to indicate that we're accessing a single value
  112. Returns a list of elements from a hash
  113. print @french{'one','two','three'}; # prints "un", "deux" & "trois"
  114. Again, note use of @ as we are accessing more than one value from the hash
  115. $hash{bar} = 'something else';
  116. Also with slices
  117. @hash{'foo', 'bar'} = ('something', 'else');
  118. @hash{'foo', 'bar'} = @hash{'bar', 'foo'};
  119. There is no equivalent to $#array
  120. print %hash is unhelpful
  121. We'll see ways round these restrictions later
  122. Many of them have punctuation marks as names
  123. Others have names in ALL_CAPS
  124. They are documented in perlvar
  125. print; # prints the value of $_
  126. If a piece of Perl code seems to be missing a variable, then it's probably using $_
  127. Think of “it” or “that” in English
  128. Three uses of $_
  129. Contains your programs command line arguments
  130. my $num = @ARGV; print "$num arguments: @ARGV";
  131. perl printargs.pl foo bar baz
  132. Contains the environment variables that your script has access to.
  133. Keys are the variable names
  134. Values are the… well… values!
  135. print $ENV{PATH};
  136. Input and Output
  137. We'll see more input and output methods later in the day
  138. But here are some simple methods
  139. print “Hello world”;
  140. $input = <STDIN>;
  141. < ... > is the “read from filehandle” operator
  142. STDIN is the standard input filehandle
  143. Operators and Functions
  144. “Things” that do “stuff”
  145. Routines built into Perl to manipulate data
  146. Less standard operations modulus (%), exponentiation (**)‏
  147. $speed = $distance / $time;
  148. $vol = $length * $breadth * $height;
  149. $area = $pi * ($radius ** 2);
  150. $odd = $number % 2;
  151. Can be abbreviated to $total += $amount;
  152. Even shorter $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1
  153. Subtle difference between $x++ and ++$x
  154. $name = $firstname . ' ' . $surname;
  155. Repetition (x)
  156. $line = '-' x 80; $police = 'hello ' x 3;
  157. Shortcut versions available
  158. $page .= $line; # $page = $page . $line
  159. $thing x= $i; # $thing = $thing x $i
  160. -e $file does the file exist
  161. -r $file is the file readable
  162. -w $file is the file writeable
  163. -d $file is the file a directory
  164. -f $file is the file a normal file
  165. -T $file is a text file
  166. -B $file is a binary file
  167. Can take more arguments than operators
  168. Arguments follow the function name
  169. See perlfunc for a complete list of Perl's built-in functions
  170. $age = 29.75; $years = int($age);
  171. @list = ('a', 'random', 'collection', 'of', 'words'); @sorted = sort(@list); # a collection of random words
  172. $len = length $a_string;
  173. uc and lc return upper and lower case versions of a string
  174. $string = 'MiXeD CaSe'; print &quot;$string&quot;, uc $string, &quot;&quot;, lc $string;
  175. See also ucfirst and lcfirst
  176. $word = 'word'; $letter = chop $word;
  177. chomp removes the last character only if it is a newline and returns true or false appropriately
  178. $string = 'Hello world'; print substr($string, 0, 5); # prints 'Hello'
  179. Unlike many other languages you can assign to a substring
  180. substr($string, 0, 5) = 'Greetings'; print $string; # prints 'Greetings world'
  181. cos , sin , tan standard trigonometric functions
  182. exp exponentiation using e
  183. log logarithm to base e
  184. rand returns a random number
  185. sqrt returns the square root
  186. push @array, $value;
  187. pop removes and returns the last element in an array
  188. $value = pop @array;
  189. shift and unshift do the same for the start of an array
  190. @sorted = sort @array;
  191. sort does a lot more besides, see the docs (perldoc -f sort)‏
  192. reverse returns a reversed list
  193. @reverse = reverse @array;
  194. @array = (1 .. 5); $string = join ', ', @array; # $string is '1, 2, 3, 4, 5'
  195. split takes a string and converts it into an array
  196. $string = '1~2~3~4~5'; @array = split(/~/, $string); # @array is (1, 2, 3, 4, 5)‏
  197. exists tells you if an element exists in a hash
  198. keys returns a list of all the keys in a hash
  199. values returns a list of all the values in a hash
  200. open(my $file, '<', 'in.dat');
  201. You can then read the file with <$file>
  202. $line = <$file>; # one line
  203. @lines = <$file>; # all lines
  204. Finally, close the file with close
  205. close($file);
  206. $bytes = read(FILE, $buffer, 1024);
  207. seek to move to a random position in a file
  208. seek(FILE, 0, 0);
  209. tell to get current file position
  210. $where = tell FILE;
  211. truncate to truncate file to given size
  212. truncate FILE, $where;
  213. open my $file, '>', 'out.dat'; # overwrite
  214. open my $file, '>>', 'out.dat'; # append
  215. Write to file using print
  216. print $file “some data”;
  217. Note lack of comma
  218. $now = time;
  219. localtime converts that into more usable values
  220. ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($now);
  221. $year is years since 1900
  222. $wday is 0 (Sun) to 6 (Sat)‏
  223. @time_bits = localtime(time);
  224. Call to time can be omitted
  225. @time_bits = localtime;
  226. Use array slices
  227. ($d, $m, $y) = (localtime)[3 .. 5];
  228. Easier to use strftime (from POSIX.pm)
  229. print strftime('%Y-%m-%d', localtime);
  230. Format followed by list of date/time values
  231. Conditional Constructs
  232. This makes for far more interesting programs
  233. The unit of program execution is a block of code
  234. Blocks are delimited with braces { … }
  235. But what is truth?
  236. '' (the empty string)‏
  237. undef (an undefined value)‏
  238. $x >= $y or $x ge $y
  239. Also < ( lt ) and <= ( le )‏
  240. '0' == (3 * 2) - 6 # true
  241. 'apple' gt 'banana' # false
  242. 'apple' == 'banana' # true(!)‏
  243. 1 + 2 == '3 bears' # true
  244. 1 + 3 == 'three' # false
  245. Different precedence though
  246. Only need to evaluate EXPR_2 if EXPR_1 evaluates as false
  247. We can use this to make code easier to follow
  248. open FILE, 'something.dat' or die &quot;Can't open file: $!&quot;;
  249. @ARGV == 2 or print $usage_msg;
  250. if (EXPR) { BLOCK }
  251. Only executes BLOCK if EXPR is true
  252. if ($name eq 'Doctor') { regenerate(); }
  253. if (EXPR) { BLOCK1 } else { BLOCK2 }
  254. If EXPR is true, execute BLOCK1, otherwise execute BLOCK2
  255. if ($name eq 'Doctor') { regenerate(); } else { die &quot;Game over!&quot;; }
  256. if (EXPR1) { BLOCK1 } elsif (EXPR2) { BLOCK2 } else { BLOCK3 }
  257. If EXPR1 is true, execute BLOCK1 else if EXPR2 is true, execute BLOCK2 otherwise execute BLOCK3
  258. if ($name eq 'Doctor') { regenerate(); } elsif ($tardis_location eq $here) { escape(); } else { die &quot;Game over!&quot;; }
  259. while (EXPR) { BLOCK }
  260. Repeat BLOCK while EXPR is true
  261. while ($dalek_prisoners) { print &quot;Ex-ter-min-ate&quot;; $dalek_prisoners--; }
  262. until (EXPR) { BLOCK }
  263. Execute BLOCK until EXPR is true
  264. until ($regenerations == 12) { print &quot;Regenerating&quot;; regenerate(); $regenerations++; }
  265. for (INIT; EXPR; INCR) { BLOCK }
  266. Like C
  267. Execute INIT If EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR
  268. for ($i = 1; $i <= 10; $i++) { print &quot;$i squared is &quot;, $i * $i, &quot;&quot;; }
  269. Used surprisingly rarely
  270. foreach VAR (LIST) { BLOCK }
  271. For each element of LIST, set VAR to equal the element and execute BLOCK
  272. foreach $i (1 .. 10) { print &quot;$i squared is &quot;, $i * $i, &quot;&quot;; }
  273. my %months = (Jan => 31, Feb => 28, Mar => 31, Apr => 30, May => 31, Jun => 30, … ); foreach (keys %months) { print &quot;$_ has $months{$_} days&quot;; }
  274. while (<STDIN>) { print; }
  275. This is the same as
  276. while (defined($_ = <STDIN>)) { print $_; }
  277. last – jump out of loop
  278. redo – jump to start of same iteration of loop
  279. Subroutines
  280. Make it easy to repeat code
  281. Subroutines have a name and a block of code
  282. sub NAME { BLOCK }
  283. exterminate();
  284. exterminate;
  285. last one only works if function has been predeclared
  286. exterminate('The Doctor');
  287. Arguments end up in the @_ array within the function
  288. sub exterminate { my ($name) = @_; print &quot;Ex-Ter-Min-Ate $name&quot;; $timelords--; }
  289. sub exterminate { foreach (@_) { print &quot;Ex-Ter-Min-Ate $_&quot;; $timelords--; } }
  290. &my_sub passes on the contents of @_ to the called subroutine
  291. sub first { &second }; sub second { print @_ }; first('some', 'random', 'data');
  292. You usually don't want to do that
  293. Passing by reference passes the actual variable. Changing the argument alters the external value
  294. Perl allows you to choose
  295. my ($arg1, $arg2) = @_;
  296. Updating $arg1 and $arg2 doesn’t effect anything outside the subroutine
  297. Simulating pass by reference
  298. $_[0] = 'whatever';
  299. Updating the contents of @_ updates the external values
  300. sub exterminate { if (rand > .25) { print &quot;Ex-Ter-Min-Ate $_[0]&quot;; $timelords--; return 1; } else { return; } }
  301. sub exterminate { my @exterminated; foreach (@_) { if (rand > .25) { print &quot;Ex-Ter-Min-Ate $_&quot;; $timelords--; push @exterminated, $_; } } return @exterminated; }
  302. Regular Expressions
  303. A bit like wild-cards
  304. Sometimes overused!
  305. Documented in perldoc perlre
  306. Works on $_ by default
  307. In scalar context returns true if the match succeeds
  308. In list context returns list of &quot;captured&quot; text
  309. m is optional if you use / characters
  310. With m you can use any delimiters
  311. Works on $_ by default
  312. In scalar context returns true if substitution succeeds
  313. In list context returns number of replacements
  314. Can choose any delimiter
  315. $name =~ s/Dave/David/;
  316. ^ - matches start of string
  317. $ - matches end of string
  318. . - matches any character (except )‏
  319. - matches a whitespace character
  320. - matches a non-whitespace character
  321. - matches any non-digit
  322. - matches any &quot;word&quot; character
  323. - matches any &quot;non-word&quot; character
  324. - matches a word boundary
  325. - matches anywhere except a word boundary
  326. ? - match zero or one
  327. * - match zero or more
  328. + - match one or more
  329. {n} - match exactly n
  330. {n,} - match n or more
  331. {n,m} - match between n and m
  332. /[aeiou]/ # match any vowel
  333. Use - to define a contiguous range
  334. /[A-Z]/ # match upper case letters
  335. Use ^ to match inverse set
  336. /[^A-Za-z] # match non-letters
  337. /rose|martha|donna/i;
  338. Use parentheses for grouping
  339. /^(rose|martha|donna)$/i;
  340. The captured parts are in $1 , $2 , etc…
  341. while (<FILE>) { if (/^(+)+(+)/) { print &quot;The first word was $1&quot;; print &quot;The second word was $2&quot;; } }
  342. my @nums = $text =~ /(+)/g; print &quot;I found these integers:&quot;; print &quot;@nums&quot;;
  343. perldoc perlretut
  344. Mastering Regular Expressions – Jeffrey Freidl
  345. Smart Matching
  346. Powerful matching operator
  347. DWIM
  348. Examines operands
  349. Decides which match to apply
  350. New operator
  351. Looks a bit like the binding operator ( =~ )
  352. Can be used in place of it
  353. $some_text =~ /some regex/
  354. Can be replaced with
  355. $some_text ~~ /some regex/
  356. ~~ does a regex match
  357. Cleverer than that though
  358. %hash ~~ /regex/
  359. Regex match on hash keys
  360. @array ~~ /regex/
  361. Regex match on array elements
  362. Checks that arrays are the same
  363. $scalar ~~ @array
  364. Checks scalar exists in array
  365. $scalar ~~ %hash
  366. Checks scalar is a hash key
  367. $scalar1 ~~ $scalar2
  368. It depends
  369. If both look like numbers
  370. ~~ acts like ==
  371. Otherwise
  372. ~~ acts like eq
  373. Finding and Using Modules
  374. Perl comes with over 100 modules (see “perldoc perlmodlib” for list)
  375. http://search.cpan.org
  376. distribution name
  377. You can have your own personal module library
  378. CPANPLUS.pm is included with newer Perls
  379. Automatically carries out installation process
  380. Can also handle required modules
  381. Or
  382. cpanp -i Some::Module
  383. Object modules usually don't
  384. Difference not clear cut (e.g. CGI.pm)
  385. use My::Module;
  386. Import optional components:
  387. use My::Module qw(my_sub @my_arr);
  388. Import defined sets of components:
  389. use My:Module qw(:advanced);
  390. Use imported components:
  391. $data = my_sub(@my_arr);
  392. use My::Object;
  393. Create an object:
  394. $obj->set_name($name);
  395. Time::Local
  396. Text::ParseWords
  397. Getopt::Std
  398. Cwd
  399. POSIX
  400. CGI
  401. Carp
  402. Benchmark
  403. Data::Dumper
  404. DBI
  405. DBIx::Class
  406. DateTime
  407. HTML::Parser
  408. WWW::Mechanize
  409. Email::Simple
  410. XML::LibXML
  411. XML::Feed
  412. Moose
  413. See you tomorrow
Advertisement