=> 'ro', isa => 'Str'; has 'age', is => 'rw', isa => 'Int'; package main; use feature ':5.10'; my $p = Person->new(name => "James"); say $p->name; Class Def Class Use • A class is just a package • A method is just a sub • An attribute is ... We’ll get to that later Tuesday, February 28, 2012
its first argument • That’s why we use my $self = shift package Car; use Moose; has 'speed', is => 'ro'; sub go { my $self = shift; print "Vroom Vroom [speed: ", $self->speed, "]\n"; } package main; my $c = Car->new(speed => 10); $c->go; Tuesday, February 28, 2012
ConfigFactory; use Moose; sub build_config { my $cfg; given ($^O) { $cfg = WinConfig->new when /MSWin32/; $cfg = UnixConfig->new; } return $cfg; } Tuesday, February 28, 2012
Use requires to define a partial implementation package Painter; use Moose::Role; requires 'drawPixel'; sub draw_line { ... } sub draw_triangle { ... } sub draw_rectangle { ... } Tuesday, February 28, 2012
is => 'ro', handles => [ qw/send_mail/ ] ); • Can take regular expressions • Can take hashref • perldoc Moose::Manual::Delegation my $c = Contact->new; $c->send_mail(subject => "Hello", text => "..."); Tuesday, February 28, 2012