Inheritance
examples/perloop/employee03/script/person.pl
use strict; use warnings; use v5.10; use Person; use Employee; use DateTime; my $programmer = Employee->new( name => 'Bar', employer => 'Amazon' ); say $programmer->name; # Bar say $programmer->employer; # Amazon my $student = Person->new( name => 'Foo', employer => 'Apple' ); say $student->name; # Foo say $student->employer; # Exception: Can't locate object method "employer" via package "Person"
examples/perloop/employee03/lib/Employee.pm
package Employee; use strict; use warnings; use parent 'Person'; sub new { my ($class, %args) = @_; my $employer = delete $args{employer}; my $self = $class->SUPER::new(%args); $self->{employer} = $employer; return $self; } sub employer { my ($self, $value) = @_; if (@_ == 2) { $self->{employer} = $value; } return $self->{employer}; } 1;