Add another init_arg example
[gitmo/moose-presentations.git] / moose-class / exercises / answers / 03-basic-attributes / Person.pm
1 package Person;
2
3 use Moose;
4
5 with 'Printable', 'HasAccount';
6
7 has title => (
8     is        => 'rw',
9     predicate => 'has_title',
10     clearer   => 'clear_title',
11 );
12
13 has first_name => (
14     is       => 'rw',
15     required => 1,
16 );
17
18 has last_name => (
19     is       => 'rw',
20     required => 1,
21 );
22
23 sub BUILDARGS {
24     my $class = shift;
25
26     if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
27         return {
28             first_name => $_[0]->[0],
29             last_name  => $_[0]->[1],
30         };
31     }
32     else {
33         return $class->SUPER::BUILDARGS(@_);
34     }
35 }
36
37 sub full_name {
38     my $self = shift;
39
40     my $title = join q{ }, $self->first_name, $self->last_name;
41     $title .= q[ (] . $self->title . q[)]
42         if $self->has_title;
43
44     return $title;
45 }
46
47 sub as_string { $_[0]->full_name }
48
49 no Moose;
50
51 __PACKAGE__->meta->make_immutable;
52
53 1;