Learning Moose
Taking Perl Objects to the Next Level


Drew Stephens <drew@dinomite.net>
Classic Perl Objects
package Horse;

sub new {
   my $class = shift; my ($name, $legs) = @_;
   my $self = {name => $name;...
It’s Crap
Enter Moose

Free constructor, accessors, and modifiers
Attribute type enforcement with parameter checking
Read-only attrib...
What’s a Horse Look Like?
    package Horse;
    use Moose;
    has 'name' => (
       is => 'rw',
       isa => ‘Str’,
  ...
Making a Horse

    use Horse;

    my $ed = Horse->new(
       name => ‘Mr. Ed’
    );

    print $ed->name() . " has " ....
Abstract Classes
    package Animal;
    use Moose::Role;
    has 'name' => (
        is => 'rw', isa => ‘Str’, required =...
Inherited Horse
    package Horse;
    use Moose;
    with 'Animal';
    sub sound { 'neigh' }
    1;

    $ed->speak(); #...
Extending Parent Methods
    package Mouse;
    use Moose;
    with 'Animal';

    sub sound { 'squeak' }

    after 'spea...
Type Coercion
    coerce 'HistoricalDateTime'
       => from 'Str'
       => via {
          require DateTime::Format::Dat...
Fill The Barn
    my $newbie = Horse->new(
       born => 'yesterday',
       name => 'Newbie'
    );
    $newbie->speak()...
References

Damian Conway: Perl Best Practices. O’Reilly, 2009

Randal L. Schwartz: Inside Out Objects.
   http://www.ston...
Upcoming SlideShare
Loading in...5
×

Learning Moose SHDH 36 Lightning Talk

3,553

Published on

A lightning talk introduction to the Moose object framework for Perl.

Video: http://www.youtube.com/watch?v=wGGDPATfsfo

Published in: Technology, Education
2 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,553
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
48
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide
  • 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;
  • Instead of using SUPER::, we can extend the parent method
  • Learning Moose SHDH 36 Lightning Talk

    1. 1. Learning Moose Taking Perl Objects to the Next Level Drew Stephens <drew@dinomite.net>
    2. 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. 3. It’s Crap
    4. 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. 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. 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. 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. 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. 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. 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. 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. 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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×