has first_name => ( is => 'rw' );
has last_name => ( is => 'rw' );
+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(@_);
+ }
+}
+
sub full_name {
my $self = shift;
# method should return the first and last name separated by a string
# ("Jon Smith").
#
+# Write a BUILDARGS method for this class which allows the caller to
+# pass a two argument array reference. These should be assigned to the
+# first and last name respectively.
+#
+# Person->new( [ 'Lisa', 'Smith' ] );
+#
# Create an Employee class in lib/Employee.pm
#
# The Employee class is a subclass of Person
is( $person->full_name, 'Bilbo Baggins',
'full_name() is correctly implemented' );
+
+ $person = Person->new( [ qw( Lisa Smith ) ] );
+ is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
+ is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
+
+ eval { Person->new( sub { 'foo' } ) };
+ like( $@, qr/\QSingle parameters to new() must be a HASH ref/,
+ 'Person constructor still rejects bad parameters' );
}
sub employee01 {