Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)


Published on

Published in: Technology, Business
  • Be the first to comment

Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)

  1. 1. Perl Design Patterns brian d foy Stonehenge Consulting Ser vices February 24, 2006 Grand Rapids Perl Mongers Sponsored by PriorityHealth
  2. 2. Patterns aren’t a ... code cookbook module on CPAN methodology goal religion
  3. 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. 4. Three parts Context System of forces Solution
  5. 5. Nothing’s Free Actions have reactions Complexity turns up somewhere else Choices have consequences
  6. 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. 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. 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. 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. 10. An implementation package My::Config; my $singleton = undef; sub new { my $class = shift; return $singleton if defined $singleton; $singleton = bless {}, $class; }
  11. 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. 12. That isn’t the only way The pattern is not a prescription It’s an option Maybe another pattern works better.
  13. 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. 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. 15. Some Perl Modules Apache::DBI Netscape::Bookmarks CGI::Prototype many things in Class::*, almost
  16. 16. Beware Patterns are not code No matter what the Gang of Four say Class::* is code Ergo, ...
  17. 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. 18. Further Reading The Perl Review (lots of articles (by me)) “Design Patterns Aren’t” by Mark Jason Dominus http:/ / Design Patterns - Erich Gamma, et al. (Gang of Four) Perl Design Patterns Wiki http:/ /