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
Enter Moose
Free constructor, accessors, and modifiers
Attribute type enforcement with parameter checking
Read-only attributes
Meta-programming
Pretty syntax
drew@dinomite.net
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
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
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
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
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
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
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
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
When non-Perl folks look at this, they see a high ratio of non-word characters, lots of existentialisim, and some religious bullshit—they’re thinking one thing…
Instead of using SUPER::, we can extend the parent method