Perl Teach-In <ul><ul><li>A One Day Perl Tutorial </li></ul></ul><ul><ul><li>Dave Cross </li></ul></ul><ul><ul><li>Magnum ...
Object Oriented Perl
Object Oriented Perl <ul><li>How to design objects </li></ul><ul><li>Design Patterns </li></ul><ul><li>Inside-Out Objects ...
What We Won't Cover <ul><li>Writing objects in Perl </li></ul><ul><li>Assume you already know that </li></ul><ul><li>perld...
How To Design Objects <ul><li>Designing Objects is  hard  - OO Barbie </li></ul><ul><li>Very few hard and fast rules </li>...
Prototyping <ul><li>Plan to throw one away; you will, anyhow - Fred Brooks (The Mythical Man Month) </li></ul><ul><li>You ...
Subclassing <ul><li>A good class is easy to subclass </li></ul><ul><li>Two argument bless </li></ul><ul><li>sub new {   my...
Constructor Tip <ul><li>You'll often see code like this </li></ul><ul><li>sub new {   my $thing = shift;   my $class = ref...
Confusing Methods <ul><li>The previous constructor can be called as a class method or an instance method </li></ul><ul><ul...
Separate Methods <ul><li>Class method creates a new, empty, object </li></ul><ul><li>sub new {   my $class = shift;   retu...
Separate Methods <ul><li>Instance method creates a copy of an object </li></ul><ul><li>sub clone {   my $self = shift;   #...
Multiple Constructors <ul><li>In Perl an constructor is just a subroutine </li></ul><ul><li>new  is not a keyword </li></u...
new  is Not a Keyword <ul><li>People coming from other languages often write </li></ul><ul><li>my $obj = new MyObject; </l...
Overriding Methods <ul><li>Subclasses are created so that methods can be overridden </li></ul><ul><ul><li>Or so that new m...
Case Study <ul><li>I needed to create a new graph type for GD::Graph </li></ul><ul><li>Waterfall graph </li></ul><ul><li>B...
Bar Chart
Waterfall Chart
Things to Override <ul><li>Colour choosing </li></ul><ul><ul><li>pick_data_clr </li></ul></ul><ul><li>Legend drawing </li>...
Be Consistent <ul><li>A good object design is consistent </li></ul><ul><li>Similar things act in similar ways </li></ul><u...
Accessor vs Mutator <ul><li>Or &quot;getter vs setter&quot; </li></ul><ul><li>Some people like one method foo </li></ul><u...
Mutator Return Values <ul><li>What does a mutator return? </li></ul><ul><ul><li>The old value </li></ul></ul><ul><ul><li>T...
Don't Always Subclass <ul><li>Not all relationships are &quot;isa_a&quot; </li></ul><ul><li>Sometimes &quot;has_a&quot; is...
Design Patterns <ul><li>Perl does have some design patterns </li></ul><ul><li>Some of them come from  Design Patterns </li...
Factory Object <ul><li>Call constructor on one class </li></ul><ul><li>Constructor works out what is the most appropriate ...
AudioFile::Info <ul><li>There are many Perl modules for reading tags from MP3 files </li></ul><ul><ul><li>MP3::ID3Lib </li...
Using AudioFile::Info <ul><li>AudioFile::Info simplifies reading tag information from audio files </li></ul><ul><li>One sy...
AudioFile::Info->new <ul><li>Works out which kind of file it has been given </li></ul><ul><li>Works out the best installed...
Singleton Pattern <ul><li>Highlander pattern </li></ul><ul><li>“ There can be only one” </li></ul><ul><li>Only ever one in...
Singleton Class <ul><li>package MySingleton; my $single; sub new {   my $class = shift;   unless ($single) {   $single = b...
More Design Patterns <ul><li>See Perl Design Patterns Wiki </li></ul><ul><li>http://perldesignpatterns.com/ </li></ul>
Inside-Out Objects <ul><li>Standard Perl objects are usually based on hashes </li></ul><ul><li>bless { name => 'Dave',   e...
Accessing Attributes <ul><li>$person->{name} = ''; </li></ul><ul><li>Avoids any checks in the mutator method </li></ul><ul...
Adding Attributes <ul><li>No checks on adding entries to the hash </li></ul><ul><li>$person->{nick} = 'davorg'; </li></ul>...
Inside-Out Objects <ul><li>Inside-out objects solve both of these problems </li></ul><ul><li>An object is no longer a hash...
Example <ul><li>package Person; use strict; my %name; my %email; sub new {   my $self = bless {}, shift;   $name{$self} = ...
Example (cont) <ul><li>sub get_name {   my $self = shift;   return $name{$self}; } sub set_name {   my $self = shift;   my...
How It Works <ul><li>The object is still a hash reference </li></ul><ul><li>Still blessed into the correct class </li></ul...
Solving Problems <ul><li>People can access attributes directly </li></ul><ul><ul><li>Attributes are now stored in lexical ...
One Improvement <ul><li>The object is still a blessed hash </li></ul><ul><li>But we never put anything into it </li></ul><...
One New Problem <ul><li>When our objects go out of scope, the blessed scalar ceases to exist </li></ul><ul><li>But the val...
More Stuff <ul><li>Need to handle inheritance </li></ul><ul><li>Automation of inside-out objects </li></ul><ul><ul><li>Cla...
Moose <ul><li>A complete modern object system for Perl 5 </li></ul><ul><li>Based on experiments with Perl 6 object model <...
Moose Example <ul><li>package Point; use Moose; has 'x' => (isa => 'Int',   is  => 'ro'); has 'y' => (isa => 'Int',   is  ...
Understanding Moose <ul><li>There's a lot going on here </li></ul><ul><li>use Moose </li></ul><ul><ul><li>Loads Moose envi...
Creating Attributes <ul><li>has 'x' => (isa => 'Int',   is  => 'ro') </li></ul><ul><ul><li>Creates an attribute called 'x'...
Defining Methods <ul><li>sub clear {   my $self = shift;   $self->{x} = 0;   $self->y(0); } </li></ul><ul><li>Standard met...
Subclassing <ul><li>package Point3D; use Moose; extends 'Point'; has 'z' => (isa => 'Int'); after 'clear' => sub {   my $s...
Subclasses <ul><li>extends 'Point' </li></ul><ul><ul><li>Similar to use base </li></ul></ul><ul><ul><li>Overwrites @ISA in...
Extending Methods <ul><li>after 'clear' => sub {   my $self = shift;   $self->{z} = 0; }; </li></ul><ul><li>New clear meth...
Creating Objects <ul><li>Moose classes are used just like any other Perl class </li></ul><ul><li>$point = Point->new(x => ...
More Moose <ul><li>Only scratching the surface </li></ul><ul><li>Many more options </li></ul><ul><li>Moose is well worth i...
Configuration Files
Configuration Files <ul><li>Can you just add...? </li></ul><ul><li>Users are never satisfied </li></ul><ul><li>Always want...
Configuration in Code <ul><li>This is bad </li></ul><ul><li>#!/usr/bin/perl use strict; use warnings; ## Configuration my ...
Why is This Bad? <ul><li>Don't trust 'em </li></ul><ul><li>Something will get broken </li></ul><ul><li>Code doesn't match ...
Windows INI Files <ul><li>[debugging] debug=1 verbose=0 </li></ul><ul><li>Most users will understand this </li></ul><ul><l...
XML <ul><li><debugging>   <debug>1</debug>   <verbose>0</debug> </debugging> </li></ul><ul><li>or </li></ul><ul><li><debug...
YAML <ul><li>--- debugging:   debug: 1   verbose: 0 </li></ul><ul><li>Looks a bit like INI file </li></ul><ul><li>Human-re...
Swiss Army Config Parser <ul><li>Config::Auto </li></ul><ul><li>&quot;Magical config file parser&quot; </li></ul><ul><li>G...
Other Places <ul><li>Environment variables </li></ul><ul><li>Command line options </li></ul><ul><ul><li>Parse with Getopt:...
Web 2.0 Perl
Web 2.0 Perl <ul><li>Mashups </li></ul><ul><li>Data Feeds </li></ul><ul><li>APIs </li></ul><ul><li>Ajax </li></ul><ul><li>...
Reading Web Feeds <ul><li>A lot of data available as web feeds </li></ul><ul><li>RSS / Atom </li></ul><ul><li>But which ve...
Parsing RSS <ul><li>XML::RSS </li></ul><ul><li>use XML::RSS; my $rss = XML::RSS->new; $rss->parsefile('myfeed.rss'); forea...
Parsing Atom <ul><li>XML::Atom </li></ul><ul><li>use XML::Atom::Feed; my $atom = XML::Atom::Feed->new('myatom.xml'); forea...
Parsing RSS and Atom <ul><li>XML::Feed uses XML::RSS or XML::Atom as appropriate </li></ul><ul><li>Allows code to handle a...
Parsing Broken Feeds <ul><li>Not all XML feeds are valid XML </li></ul><ul><li>This is bad! </li></ul><ul><li>You should c...
Mashing Up Web Feeds <ul><li>Plagger - Pluggable RSS/Atom Aggregator </li></ul><ul><li>Huge distribution, installs half of...
Web Services APIs <ul><li>Many web sites expose web services </li></ul><ul><li>REST, RPC, SOAP </li></ul><ul><li>HTTP requ...
Web Services on CPAN <ul><li>Many CPAN modules for handling web services </li></ul><ul><li>Wrappers around LWP or WWW::Mec...
CPAN Examples <ul><li>Net::Backpack </li></ul><ul><li>Net::Amazon::* </li></ul><ul><li>Flickr::API </li></ul><ul><li>Geo::...
API Keys <ul><li>Many of these sites will monitor the requests you make </li></ul><ul><li>Use an API key </li></ul><ul><li...
API Example: Flickr <ul><li>Get details of photos </li></ul><ul><li>Details at http://www.flickr.com/services/api/ </li></...
Flickr API <ul><li>#!/usr/bin/perl use strict; use warnings; use Flickr::API; my $flickr = Flickr::API->new({   key => 'yo...
Flickr API Results <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <rsp stat=&quot;ok&quot;> <photos p...
APIs Without CPAN <ul><li>You might find an API that doesn't have a CPAN module </li></ul><ul><li>Easy to do it yourself <...
Ajax <ul><li>You don't need to be using Ruby on Rails to use AJAX </li></ul><ul><li>AJAX is largely just Javascript </li><...
Ajax on CPAN <ul><li>CGI::Ajax </li></ul><ul><ul><li>Creates AJAX code in your CGI program output </li></ul></ul><ul><ul><...
Further Information
Further Information <ul><li>Some suggestions for places to go for further information </li></ul><ul><li>Web sites </li></u...
Copenhagen Perl Mongers <ul><li>http://copenhagen.pm.org/ </li></ul><ul><li>Mailing list </li></ul><ul><li>Regular meeting...
Web Sites <ul><li>use Perl; </li></ul><ul><ul><li>Perl news site </li></ul></ul><ul><ul><li>Also journals </li></ul></ul><...
Web Sites <ul><li>Perl Monks </li></ul><ul><ul><li>Best web site for Perl questions </li></ul></ul><ul><ul><li>Many Perl e...
Books <ul><li>Some recent Perl books </li></ul><ul><li>Perl Best Practices  - Damian Conway </li></ul><ul><li>Advanced Per...
More Books <ul><li>Higher Order Perl  - Mark-Jason Dominus </li></ul><ul><li>Minimal Perl  - Tim Maher </li></ul><ul><li>P...
Magazines <ul><li>The Perl Review </li></ul><ul><ul><li>http://www.theperlreview.com/ </li></ul></ul><ul><li>Randal's mont...
Mailing Lists <ul><li>Many mailing lists devoted to Perl topics </li></ul><ul><li>See http://lists.cpan.org/ </li></ul>
Conferences <ul><li>The Open Source Convention </li></ul><ul><ul><li>Portland 21-25 July 2008 </li></ul></ul><ul><li>YAPC ...
The End <ul><li>That's all folks </li></ul><ul><li>Any questions? </li></ul>
Upcoming SlideShare
Loading in...5
×

Perl Teach-In (part 2)

6,279

Published on

Part two of the Perl Teach-In. This is a newer version of the talk as presented at YAPC::Europe in August 2008.

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,279
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
163
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Perl Teach-In (part 2)

  1. 1. Perl Teach-In <ul><ul><li>A One Day Perl Tutorial </li></ul></ul><ul><ul><li>Dave Cross </li></ul></ul><ul><ul><li>Magnum Solutions Ltd </li></ul></ul><ul><ul><li>[email_address] </li></ul></ul>
  2. 2. Object Oriented Perl
  3. 3. Object Oriented Perl <ul><li>How to design objects </li></ul><ul><li>Design Patterns </li></ul><ul><li>Inside-Out Objects </li></ul><ul><li>Moose </li></ul>
  4. 4. What We Won't Cover <ul><li>Writing objects in Perl </li></ul><ul><li>Assume you already know that </li></ul><ul><li>perldoc perlboot </li></ul><ul><li>Object Oriented Perl – Damian Conway </li></ul>
  5. 5. How To Design Objects <ul><li>Designing Objects is hard - OO Barbie </li></ul><ul><li>Very few hard and fast rules </li></ul><ul><li>A few heuristics </li></ul><ul><li>A bit of experience </li></ul><ul><li>A bit of guesswork </li></ul>
  6. 6. Prototyping <ul><li>Plan to throw one away; you will, anyhow - Fred Brooks (The Mythical Man Month) </li></ul><ul><li>You won't get it right first time </li></ul><ul><li>Unless you're very lucky </li></ul><ul><li>Make it easy to refactor your code </li></ul><ul><ul><li>Unit tests </li></ul></ul><ul><ul><li>Source code control </li></ul></ul>
  7. 7. Subclassing <ul><li>A good class is easy to subclass </li></ul><ul><li>Two argument bless </li></ul><ul><li>sub new { my $class = shift; my $self = {}; return bless $self, $class; } </li></ul>
  8. 8. Constructor Tip <ul><li>You'll often see code like this </li></ul><ul><li>sub new { my $thing = shift; my $class = ref $thing || $thing; return bless {}, $class; } </li></ul><ul><li>Don't do that </li></ul>
  9. 9. Confusing Methods <ul><li>The previous constructor can be called as a class method or an instance method </li></ul><ul><ul><li>my $obj = MyClass->new; </li></ul></ul><ul><ul><li>my $obj2 = $obj->new; </li></ul></ul><ul><li>Potentially confusing </li></ul><ul><li>What does $obj->new do? </li></ul><ul><li>Clone? </li></ul>
  10. 10. Separate Methods <ul><li>Class method creates a new, empty, object </li></ul><ul><li>sub new { my $class = shift; return bless {}, $class; } </li></ul>
  11. 11. Separate Methods <ul><li>Instance method creates a copy of an object </li></ul><ul><li>sub clone { my $self = shift; # warning! simplified! Buggy! return bless { %$self }, ref $self; } </li></ul>
  12. 12. Multiple Constructors <ul><li>In Perl an constructor is just a subroutine </li></ul><ul><li>new is not a keyword </li></ul><ul><li>You can have as many constructors as you want </li></ul><ul><li>They can be called whatever you want </li></ul><ul><ul><li>But using new for the standard constructor is recommended </li></ul></ul>
  13. 13. new is Not a Keyword <ul><li>People coming from other languages often write </li></ul><ul><li>my $obj = new MyObject; </li></ul><ul><li>This is potentially dangerous </li></ul><ul><li>See &quot;perldoc perlobj&quot; for gory details </li></ul><ul><li>Most of the time it will be fine </li></ul><ul><li>But avoid it </li></ul><ul><li>my $obj = MyObject->new; </li></ul>
  14. 14. Overriding Methods <ul><li>Subclasses are created so that methods can be overridden </li></ul><ul><ul><li>Or so that new methods can be added </li></ul></ul><ul><li>Make it as easy as possible to subclass your classes </li></ul><ul><li>Create many methods </li></ul>
  15. 15. Case Study <ul><li>I needed to create a new graph type for GD::Graph </li></ul><ul><li>Waterfall graph </li></ul><ul><li>Based on bar chart </li></ul><ul><li>Subclassing GD::Graph::bars </li></ul>
  16. 16. Bar Chart
  17. 17. Waterfall Chart
  18. 18. Things to Override <ul><li>Colour choosing </li></ul><ul><ul><li>pick_data_clr </li></ul></ul><ul><li>Legend drawing </li></ul><ul><ul><li>??? </li></ul></ul><ul><li>The author didn't envisage my use case </li></ul><ul><li>I ended up copying far too much code </li></ul>
  19. 19. Be Consistent <ul><li>A good object design is consistent </li></ul><ul><li>Similar things act in similar ways </li></ul><ul><li>Good candidates for programming standards </li></ul><ul><li>Some examples </li></ul>
  20. 20. Accessor vs Mutator <ul><li>Or &quot;getter vs setter&quot; </li></ul><ul><li>Some people like one method foo </li></ul><ul><ul><li>Works out what to do based on parameters </li></ul></ul><ul><li>Some people like get_foo and set_foo </li></ul><ul><li>Pick one and stick to it </li></ul>
  21. 21. Mutator Return Values <ul><li>What does a mutator return? </li></ul><ul><ul><li>The old value </li></ul></ul><ul><ul><li>The new value </li></ul></ul><ul><ul><li>The object </li></ul></ul><ul><ul><li>Nothing </li></ul></ul><ul><li>Pick one and stick to it </li></ul>
  22. 22. Don't Always Subclass <ul><li>Not all relationships are &quot;isa_a&quot; </li></ul><ul><li>Sometimes &quot;has_a&quot; is more appropriate </li></ul><ul><li>If you're connecting to a database </li></ul><ul><li>Don't subclass DBI </li></ul><ul><li>Have a DBI attribute </li></ul>
  23. 23. Design Patterns <ul><li>Perl does have some design patterns </li></ul><ul><li>Some of them come from Design Patterns </li></ul><ul><li>Some are more &quot;Perlish&quot; </li></ul>
  24. 24. Factory Object <ul><li>Call constructor on one class </li></ul><ul><li>Constructor works out what is the most appropriate class to use </li></ul><ul><li>Returns an object of the appropriate class </li></ul><ul><li>Often not the same class as the constructor was called on </li></ul><ul><li>E.g. DBI->connect </li></ul>
  25. 25. AudioFile::Info <ul><li>There are many Perl modules for reading tags from MP3 files </li></ul><ul><ul><li>MP3::ID3Lib </li></ul></ul><ul><ul><li>MP3::Info </li></ul></ul><ul><ul><li>MP3::Tag </li></ul></ul><ul><li>And a couple for reading tags from Ogg Vorbis files </li></ul><ul><ul><li>Ogg::Vorbis::Header </li></ul></ul><ul><ul><li>Ogg::Vorbis::Header::PurePerl </li></ul></ul>
  26. 26. Using AudioFile::Info <ul><li>AudioFile::Info simplifies reading tag information from audio files </li></ul><ul><li>One syntax across numerous modules </li></ul><ul><li>$song = AudioFile::Info->new('a_song.mp3'); print $song->title, ' - ', $song->artist; </li></ul><ul><li>$song2 = AudioFile::Info->new('a_song.ogg'); print $song2->title, ' - ', $song2->artist; </li></ul><ul><li>print ref $song; # AudioFile::Info::MP3::Tag </li></ul><ul><li>print ref $song2; # AudioFile::Info::Ogg::Vorbis::Header </li></ul>
  27. 27. AudioFile::Info->new <ul><li>Works out which kind of file it has been given </li></ul><ul><li>Works out the best installed module to handle that kind of file </li></ul><ul><li>Loads the appropriate module </li></ul><ul><li>Calls the constructor </li></ul><ul><li>Returns the new object </li></ul>
  28. 28. Singleton Pattern <ul><li>Highlander pattern </li></ul><ul><li>“ There can be only one” </li></ul><ul><li>Only ever one instance of the class </li></ul><ul><li>If an instance has been created then use that </li></ul><ul><li>Otherwise create new instance </li></ul>
  29. 29. Singleton Class <ul><li>package MySingleton; my $single; sub new { my $class = shift; unless ($single) { $single = bless {}, $class; } return $single; } </li></ul>
  30. 30. More Design Patterns <ul><li>See Perl Design Patterns Wiki </li></ul><ul><li>http://perldesignpatterns.com/ </li></ul>
  31. 31. Inside-Out Objects <ul><li>Standard Perl objects are usually based on hashes </li></ul><ul><li>bless { name => 'Dave', email => 'dave@dave.org.uk' }, 'Person'; </li></ul><ul><li>Two problems </li></ul><ul><li>People can access attributes directly </li></ul><ul><li>People can add attributes easily </li></ul>
  32. 32. Accessing Attributes <ul><li>$person->{name} = ''; </li></ul><ul><li>Avoids any checks in the mutator method </li></ul><ul><li>sub set_name { my $self = shift; my $name = shift; croak &quot;Name can't be empty&quot; unless $name; $self->{name} = $name; } </li></ul>
  33. 33. Adding Attributes <ul><li>No checks on adding entries to the hash </li></ul><ul><li>$person->{nick} = 'davorg'; </li></ul><ul><li>Our class knows nothing about this attribute </li></ul><ul><li>No check on mistyped attributes </li></ul><ul><li>$person->{NAME} = 'dave'; </li></ul>
  34. 34. Inside-Out Objects <ul><li>Inside-out objects solve both of these problems </li></ul><ul><li>An object is no longer a hash containing attributes </li></ul><ul><li>Each attribute is a package variable </li></ul><ul><li>A hash </li></ul><ul><ul><li>Key is unique identifier for object </li></ul></ul><ul><ul><li>Value is the attribute value for that object </li></ul></ul>
  35. 35. Example <ul><li>package Person; use strict; my %name; my %email; sub new { my $self = bless {}, shift; $name{$self} = shift; $email{$self} = shift; return $self; } </li></ul>
  36. 36. Example (cont) <ul><li>sub get_name { my $self = shift; return $name{$self}; } sub set_name { my $self = shift; my $name = shift; croak &quot;name cannot be empty&quot; unless $name; $name{$self} = $name; } </li></ul>
  37. 37. How It Works <ul><li>The object is still a hash reference </li></ul><ul><li>Still blessed into the correct class </li></ul><ul><li>But it contains no data </li></ul><ul><li>All data is stored in the lexical variable hashes </li></ul><ul><li>Hence the name - Inside-Out </li></ul>
  38. 38. Solving Problems <ul><li>People can access attributes directly </li></ul><ul><ul><li>Attributes are now stored in lexical variables </li></ul></ul><ul><ul><li>Only visible from within package </li></ul></ul><ul><ul><li>All access is through methods </li></ul></ul><ul><li>People can add attributes easily </li></ul><ul><ul><li>Attribute names are now the names of lexical variables </li></ul></ul><ul><ul><li>use strict ensures that variable names can't be mistyped </li></ul></ul>
  39. 39. One Improvement <ul><li>The object is still a blessed hash </li></ul><ul><li>But we never put anything into it </li></ul><ul><li>So it may as well be a blessed scalar </li></ul><ul><li>sub new { my $self = bless (my $dummy), shift; $name{$self} = shift; $email{$self} = shift; return $self; } </li></ul><ul><li>No anonymous scalars </li></ul>
  40. 40. One New Problem <ul><li>When our objects go out of scope, the blessed scalar ceases to exist </li></ul><ul><li>But the values still exist in the attribute hashes </li></ul><ul><li>Need a DESTROY method </li></ul><ul><li>sub DESTROY { my $self = shift; delete $name{$self}; delete $email{$self}; } </li></ul>
  41. 41. More Stuff <ul><li>Need to handle inheritance </li></ul><ul><li>Automation of inside-out objects </li></ul><ul><ul><li>Class::Std </li></ul></ul><ul><ul><li>Class::InsideOut </li></ul></ul><ul><ul><li>Object::InsideOut </li></ul></ul><ul><li>See also Perl Best Practices (Damian Conway) </li></ul>
  42. 42. Moose <ul><li>A complete modern object system for Perl 5 </li></ul><ul><li>Based on experiments with Perl 6 object model </li></ul><ul><li>Built on top of Class::MOP </li></ul><ul><ul><li>MOP - Meta Object Protocol </li></ul></ul><ul><ul><li>Set of abstractions for components of an object system </li></ul></ul><ul><ul><li>Classes, Objects, Methods, Attributes </li></ul></ul><ul><li>An example might help </li></ul>
  43. 43. Moose Example <ul><li>package Point; use Moose; has 'x' => (isa => 'Int', is => 'ro'); has 'y' => (isa => 'Int', is => 'rw'); sub clear { my $self = shift; $self->{x} = 0; $self->y(0); } </li></ul>
  44. 44. Understanding Moose <ul><li>There's a lot going on here </li></ul><ul><li>use Moose </li></ul><ul><ul><li>Loads Moose environment </li></ul></ul><ul><ul><li>Makes our class a subclass of Moose::Object </li></ul></ul><ul><ul><li>Turns on strict and warnings </li></ul></ul>
  45. 45. Creating Attributes <ul><li>has 'x' => (isa => 'Int', is => 'ro') </li></ul><ul><ul><li>Creates an attribute called 'x' </li></ul></ul><ul><ul><li>Constrainted to be an integer </li></ul></ul><ul><ul><li>Read-only accessor </li></ul></ul><ul><li>has 'y' => (isa => 'Int', is => 'rw') </li></ul>
  46. 46. Defining Methods <ul><li>sub clear { my $self = shift; $self->{x} = 0; $self->y(0); } </li></ul><ul><li>Standard method syntax </li></ul><ul><li>Uses generated method to set y </li></ul><ul><li>Direct hash access for x </li></ul>
  47. 47. Subclassing <ul><li>package Point3D; use Moose; extends 'Point'; has 'z' => (isa => 'Int'); after 'clear' => sub { my $self = shift; $self->{z} = 0; }; </li></ul>
  48. 48. Subclasses <ul><li>extends 'Point' </li></ul><ul><ul><li>Similar to use base </li></ul></ul><ul><ul><li>Overwrites @ISA instead of appending </li></ul></ul><ul><li>has 'z' => (isa = 'Int') </li></ul><ul><ul><li>Adds new attribute 'z' </li></ul></ul><ul><ul><li>No accessor function - private attribute </li></ul></ul>
  49. 49. Extending Methods <ul><li>after 'clear' => sub { my $self = shift; $self->{z} = 0; }; </li></ul><ul><li>New clear method for subclass </li></ul><ul><li>Called after method for superclass </li></ul><ul><li>Cleaner than $self->SUPER::clear() </li></ul>
  50. 50. Creating Objects <ul><li>Moose classes are used just like any other Perl class </li></ul><ul><li>$point = Point->new(x => 1, y => 2); </li></ul><ul><li>$p3d = Point3D->new(x => 1, y => 2, z => 3); </li></ul>
  51. 51. More Moose <ul><li>Only scratching the surface </li></ul><ul><li>Many more options </li></ul><ul><li>Moose is well worth investigating </li></ul><ul><li>perldoc Moose::Cookbook::* </li></ul>
  52. 52. Configuration Files
  53. 53. Configuration Files <ul><li>Can you just add...? </li></ul><ul><li>Users are never satisfied </li></ul><ul><li>Always want something changed </li></ul><ul><li>Most users won't edit code </li></ul><ul><li>Most users can be trained to edit config files </li></ul><ul><li>Move as much as possible into config files </li></ul>
  54. 54. Configuration in Code <ul><li>This is bad </li></ul><ul><li>#!/usr/bin/perl use strict; use warnings; ## Configuration my $debug = 0; my $email = 'dave@dave.org.uk'; my $output_file = 'output.txt'; ## DO NOT EDIT BEYOND THIS POINT </li></ul>
  55. 55. Why is This Bad? <ul><li>Don't trust 'em </li></ul><ul><li>Something will get broken </li></ul><ul><li>Code doesn't match what is in source code control </li></ul><ul><li>Recipe for disaster </li></ul><ul><li>Don't do it </li></ul>
  56. 56. Windows INI Files <ul><li>[debugging] debug=1 verbose=0 </li></ul><ul><li>Most users will understand this </li></ul><ul><li>Provide them with a dry-run option </li></ul><ul><ul><li>Tell them what would happen </li></ul></ul><ul><li>Parse with Config::IniFiles </li></ul>
  57. 57. XML <ul><li><debugging> <debug>1</debug> <verbose>0</debug> </debugging> </li></ul><ul><li>or </li></ul><ul><li><debugging debug=&quot;1&quot; verbose=&quot;0&quot; /> </li></ul><ul><li>Too complicated </li></ul><ul><li>Parse with XML::Simple (or XML::LibXML / XML::XPath) </li></ul>
  58. 58. YAML <ul><li>--- debugging: debug: 1 verbose: 0 </li></ul><ul><li>Looks a bit like INI file </li></ul><ul><li>Human-readable </li></ul><ul><li>Still give them a dry-run option </li></ul><ul><li>Parse with YAML </li></ul>
  59. 59. Swiss Army Config Parser <ul><li>Config::Auto </li></ul><ul><li>&quot;Magical config file parser&quot; </li></ul><ul><li>Guesses where your config file is </li></ul><ul><li>Guesses what format it is in </li></ul><ul><li>And usually gets it right </li></ul>
  60. 60. Other Places <ul><li>Environment variables </li></ul><ul><li>Command line options </li></ul><ul><ul><li>Parse with Getopt::Long </li></ul></ul><ul><li>Need to establish an order of precedence </li></ul><ul><li>$debug = defined $opt{debug} ? $opt{debug} : defined $ENV{DEBUG} ? $ENV{DEBUG} : defined $conf{debug} ? $conf{debug} : 0; </li></ul>
  61. 61. Web 2.0 Perl
  62. 62. Web 2.0 Perl <ul><li>Mashups </li></ul><ul><li>Data Feeds </li></ul><ul><li>APIs </li></ul><ul><li>Ajax </li></ul><ul><li>Don't need to be using Ruby on Rails </li></ul><ul><li>CPAN modules can help </li></ul>
  63. 63. Reading Web Feeds <ul><li>A lot of data available as web feeds </li></ul><ul><li>RSS / Atom </li></ul><ul><li>But which version of RSS </li></ul><ul><li>RDF vs RSS </li></ul>
  64. 64. Parsing RSS <ul><li>XML::RSS </li></ul><ul><li>use XML::RSS; my $rss = XML::RSS->new; $rss->parsefile('myfeed.rss'); foreach (@{$rss->{items}}) { print $_->{title}, &quot; &quot;; print $_->{link}, &quot;n&quot;; } </li></ul><ul><li>Supports RSS 0.9, 0.91, 1.0 and 2.0 </li></ul><ul><li>Most of the versions you'll see in the wild </li></ul>
  65. 65. Parsing Atom <ul><li>XML::Atom </li></ul><ul><li>use XML::Atom::Feed; my $atom = XML::Atom::Feed->new('myatom.xml'); foreach ($atom->entries) { print $_->title, &quot; &quot;; print $_->link, &quot; &quot;; } </li></ul><ul><li>Cleverer than XML::RSS </li></ul><ul><li>Handles things like autodiscovery </li></ul><ul><li>my @feeds = XML::Atom::Feed->find_feeds ('http://example.com/'); </li></ul>
  66. 66. Parsing RSS and Atom <ul><li>XML::Feed uses XML::RSS or XML::Atom as appropriate </li></ul><ul><li>Allows code to handle all web feeds. </li></ul><ul><li>use XML::Feed; my $feed = XML::Feed->parse('feed.xml'); print $feed->title; foreach ($feed->entries) { print $_->title; } </li></ul>
  67. 67. Parsing Broken Feeds <ul><li>Not all XML feeds are valid XML </li></ul><ul><li>This is bad! </li></ul><ul><li>You should complain bitterly to the provider </li></ul><ul><li>But until it is fixed you can use XML::Liberal </li></ul><ul><ul><li>Slightly badly named </li></ul></ul><ul><ul><li>What it parses isn't XML </li></ul></ul>
  68. 68. Mashing Up Web Feeds <ul><li>Plagger - Pluggable RSS/Atom Aggregator </li></ul><ul><li>Huge distribution, installs half of CPAN </li></ul><ul><li>Dozens of small plugins </li></ul><ul><li>Single program ( plagger ) </li></ul><ul><li>YAML configuration file </li></ul>
  69. 69. Web Services APIs <ul><li>Many web sites expose web services </li></ul><ul><li>REST, RPC, SOAP </li></ul><ul><li>HTTP request </li></ul><ul><li>Parse response </li></ul><ul><ul><li>Usually XML </li></ul></ul><ul><ul><li>Or JSON </li></ul></ul>
  70. 70. Web Services on CPAN <ul><li>Many CPAN modules for handling web services </li></ul><ul><li>Wrappers around LWP or WWW::Mechanize </li></ul><ul><li>Parse response into data structures or objects </li></ul><ul><li>Make dealing with web services very easy </li></ul>
  71. 71. CPAN Examples <ul><li>Net::Backpack </li></ul><ul><li>Net::Amazon::* </li></ul><ul><li>Flickr::API </li></ul><ul><li>Geo::Google </li></ul><ul><li>Geo::Coder::Yahoo </li></ul>
  72. 72. API Keys <ul><li>Many of these sites will monitor the requests you make </li></ul><ul><li>Use an API key </li></ul><ul><li>Apply for the key on the web site </li></ul><ul><li>Use the key in every request you use </li></ul>
  73. 73. API Example: Flickr <ul><li>Get details of photos </li></ul><ul><li>Details at http://www.flickr.com/services/api/ </li></ul><ul><li>Many methods </li></ul><ul><li>Perl module gives low level access </li></ul><ul><li>Need to know the API pretty well </li></ul>
  74. 74. Flickr API <ul><li>#!/usr/bin/perl use strict; use warnings; use Flickr::API; my $flickr = Flickr::API->new({ key => 'your key goes here' }); my $response = $flickr->execute_method( 'flickr.photos.getRecent', { per_page => 2 } ); print $response->content; </li></ul>
  75. 75. Flickr API Results <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <rsp stat=&quot;ok&quot;> <photos page=&quot;1&quot; pages=&quot;500&quot; perpage=&quot;2&quot; total=&quot;1000&quot;> <photo id=&quot;525440809&quot; owner=&quot;65666164@N00&quot; secret=&quot;3ab099b893&quot; server=&quot;223&quot; farm=&quot;1&quot; title=&quot;00760052&quot; ispublic=&quot;1&quot; isfriend=&quot;0&quot; isfamily=&quot;0&quot; /> <photo id=&quot;525440801&quot; owner=&quot;47391132@N00&quot; secret=&quot;d24451edc8&quot; server=&quot;1009&quot; farm=&quot;2&quot; title=&quot;041070 14 29B&quot; ispublic=&quot;1&quot; isfriend=&quot;0&quot; isfamily=&quot;0&quot; /> </photos> </rsp> </li></ul>
  76. 76. APIs Without CPAN <ul><li>You might find an API that doesn't have a CPAN module </li></ul><ul><li>Easy to do it yourself </li></ul><ul><li>Read the API documentation </li></ul><ul><li>LWP / WWW::Mechanize </li></ul><ul><li>Parse the response </li></ul><ul><li>(Release to CPAN) </li></ul>
  77. 77. Ajax <ul><li>You don't need to be using Ruby on Rails to use AJAX </li></ul><ul><li>AJAX is largely just Javascript </li></ul><ul><li>With a server-side application </li></ul><ul><li>Which can be written in Perl </li></ul>
  78. 78. Ajax on CPAN <ul><li>CGI::Ajax </li></ul><ul><ul><li>Creates AJAX code in your CGI program output </li></ul></ul><ul><ul><li>Similar to Ruby on Rails AJAX helpers </li></ul></ul><ul><li>JSON </li></ul><ul><ul><li>Perl module for creating JSON </li></ul></ul><ul><ul><li>Common format for AJAX data exchange </li></ul></ul><ul><ul><li>Serialised Javascript Objects </li></ul></ul><ul><ul><li>A lot like YAML </li></ul></ul>
  79. 79. Further Information
  80. 80. Further Information <ul><li>Some suggestions for places to go for further information </li></ul><ul><li>Web sites </li></ul><ul><li>Books </li></ul><ul><li>Magazines </li></ul><ul><li>Mailing lists </li></ul><ul><li>Conferences </li></ul>
  81. 81. Copenhagen Perl Mongers <ul><li>http://copenhagen.pm.org/ </li></ul><ul><li>Mailing list </li></ul><ul><li>Regular meetings </li></ul><ul><li>Nordic Perl Workshop </li></ul><ul><li>YAPC::Europe </li></ul><ul><li>Many other local Perl Monger groups </li></ul><ul><ul><li>http://pm.org/ </li></ul></ul>
  82. 82. Web Sites <ul><li>use Perl; </li></ul><ul><ul><li>Perl news site </li></ul></ul><ul><ul><li>Also journals </li></ul></ul><ul><li>perl.com </li></ul><ul><ul><li>O'Reilly run site </li></ul></ul><ul><ul><li>High quality articles </li></ul></ul>
  83. 83. Web Sites <ul><li>Perl Monks </li></ul><ul><ul><li>Best web site for Perl questions </li></ul></ul><ul><ul><li>Many Perl experts </li></ul></ul><ul><li>The Perl directory </li></ul><ul><ul><li>http://perl.org/ </li></ul></ul><ul><ul><li>Lists of many Perl-related sites </li></ul></ul>
  84. 84. Books <ul><li>Some recent Perl books </li></ul><ul><li>Perl Best Practices - Damian Conway </li></ul><ul><li>Advanced Perl Programming - Simon Cozens </li></ul><ul><li>Perl Hacks - chromatic, Conway & Poe </li></ul><ul><li>Intermediate Perl - Schwartz, foy & Phoenix </li></ul><ul><li>Mastering Perl - brian d foy </li></ul>
  85. 85. More Books <ul><li>Higher Order Perl - Mark-Jason Dominus </li></ul><ul><li>Minimal Perl - Tim Maher </li></ul><ul><li>Pro Perl Debugging - Richard Foley & Joe McMahon </li></ul><ul><li>Perl & LWP - Sean M Burke </li></ul><ul><ul><li>Updated online edition </li></ul></ul><ul><ul><li>http://lwp.interglacial.com/ </li></ul></ul><ul><li>See http://books.perl.org/ </li></ul>
  86. 86. Magazines <ul><li>The Perl Review </li></ul><ul><ul><li>http://www.theperlreview.com/ </li></ul></ul><ul><li>Randal's monthly columns </li></ul><ul><ul><li>Linux Magazine </li></ul></ul><ul><ul><li>SysAdmin </li></ul></ul>
  87. 87. Mailing Lists <ul><li>Many mailing lists devoted to Perl topics </li></ul><ul><li>See http://lists.cpan.org/ </li></ul>
  88. 88. Conferences <ul><li>The Open Source Convention </li></ul><ul><ul><li>Portland 21-25 July 2008 </li></ul></ul><ul><li>YAPC </li></ul><ul><ul><li>Chicago 16-18 June 2008 </li></ul></ul><ul><ul><li>Copenhagen 13-15 August 2008 </li></ul></ul><ul><ul><li>Brazil, Asia, Israel, Australia </li></ul></ul><ul><li>One-Day Perl Workshops </li></ul><ul><li>See http://yapc.org/ </li></ul>
  89. 89. The End <ul><li>That's all folks </li></ul><ul><li>Any 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.

×