3a4294996d487693a7e6728a7b21e56190fe5dc3
[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 => ( is => 'rw' );
14
15 has last_name  => ( is => 'rw' );
16
17 sub BUILDARGS {
18     my $class = shift;
19
20     if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
21         return {
22             first_name => $_[0]->[0],
23             last_name  => $_[0]->[1],
24         };
25     }
26     else {
27         return $class->SUPER::BUILDARGS(@_);
28     }
29 }
30
31 sub full_name {
32     my $self = shift;
33
34     my $title = join q{ }, $self->first_name, $self->last_name;
35     $title .= q[ (] . $self->title . q[)]
36         if $self->has_title;
37
38     return $title;
39 }
40
41 sub as_string { $_[0]->full_name }
42
43 no Moose;
44
45 __PACKAGE__->meta->make_immutable;
46
47 1;