what is perlflossuk 23rd february 2012 schedule 09:45 – begin 11:15 – coffee break (15 mins)...
TRANSCRIPT
![Page 1: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/1.jpg)
FlossUK23rd February 2012
Schedule
09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 – End
![Page 2: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/2.jpg)
FlossUK23rd February 2012
Resources
Slides available on-line− http://mag-sol.com/train/public/2012-02/ukuug
Also see Slideshare− http://www.slideshare.net/davorg/slideshows
Get Satisfaction− http://getsatisfaction.com/magnum
![Page 3: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/3.jpg)
FlossUK23rd February 2012
What We Will Cover
Modern Core Perl− What's new in Perl 5.10, 5.12 & 5.14
Advanced Testing Database access with DBIx::Class Handling Exceptions
![Page 4: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/4.jpg)
FlossUK23rd February 2012
What We Will Cover
Profiling and Benchmarking Object oriented programming with Moose MVC Frameworks
− Catalyst
PSGI and Plack
![Page 5: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/5.jpg)
Modern Core PerlModern Core Perl
![Page 6: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/6.jpg)
FlossUK23rd February 2012
Perl Releases Perl 5 has moved to a regular release cycle Major release every year
− In Spring
Minor releases when required
![Page 7: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/7.jpg)
FlossUK23rd February 2012
Perl Version Numbers Even major numbers are production releases
− 5.10, 5.12, 5.14, 5.16
Odd major numbers are dev releases− 5.9, 5.11, 5.13, 5.15
![Page 8: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/8.jpg)
FlossUK23rd February 2012
Perl Support p5p provide support for current and previous
major releases− Currently 5.12 and 5.14
Further support may be available from distributors
![Page 9: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/9.jpg)
FlossUK23rd February 2012
Recent Perl Releases 5.10.0 – 2007 Dec 18 5.10.1 – 2009 Aug 22 5.12.0 – 2010 Apr 12 5.12.1 – 2010 May 16 5.12.2 – 2010 Sep 6 5.12.3 – 2011 Jan 21
![Page 10: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/10.jpg)
FlossUK23rd February 2012
Recent Perl Releases 5.14.0 – 2011 May 14 5.14.1 – 2011 Jun 16 5.12.4 – 2011 Jun 20 5.14.2 – 2011 Sep 26
![Page 11: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/11.jpg)
FlossUK23rd February 2012
Perl 5.10
Released 18th Dec 2007− Perl's 20th birthday
Many new features Well worth upgrading
![Page 12: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/12.jpg)
FlossUK23rd February 2012
New Features Defined-or operator Switch operator Smart matching say() Lexical $_
![Page 13: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/13.jpg)
FlossUK23rd February 2012
New Features
State variables Stacked file tests Regex improvements Many more
![Page 14: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/14.jpg)
FlossUK23rd February 2012
Defined Or Boolean expressions “short-circuit” $val = $val || $default;
$val ||= $default;
What if 0 is a valid value?
![Page 15: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/15.jpg)
FlossUK23rd February 2012
Defined Or Need to check “definedness” $val = defined $val ? $val : $default;
$val = $default unless defined $val;
![Page 16: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/16.jpg)
FlossUK23rd February 2012
Defined Or
The defined or operator makes this easier $val = $val // $default;
A different slant on truth Checks definedness Shortcircuit version too $val //= $value;
![Page 17: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/17.jpg)
FlossUK23rd February 2012
Switch Statement
Switch.pm was added with Perl 5.8 Source filter Parser limitations
− Regular expressions
− eval
5.10 introduces a build-in switch statement
![Page 18: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/18.jpg)
FlossUK23rd February 2012
Given ... When
Switch is spelled “given” Case is spelled “when” Powerful matching syntax
![Page 19: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/19.jpg)
FlossUK23rd February 2012
Given Example given ($foo) { when (/^abc/) { $abc = 1; } when (/^def/) { $def = 1; } when (/^xyz/) { $xyz = 1; } default { $nothing = 1; }}
![Page 20: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/20.jpg)
FlossUK23rd February 2012
New Keywords Four new keywords
− given
− when
− default
− continue
![Page 21: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/21.jpg)
FlossUK23rd February 2012
given
given(EXPR)
Assigns the result of EXPR to $_ within the following block
Similar to do { my $_ = EXPR; ... }
![Page 22: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/22.jpg)
FlossUK23rd February 2012
when
when (EXPR)
Uses smart matching to compare $_ with EXPR
Equivalent to when ($_ ~~ EXPR)
~~ is the new smart match operator Compares two values and “does the right
thing”
![Page 23: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/23.jpg)
FlossUK23rd February 2012
default
default defines a block that is executed if no when blocks match
default block is optional
![Page 24: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/24.jpg)
FlossUK23rd February 2012
continue
continue keyword falls through to the next when block
Normal behaviour is to break out of given block once the first when condition is matched
Inverse of most other programming languages
![Page 25: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/25.jpg)
FlossUK23rd February 2012
continue given($foo) { when (/x/) { say '$foo contains an x'; continue } when (/y/) { say '$foo contains a y' } default { say '$foo contains no x or y' }}
![Page 26: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/26.jpg)
FlossUK23rd February 2012
Smart Matching
~~ is the new Smart Match operator Different kinds of matches Dependent on the types of the operands See “perldoc perlsyn” for the full details Warning: Still under discussion
![Page 27: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/27.jpg)
FlossUK23rd February 2012
Smart Match Examples $foo ~~ $bar; # == or cmp
$foo ~~ @bar; # array contains value
$foo ~~ %bar; # hash key exists
$foo ~~ qr{$bar}; # regex match
@foo ~~ @bar; # arrays are identical
%foo ~~ %bar; # hash keys match
Many more alternatives
![Page 28: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/28.jpg)
FlossUK23rd February 2012
say()
say() is a new alternative to print() Adds a new line at the end of each call say($foo); # print $foo, “\n”;
Two characters shorter than print Less typing
![Page 29: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/29.jpg)
FlossUK23rd February 2012
Lexical $_
$_ is a package variable Always exists in main package Can lead to subtle bugs when not localised
correctly
Can now use my $_ to create a lexically scoped variable called $_
![Page 30: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/30.jpg)
FlossUK23rd February 2012
State Variables
Lexical variables disappear when their scope is destroyed
sub variables { my $x;
say ++$x;}
variables() for 1 .. 3;
![Page 31: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/31.jpg)
FlossUK23rd February 2012
State Variables State variables retain their value when their
scope is destroyed sub variables { state $x;
say ++$x;}
variables() for 1 .. 3;
![Page 32: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/32.jpg)
FlossUK23rd February 2012
State Variables Like static variables in C Deprecating bugs
− my $x if 0;
![Page 33: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/33.jpg)
FlossUK23rd February 2012
Stacked File Tests
People often think you can do this -f -w -x $file
Previously you couldn't Now you can Equivalent to -x $file && -w _ && -f _
![Page 34: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/34.jpg)
FlossUK23rd February 2012
Regex Improvements
Plenty of regular expression improvements Named capture buffers Possessive quantifiers Relative backreferences New escape sequences Many more
![Page 35: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/35.jpg)
FlossUK23rd February 2012
Named Capture Buffers
Variables $1, $2, etc change if the regex is altered
Named captures retain their names
(?<name> ... ) to define
Use new %+ hash to access them
![Page 36: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/36.jpg)
FlossUK23rd February 2012
Named Capture Example
while (<DATA>) { if (/(?<header>[\w\s]+) :\s+(?<value>.+)/x) { print "$+{header} -> "; print "$+{value}\n"; }}
![Page 37: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/37.jpg)
FlossUK23rd February 2012
Possessive Quantifiers
?+, *+, ++
Grab as much as they can Never give it back Finer control over backtracking 'aaaa' =~ /a++a/
− Never matches
Rick Astley quantifier
![Page 38: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/38.jpg)
FlossUK23rd February 2012
Relative Backreferences
\g{N}
More powerful version of \1, \2, etc
\g{1} is the same as \1
\g{-1} is the last capture buffer
\g{-2} is the one before that
![Page 39: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/39.jpg)
FlossUK23rd February 2012
New Escape Sequences
\h – Horizontal white space
\v – Vertical white space
Also \H and \V
![Page 40: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/40.jpg)
FlossUK23rd February 2012
Accessing New Features Some new features would break backwards
compatibility They are therefore turned off by default Various ways to turn them on
![Page 41: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/41.jpg)
FlossUK23rd February 2012
Feature Pragma Turn new features on with the feature
pragma use feature 'say';
use feature 'switch';
use feature 'state';
use feature ':5.10';
![Page 42: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/42.jpg)
FlossUK23rd February 2012
Implicit Loading
Two ways to automatically turn on 5.10 features
Require a high enough version of Perl use 5.10.0; # Or higher
-E command line option perl -e 'say “hello”'
perl -E 'say “hello”'
![Page 43: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/43.jpg)
FlossUK23rd February 2012
Perl 5.12
Released 12 April 2010− 5.12.4 20 June 2011
Many new enhancements
![Page 44: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/44.jpg)
FlossUK23rd February 2012
5.12 Enhancements package NAME VERSION syntax ... operator Implicit strictures Y2038 compliance
![Page 45: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/45.jpg)
FlossUK23rd February 2012
5.12 Enhancements Smart match changes New modules
− autodie
− parent
![Page 46: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/46.jpg)
FlossUK23rd February 2012
package NAME VER Declare the version of a package in the
package declaration package My::Package 1.23;
Equivalent to package My::Package;our $VERSION = 1.23;
![Page 47: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/47.jpg)
FlossUK23rd February 2012
... Operator Called the “yada-yada” operator Used to stand in for unwritten code sub unimplemented { ...}
Code compiles Throws an “unimplemented” exception
when run
![Page 48: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/48.jpg)
FlossUK23rd February 2012
Implicit Strictures Requiring a version of Perl greater than 5.11
implicitly turns on use strict use 5.12.0;
Equivalent to use strict;use feature ':5.12';
![Page 49: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/49.jpg)
FlossUK23rd February 2012
Y2038 Compliance Core time functions are now Y2038
compliant
![Page 50: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/50.jpg)
FlossUK23rd February 2012
Smart Match Changes Some changes to Smart Match operator No longer commutative See new table in perlsyn Still in flux!
![Page 51: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/51.jpg)
FlossUK23rd February 2012
New Modules Some new modules in the standard
distribution autodie parent
− Better version of base.
![Page 52: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/52.jpg)
FlossUK23rd February 2012
Perl 5.14
Released 14 May 2011− 5.14.2 26 Sept 2011
Many new enhancements
![Page 53: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/53.jpg)
FlossUK23rd February 2012
5.14 Enhancements
Non-destructive substitution Container functions accept references Package block New modules
![Page 54: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/54.jpg)
FlossUK23rd February 2012
Non-destructive substitution
New /r option on s/// and tr/// Copies input Acts on copy Original unmodifed $_ = 'cat';$new = s/cat/dog/r'; # $_ remains 'cat'
![Page 55: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/55.jpg)
FlossUK23rd February 2012
Container functions accept references
Array & hash functions used to require arrays or hashes
− push @array, $value
− @keys = keys %hash
Even if you have a reference− push @$arrayref, $value
− @keys = keys %$hashref
![Page 56: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/56.jpg)
FlossUK23rd February 2012
Container functions accept references
Array & hash functions now accept references
− push $array_ref, $value
− @keys = keys $hash_ref
Currently experimental
![Page 57: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/57.jpg)
FlossUK23rd February 2012
Package block Attach a code block to a package declaration package MyPackage { ... }
Equivalent to { package MyPackage; ... }
Can also declare a version package MyPackage 1.23 { ... }
![Page 58: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/58.jpg)
FlossUK23rd February 2012
New Modules Many modules for parsing META files CPAN::Meta::YAML & JSON::PP CPAN::Meta CPAN::Meta::Spec & CPAN::Meta::History Module::Metadata
![Page 59: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/59.jpg)
FlossUK23rd February 2012
New Modules Other new modules HTTP::Tiny Perl::OSType Version::Requirements
![Page 60: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/60.jpg)
FlossUK23rd February 2012
Perl 5.16 Due in spring 2012 Currently in development at 5.15
− 5.15.7 – 2012 Jan 20
− Code freeze – 2011 Dec 20
![Page 61: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/61.jpg)
FlossUK23rd February 2012
Perl 5.16 Look for changes in perldelta
− perldelta5150
− perldelta5151
− etc...
![Page 62: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/62.jpg)
FlossUK23rd February 2012
Some Highlights CORE on all keywords Continue outside switch Breakpoints with filenames Remove Perl 4 *.pl
![Page 63: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/63.jpg)
FlossUK23rd February 2012
More Information perldoc perl5100delta perldoc perl5120delta perldoc perl5140delta
![Page 64: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/64.jpg)
FlossUK23rd February 2012
Core Perl Examples Write a program that uses at least three or
four of the new features
![Page 65: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/65.jpg)
AdvancedAdvancedTestingTesting
![Page 66: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/66.jpg)
FlossUK23rd February 2012
Writing Test Modules Standard test modules all work together Built using Test::Builder Ensures that test modules all use the same
framework Use it as the basis of your own Test::*
modules Test your Test::Builder test modules with
Test::Builder::Tester
![Page 67: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/67.jpg)
FlossUK23rd February 2012
Test::Between package Test::Between;
use strict;use warnings;
use base 'Exporter';our @EXPORT = qw(is_between);
use Test::Builder;
my $test = Test::Builder->new;
![Page 68: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/68.jpg)
FlossUK23rd February 2012
Test::Between sub is_between { my ($item, $lower, $upper, $desc) = @_;
return ($test->ok($lower le $item && $item le $upper, $desc) || $test->diag("$item is not between $lower and $upper"));}
1;
![Page 69: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/69.jpg)
FlossUK23rd February 2012
Using Test::Between #!/usr/bin/perluse strict;use warnings;use Test::More tests => 3;use Test::Between;
is_between('b', 'a', 'c', 'alpha');is_between( 2, 1, 3, 'numeric');is_between('two',1, 3, 'wrong');
![Page 70: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/70.jpg)
FlossUK23rd February 2012
Test::Between Output $ prove -v test.pltest.pl .. 1..3ok 1 – alphaok 2 – numericnot ok 3 – wrong
# Failed test 'wrong'# at test.pl line 11.# two is not between 1 and 3# Looks like you failed 1 test of 3.Dubious, test returned 1 (wstat 256, 0x100)Failed 1/3 subtests
![Page 71: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/71.jpg)
FlossUK23rd February 2012
Test::Between Output Test Summary Report-------------------test.pl (Wstat: 256 Tests: 3 Failed: 1) Failed test: 3 Non-zero exit status: 1Files=1, Tests=3, 1 wallclock secs ( 0.07 usr 0.01 sys + 0.05 cusr 0.01 csys = 0.14 CPU)Result: FAIL
![Page 72: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/72.jpg)
FlossUK23rd February 2012
Mocking Objects
Sometimes it's hard to test external interfaces
Fake them Test::MockObject pretends to be other
objects Gives you complete control over what they
return
![Page 73: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/73.jpg)
FlossUK23rd February 2012
Testing Reactors
You're writing code that monitors a nuclear reactor
It's important that your code reacts correctly when the reactor overheats
You don't have a reactor in the test environment
![Page 74: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/74.jpg)
FlossUK23rd February 2012
Testing Reactors
Even if you did, you wouldn't want to make it overheat every time you run the tests
Especially if you're not 100% sure of your code
Of if you're running unattended smoke tests Fake it with a mock object
![Page 75: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/75.jpg)
FlossUK23rd February 2012
My::Monitor Spec
If the temperature of a reactor is over 100 then try to cool it down
If you have tried cooling a reactor down 5 times and the temperature is still over 100 then return an error
![Page 76: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/76.jpg)
FlossUK23rd February 2012
My::Monitor Code package My::Monitor;
sub new { my $class = shift; my $self = { tries => 0 };
return bless $self, $class;}
![Page 77: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/77.jpg)
FlossUK23rd February 2012
My::Monitor Code sub check { my $self = shift; my $reactor = shift;
my $temp = $reactor->temperature;
if ($temp > 100) { $reactor->cooldown; ++$self->{tries}; if ($self->{tries} > 5) { return; } return 1;
![Page 78: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/78.jpg)
FlossUK23rd February 2012
My::Monitor Code } else { $self->{tries} = 0; return 1; }}
1;
![Page 79: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/79.jpg)
FlossUK23rd February 2012
Mock Reactor
Create a mock reactor object that acts exactly how we want it to
Reactor object has two interesting methods temperature - returns the current temperature cooldown - cools reactor and returns success
or failure
![Page 80: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/80.jpg)
FlossUK23rd February 2012
monitor.t use Test::More tests => 10;
use Test::MockObject;
# Standard tests
BEGIN { use_ok('My::Monitor'); }
ok(my $mon = My::Monitor->new);isa_ok($mon, 'My::Monitor');
![Page 81: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/81.jpg)
FlossUK23rd February 2012
monitor.t # Create Mock Reactor Object
my $t = 10;my $reactor = Test::MockObject;
$reactor->set_bound('temperature', \$t);
$reactor->set_true('cooldown');
![Page 82: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/82.jpg)
FlossUK23rd February 2012
monitor.t # Test reactor
ok($mon->check($reactor));
$t = 120;
ok($mon->check($reactor)) for 1 .. 5;
ok(!$mon->check($reactor));
![Page 83: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/83.jpg)
FlossUK23rd February 2012
How Good Are Your Tests? How much of your code is exercised by your
tests? Devel::Cover can help you to find out Deep internal magic Draws pretty charts
− HARNESS_PERL_SWITCHES= -MDevel::Cover make test
− cover
![Page 84: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/84.jpg)
FlossUK23rd February 2012
Devel::Cover Output
![Page 85: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/85.jpg)
FlossUK23rd February 2012
Devel::Cover Output
![Page 86: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/86.jpg)
FlossUK23rd February 2012
Devel::Cover Output
![Page 87: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/87.jpg)
FlossUK23rd February 2012
Alternative Test Paradigms Not everyone likes the Perl testing
framework Other frameworks are available Test::Class
− xUnit style framework
Test::FIT− Framework for Interactive Testing
− http://fit.c2.com
![Page 88: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/88.jpg)
FlossUK23rd February 2012
More Information Perl Testing: A Developer's Notebook (Ian
Langworth & chromatic) perldoc Test::MockObject perldoc Test::Builder Devel::Cover etc...
![Page 89: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/89.jpg)
FlossUK23rd February 2012
Testing Examples
Write a test plan for the supplied Perl module
Do you need to mock any of the interfaces? How good is the coverage of your tests?
![Page 90: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/90.jpg)
Object Relational Object Relational
MappingMapping
![Page 91: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/91.jpg)
FlossUK23rd February 2012
ORM
Mapping database relations into objects Tables (relations) map onto classes Rows (tuples) map onto objects Columns (attributes) map onto attributes Don't write SQL
![Page 92: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/92.jpg)
FlossUK23rd February 2012
SQL Is Tedious
Select the id and name from this table Select all the details of this row Select something about related tables Update this row with these values Insert a new record with these values Delete this record
![Page 93: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/93.jpg)
FlossUK23rd February 2012
Replacing SQL
Instead of SELECT *FROM my_tableWHERE my_id = 10
and then dealing with the prepare/execute/fetch code
![Page 94: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/94.jpg)
FlossUK23rd February 2012
Replacing SQL
We can write use My::Object;
# warning! not a real ormmy $obj = My::Object->retrieve(10)
Or something similar
![Page 95: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/95.jpg)
FlossUK23rd February 2012
Writing An ORM Layer
Not actually that hard to do yourself Each class needs an associated table Each class needs a list of columns Create simple SQL for basic CRUD
operations Don't do that
![Page 96: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/96.jpg)
FlossUK23rd February 2012
Perl ORM Options
Plenty of choices on CPAN Fey::ORM Rose::DB Class::DBI DBIx::Class
− The current favourite
![Page 97: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/97.jpg)
FlossUK23rd February 2012
DBIx::Class
Standing on the shoulders of giants Learning from problems in Class::DBI More flexible More powerful
![Page 98: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/98.jpg)
FlossUK23rd February 2012
DBIx::Class Example
Modeling a CD collection Three tables artist (artistid, name) cd (cdid, artist, title) track (trackid, cd, title)
![Page 99: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/99.jpg)
FlossUK23rd February 2012
Main Schema
Define main schema class DB/Main.pm package DB::Main;use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes();
1;
![Page 100: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/100.jpg)
FlossUK23rd February 2012
Object Classes
DB/Main/Artist.pm package DB::Main::Artist;use base qw/DBIx::Class/;__PACKAGE__->load_components(qw/PK::Auto Core/);__PACKAGE__->table('artist');__PACKAGE__->add_columns(qw/ artistid name /);__PACKAGE__->set_primary_key('artistid');__PACKAGE__->has_many(cds => 'DB::Main::Cd');1;
![Page 101: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/101.jpg)
FlossUK23rd February 2012
Object Classes
DB/Main/CD.pm package DB::Main::CD;use base qw/DBIx::Class/;__PACKAGE__->load_components(qw/PK::Auto Core/);__PACKAGE__->table('cd');__PACKAGE__->add_columns(qw/ cdid artist title year /);__PACKAGE__->set_primary_key('cdid');__PACKAGE__->belongs_to(artist => 'DB::Main::Artist');1;
![Page 102: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/102.jpg)
FlossUK23rd February 2012
Inserting Artists
my $schema = DB::Main->connect($dbi_str);
my @artists = ('The Beta Band', 'Beth Orton');
my $art_rs = $schema->resultset('Artist');
foreach (@artists) { $art_rs->create({ name => $_ });}
![Page 103: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/103.jpg)
FlossUK23rd February 2012
Inserting CDs
Hash of Artists and CDs my %cds = ( 'The Three EPs' => 'The Beta Band', 'Trailer Park' => 'Beth Orton');
![Page 104: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/104.jpg)
FlossUK23rd February 2012
Inserting CDs Find each artist and insert CD foreach (keys $cds) { my ($artist) = $art_rs->search( { name => $cds{$_} } );
$artist->add_to_cds({ title => $_, });}
![Page 105: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/105.jpg)
FlossUK23rd February 2012
Retrieving Data
Get CDs by artist my ($artist) = $art_rs->search({ name => 'Beth Orton', });
foreach ($artist->cds) { say $_->title;}
![Page 106: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/106.jpg)
FlossUK23rd February 2012
Searching for Data Search conditions can be more complex Alternatives
− $rs->search({year => 2006}, {year => 2007});
Like− $rs->search({name => { 'like', 'Dav%' }});
![Page 107: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/107.jpg)
FlossUK23rd February 2012
Searching for Data
Combinations− $rs->search({forename => { 'like', 'Dav%' }, surname => 'Cross' });
![Page 108: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/108.jpg)
FlossUK23rd February 2012
Don't Repeat Yourself
There's a problem with this approach Information is repeated Columns and relationships defined in the
database schema Columns and relationships defined in class
definitions
![Page 109: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/109.jpg)
FlossUK23rd February 2012
Repeated Information
CREATE TABLE artist ( artistid INTEGER PRIMARY KEY, name TEXT NOT NULL );
![Page 110: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/110.jpg)
FlossUK23rd February 2012
Repeated Information package DB::Main::Artist;use base qw/DBIx::Class/;__PACKAGE__-> load_components(qw/PK::Auto Core/);__PACKAGE__->table('artist');__PACKAGE__-> add_columns(qw/ artistid name /);__PACKAGE__> set_primary_key('artistid');__PACKAGE__->has_many('cds' => 'DB::Main::Cd');
![Page 111: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/111.jpg)
FlossUK23rd February 2012
Database Metadata
Some people don't put enough metadata in their databases
Just tables and columns No relationships. No constraints You may as well make each column
VARCHAR(255)
![Page 112: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/112.jpg)
FlossUK23rd February 2012
Database Metadata
Describe your data in your database It's what your database is for It's what your database does best
![Page 113: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/113.jpg)
FlossUK23rd February 2012
No Metadata (Excuse 1) "This is the only application that will ever
access this database" Nonsense All data will be shared eventually People will update your database using
other applications Can you guarantee that someone won't use
mysql to update your database?
![Page 114: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/114.jpg)
FlossUK23rd February 2012
No Metadata (Excuse 2) "Database doesn't support those features" Nonsense MySQL 3.x is not a database
− It's a set of data files with a vaguely SQL-like query syntax
MySQL 4.x is a lot better MySQL 5.x is most of the way there Don't be constrained by using inferior tools
![Page 115: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/115.jpg)
FlossUK23rd February 2012
DBIC::Schema::Loader
Creates classes by querying your database metadata
No more repeated data We are now DRY Schema definitions in one place But... Performance problems
![Page 116: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/116.jpg)
FlossUK23rd February 2012
Performance Problems
You don't really want to generate all your class definitions each time your program is run
Need to generate the classes in advance dump_to_dir method Regenerate classes each time schema
changes
![Page 117: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/117.jpg)
FlossUK23rd February 2012
Alternative Approach Need one canonical definition of the data
tables Doesn't need to be SQL DDL Could be in Perl code Write DBIx::Class definitions Generate DDL from those Harder approach
− Might need to generate ALTER TABLE
![Page 118: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/118.jpg)
FlossUK23rd February 2012
Conclusions
ORM is a bridge between relational objects and program objects
Avoid writing SQL in common cases DBIx::Class is the currently fashionable
module Lots of plugins Caveat: ORM may be overkill for simple
programs
![Page 119: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/119.jpg)
FlossUK23rd February 2012
More Information
Manual pages (on CPAN) DBIx::Class DBIx::Class::Manual::* DBIx::Class::Schema::Loader Mailing list (Google for it)
![Page 120: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/120.jpg)
FlossUK23rd February 2012
DBIx::Class Examples
Create the CD database on your computer Use DBIx::Class::Schema::Loader to
generate classes for your database Write code to insert data into the tables Write code to report on the data in the tables
![Page 121: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/121.jpg)
ExceptionExceptionHandlingHandling
![Page 122: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/122.jpg)
FlossUK23rd February 2012
Error Handling How do you handle errors in your code? Return error values from subroutines sub get_object { my ($class, $id) = @_; if (my $obj = find_obj_in_db($id)) { return $obj; } else { return; }}
![Page 123: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/123.jpg)
FlossUK23rd February 2012
Problems
What if someone doesn't check return code? my $obj = MyClass->get_object(100);print $obj->name; # error
Caller assumes that $obj is a valid object Bad things follow
![Page 124: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/124.jpg)
FlossUK23rd February 2012
Basic Exceptions
Throw an exception instead sub get_object { my ($class, $id) = @_; if (my $obj = find_obj_in_db($id)) { return $obj; } else { die “No object found with id: $id”; }}
Now caller has to deal with exceptions
![Page 125: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/125.jpg)
FlossUK23rd February 2012
Dealing with Exceptions
Use eval to catch exceptions my $obj = eval { MyClass->get_object(100)};
if ($@) { # handle exception...} else { print $obj->name; # error}
![Page 126: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/126.jpg)
FlossUK23rd February 2012
Exceptions as Objects
$@ can be set to an object sub get_object { my ($class, $id) = @_; if (my $obj = find_obj_in_db($id)) { return $obj; } else { die MyException->new( type => 'obj_not_found', id => $id, ); }}
![Page 127: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/127.jpg)
FlossUK23rd February 2012
Exception::Class Easy way to define your own exception
objects Define exception hierarchies As recommended in Perl Best Practices
![Page 128: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/128.jpg)
FlossUK23rd February 2012
Define Exceptions use Exception::Class (
'MyException', 'AnotherException' => { isa => 'MyException' }, 'YetAnotherException' => { isa => 'AnotherException', description => 'These exceptions are related to IPC' }, 'ExceptionWithFields' => { isa => 'YetAnotherException', fields => [ 'grandiosity', 'quixotic' ], alias => 'throw_fields', },);
![Page 129: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/129.jpg)
FlossUK23rd February 2012
Using Exceptions eval { MyException->throw( error => 'I feel funny.' ) };
my $e;
if ( $e = Exception::Class->caught('MyException') ) { warn $e->error, "\n", $e->trace->as_string, "\n"; warn join ' ', $e->euid, $e->egid, $e->uid, $e->gid, $e->pid, $e->time; exit;}elsif ( $e = Exception::Class->caught('ExceptionWithFields') ) { $e->quixotic ? do_something_wacky() : do_something_sane();}else { $e = Exception::Class->caught(); ref $e ? $e->rethrow : die $e;}
![Page 130: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/130.jpg)
FlossUK23rd February 2012
Try Catch TryCatch adds “syntactic sugar” try { ...}catch ($e) { ...}
Looks a lot like many other languages
![Page 131: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/131.jpg)
FlossUK23rd February 2012
TryCatch with Scalar try { some_function_that_might_die();}catch ($e) { if ($e =~ /some error/) { # handle error } else { die $e; }}
![Page 132: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/132.jpg)
FlossUK23rd February 2012
TryCatch with Object try { some_function_that_might_die();}catch ($e) { if ($e->type eq 'file') { # handle error } else { die $e; }}
![Page 133: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/133.jpg)
FlossUK23rd February 2012
Better Checks try { some_function_that_might_die();}catch (My::Error $e) { # handle error}
![Page 134: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/134.jpg)
FlossUK23rd February 2012
Even Better Checks try { some_function_that_might_die();}catch (HTTP::Error $e where { $e->code == 404 }) { # handle 404 error}
![Page 135: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/135.jpg)
FlossUK23rd February 2012
Multiple Checks try {
some_function_that_might_die();}catch (HTTP::Error $e where { $e->code == 404 }) { # handle 404 error}catch (HTTP::Error $e where { $e->code == 500 }) { # handle 500 error}catch (HTTP::Error $e) { # handle other HTTP error}catch ($e) { # handle other error}
![Page 136: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/136.jpg)
FlossUK23rd February 2012
More on Exceptions
Exceptions force callers to deal with error conditions
But you have to explicitly code to throw exceptions
open my $fh, '<', 'somefile.txt' or die $!;
What if you forget to check the return value?
![Page 137: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/137.jpg)
FlossUK23rd February 2012
Not Checking Errors
open my $fh, '<', 'somefile.txt';while (<$fh>) { # do something useful}
If the 'open' fails, you can't read any data You might not even get any warnings
![Page 138: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/138.jpg)
FlossUK23rd February 2012
Automatic Exceptions
use Fatal qw(open);
Comes with Perl since 5.003 Errors in built-in functions become fatal
errors This solves our previous problem open my $fh, '<', 'somefile.txt';
![Page 139: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/139.jpg)
FlossUK23rd February 2012
However Fatal.pm has its own problems Unintelligent check for success or failure Checks return value for true/false Assumes false is failure Can't be used if function can legitimately
return a false value− e.g. fork
![Page 140: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/140.jpg)
FlossUK23rd February 2012
Also
Nasty error messages $ perl -MFatal=open -E'open my $fh, "notthere"'Can't open(GLOB(0x86357a4), notthere): No such file or directory at (eval 1) line 4main::__ANON__('GLOB(0x86357a4)', 'notthere') called at -e line 1
![Page 141: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/141.jpg)
FlossUK23rd February 2012
Enter autodie
autodie is a cleverer Fatal In Perl core since 5.10.1 Fatal needed a list of built-ins autodie assumes all built-ins
− use autodie;
![Page 142: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/142.jpg)
FlossUK23rd February 2012
Turning autodie on/off
Lexically scoped− use autodie;
− no autodie;
Turn off for specific built-ins− no autodie 'open';
![Page 143: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/143.jpg)
FlossUK23rd February 2012
Failing Calls
autodie has more intelligence about failing calls
Not just a boolean check Understands fork, system, etc
![Page 144: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/144.jpg)
FlossUK23rd February 2012
Errors are Objects Errors thrown by autodie are objects Can be inspected for details of the error try {
open my $fh, '<', 'not-there';}
catch ($e) { warn 'Error opening ', $e->args->[-1], "\n"; warn 'File: ', $e->file, "\n"; warn 'Function: ', $e->function, "\n"; warn 'Package: ', $e->package, "\n"; warn 'Caller: ', $e->caller, "\n"; warn 'Line: ', $e->line, "\n";}
![Page 145: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/145.jpg)
FlossUK23rd February 2012
Nicer Errors Too $ perl -Mautodie -E'open my $fh, "not-there"'Can't open($fh, 'not-there'): No such file or directory at -e line 1
![Page 146: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/146.jpg)
FlossUK23rd February 2012
More Information perldoc TryCatch
− See also Try::Tiny
perldoc Fatal perldoc autodie autodie - The art of Klingon Programming
− http://perltraining.com.au/tips/2008-08-20.html
![Page 147: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/147.jpg)
FlossUK23rd February 2012
Exceptions Examples
Write a program that relies on autodie to throw exceptions
Write a class which uses Exception::Class to throw exceptions
Write code which uses your class
![Page 148: What is PerlFlossUK 23rd February 2012 Schedule 09:45 – Begin 11:15 – Coffee break (15 mins) 13:00 – Lunch (60 mins) 14:00 – Begin 15:30 – Coffee break (15 mins) 17:00 –](https://reader035.vdocuments.net/reader035/viewer/2022070714/5ed394e67ec1ed47a70d65e7/html5/thumbnails/148.jpg)
That's all folksThat's all folks
• Any questions?