Advertisement
Advertisement

More Related Content

Advertisement

Learning Moose SHDH 36 Lightning Talk

  1. Learning Moose Taking Perl Objects to the Next Level Drew Stephens <drew@dinomite.net>
  2. Classic Perl Objects package Horse; sub new { my $class = shift; my ($name, $legs) = @_; my $self = {name => $name; legs => $legs;} return bless $self, $class; } sub name { my $self = shift; if (@_) {$self->{name} = shift;} return $self->{name}; } drew@dinomite.net
  3. It’s Crap
  4. Enter Moose Free constructor, accessors, and modifiers Attribute type enforcement with parameter checking Read-only attributes Meta-programming Pretty syntax drew@dinomite.net
  5. What’s a Horse Look Like? package Horse; use Moose; has 'name' => ( is => 'rw', isa => ‘Str’, required => 1, ); has 'legs' => ( is => 'ro', isa => ‘Int’, default => 4, ); 1; Randal L. Schwartz: Linux Magazine Column 94: The Moose is Flying. http://is.gd/50Ezi drew@dinomite.net
  6. Making a Horse use Horse; my $ed = Horse->new( name => ‘Mr. Ed’ ); print $ed->name() . " has " . $ed->legs() . " legsn"; Randal L. Schwartz: Linux Magazine Column 94: The Moose is Flying. http://is.gd/50Ezi drew@dinomite.net
  7. Abstract Classes package Animal; use Moose::Role; has 'name' => ( is => 'rw', isa => ‘Str’, required => 1, ); has 'legs' => ( is => 'ro', isa => ‘Int’, default => 4, ); requires 'sound'; sub speak { my $self = shift; print $self->name, " goes ", $self->sound, "n"; } Randal L. Schwartz: Linux Magazine Column 94: The Moose is Flying. http://is.gd/50Ezi drew@dinomite.net
  8. Inherited Horse package Horse; use Moose; with 'Animal'; sub sound { 'neigh' } 1; $ed->speak(); # Mr. Ed goes neigh Randal L. Schwartz: Linux Magazine Column 94: The Moose is Flying. http://is.gd/50Ezi drew@dinomite.net
  9. Extending Parent Methods package Mouse; use Moose; with 'Animal'; sub sound { 'squeak' } after 'speak' => sub { print "[but you can barely hear it!]n"; }; # A mouse goes squeak # [but you can barely hear it!] Randal L. Schwartz: Linux Magazine Column 94: The Moose is Flying. http://is.gd/50Ezi drew@dinomite.net
  10. Type Coercion coerce 'HistoricalDateTime' => from 'Str' => via { require DateTime::Format::DateManip; DateTime::Format::DateManip->parse_datetime($_); }; has 'born' => ( is => 'ro', isa => 'HistoricalDateTime', coerce => 1, ); Randal L. Schwartz: Linux Magazine Column 95: The Moose is Flying (part 2). http://is.gd/50Nfm drew@dinomite.net
  11. Fill The Barn my $newbie = Horse->new( born => 'yesterday', name => 'Newbie' ); $newbie->speak(); # Newbie goes neigh my $mouse = Mouse->new( name => 'Minnie', born => '3/14/1929' ); $mouse->speak(); # Minnie goes squeak # [but you can barely hear it!] Randal L. Schwartz: Linux Magazine Column 95: The Moose is Flying (part 2). http://is.gd/50Nfm drew@dinomite.net
  12. References Damian Conway: Perl Best Practices. O’Reilly, 2009 Randal L. Schwartz: Inside Out Objects. http://www.stonehenge.com/merlyn/UnixReview/col63.html Jay Kuri: A Gentle Introduction to Moose. http://www.catalyzed.org/2009/06/a-gentle-introduction-to-moose.html Randal L. Schwartz: The Moose is Flying. http://www.stonehenge.com/merlyn/LinuxMag/col94.html Moose.pm on CPAN (v0.93). http://search.cpan.org/dist/Moose/lib/Moose/Manual.pod drew@dinomite.net

Editor's Notes

  1. When non-Perl folks look at this, they see a high ratio of non-word characters, lots of existentialisim, and some religious bullshit&amp;#x2014;they&amp;#x2019;re thinking one thing&amp;#x2026;
  2. Instead of using SUPER::, we can extend the parent method
Advertisement