Add another init_arg example
[gitmo/moose-presentations.git] / moose-class / exercises / answers / 04-method-modifiers / Person.pm
CommitLineData
c21bbce8 1package Person;
2
3use Moose;
4
5has title => (
6 is => 'rw',
7 predicate => 'has_title',
8 clearer => 'clear_title',
9);
10
11has first_name => (
12 is => 'rw',
13 required => 1,
14);
15
16has last_name => (
17 is => 'rw',
18 required => 1,
19);
20
21sub BUILDARGS {
22 my $class = shift;
23
24 if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
25 return {
26 first_name => $_[0]->[0],
27 last_name => $_[0]->[1],
28 };
29 }
30 else {
31 return $class->SUPER::BUILDARGS(@_);
32 }
33}
34
35our @CALL;
36
37before full_name => sub {
38 push @CALL, 'calling full_name';
39};
40
41after full_name => sub {
42 push @CALL, 'called full_name';
43};
44
45sub full_name {
46 my $self = shift;
47
48 my $title = join q{ }, $self->first_name, $self->last_name;
49 $title .= q[ (] . $self->title . q[)]
50 if $self->has_title;
51
52 return $title;
53}
54
55around full_name => sub {
56 my $orig = shift;
57 my $self = shift;
58
59 return $self->$orig unless $self->last_name eq 'Wall';
60
61 return q{*} . $self->$orig . q{*};
62};
63
64sub as_string { $_[0]->full_name }
65
66no Moose;
67
68__PACKAGE__->meta->make_immutable;
69
701;