X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=moose-class%2Fexercises%2Fanswers%2F04-method-modifiers%2FPerson.pm;h=8c519e2a86f101f03c0ef93f1f61cbf80d0786ab;hb=c21bbce81e4a09e4f1225a1c4914d91036dda49c;hp=e1edc607108a4321119cf518972309ad838f1742;hpb=26164c8d9acd6c4d8bad6fe3494db86102bac515;p=gitmo%2Fmoose-presentations.git diff --git a/moose-class/exercises/answers/04-method-modifiers/Person.pm b/moose-class/exercises/answers/04-method-modifiers/Person.pm index e1edc60..8c519e2 100644 --- a/moose-class/exercises/answers/04-method-modifiers/Person.pm +++ b/moose-class/exercises/answers/04-method-modifiers/Person.pm @@ -2,17 +2,45 @@ package Person; use Moose; -with 'Printable', 'HasAccount', 'OutputsXML'; - has title => ( is => 'rw', predicate => 'has_title', clearer => 'clear_title', ); -has first_name => ( is => 'rw' ); +has first_name => ( + is => 'rw', + required => 1, +); + +has last_name => ( + is => 'rw', + required => 1, +); + +sub BUILDARGS { + my $class = shift; + + if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) { + return { + first_name => $_[0]->[0], + last_name => $_[0]->[1], + }; + } + else { + return $class->SUPER::BUILDARGS(@_); + } +} + +our @CALL; -has last_name => ( is => 'rw' ); +before full_name => sub { + push @CALL, 'calling full_name'; +}; + +after full_name => sub { + push @CALL, 'called full_name'; +}; sub full_name { my $self = shift; @@ -24,15 +52,16 @@ sub full_name { return $title; } -sub as_string { $_[0]->full_name } - -sub as_xml { +around full_name => sub { + my $orig = shift; my $self = shift; - return - ( map { "<$_>" . ( $self->$_ || q{} ) . "" } qw( first_name last_name title ) ), - inner(); -} + return $self->$orig unless $self->last_name eq 'Wall'; + + return q{*} . $self->$orig . q{*}; +}; + +sub as_string { $_[0]->full_name } no Moose;