• Save
Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)
Upcoming SlideShare
Loading in...5

Like this? Share it with your network

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 3

http://www.slideshare.net 3

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Perl Design Patterns brian d foy Stonehenge Consulting Ser vices February 24, 2006 Grand Rapids Perl Mongers Sponsored by PriorityHealth
  • 2. Patterns aren’t a ... code cookbook module on CPAN methodology goal religion
  • 3. Patterns are a ... abstraction for things we need to do solutions for common problems name for a design element common language for design discussions way to apply a common design to code relationships
  • 4. Three parts Context System of forces Solution
  • 5. Nothing’s Free Actions have reactions Complexity turns up somewhere else Choices have consequences
  • 6. Who’s responsible? Our application needs config information But we have several modules Who loads the data? How do the other modules get it?
  • 7. A pattern Load the configuration from anywhere But load it only one time Anyone else gets a reference to it The order doesn’t matter
  • 8. What’s in a name A couple words instead of sentences We agree on what the name implies Others know what we mean “A rose is a rose is a rose”
  • 9. The Singleton There is only one configuration Let’s call it a singleton ... or a highlander We don’t have an implementation Just a name with implied design elements
  • 10. An implementation package My::Config; my $singleton = undef; sub new { my $class = shift; return $singleton if defined $singleton; $singleton = bless {}, $class; }
  • 11. A use package My::Database; use My::Config; my $config = My::Config->new( ... ); package My::Network; use My::Config; my $config = My::Config->new( ... );
  • 12. That isn’t the only way The pattern is not a prescription It’s an option Maybe another pattern works better.
  • 13. A Meta Class Write a meta class that contains all of the object parts Objects talk to the meta class to communicate with the other parts use My::Controller; my $controller = My::Controller->new(...); my $value = $controller->config->get( ... );
  • 14. Delegates package My::Controller; sub new { my $class = shift; my $self = bless {}, $class; # weaken some of these circular refs @$self{ qw(_config _database _network ) } = ( My::Config->new( controller => $self ), DBI->new( ... ), My::Socket->new( controller => $self ), ); $self; } sub config { $_[0]{_config} }
  • 15. Some Perl Modules Apache::DBI Netscape::Bookmarks CGI::Prototype many things in Class::*, almost
  • 16. Beware Patterns are not code No matter what the Gang of Four say Class::* is code Ergo, ...
  • 17. Perl is Better People like Patterns because they get code C++, Java suck at some things (Iterators) Perl doesn’t suck at the same things
  • 18. Further Reading The Perl Review (lots of articles (by me)) “Design Patterns Aren’t” by Mark Jason Dominus http:/ /perl.plover.com/yak/design/ Design Patterns - Erich Gamma, et al. (Gang of Four) Perl Design Patterns Wiki http:/ /perldesignpatterns.com/