package Person;
sub new { bless { name => $_[1], job => $_[2] }, $_[0] }
sub name { $_[0]{name} }
sub job { $_[0]{job} }
package Person::Resumefied;
sub new { bless { name => $_[1], jobs => [ @$_[2] ] }, $_[0] }
sub jobs { @{$_[0]{jobs}} };
sub job { $_[0]{jobs}[$#{$_[0]{jobs}}] } # the last job is assumed current
####
package Person;
# ... other methods and such
sub cool { not $_[0]{job} =~ /programmer/ };
####
sub cool { not $_[0]->job() =~ /programmer/ };
####
nothingmuch% perl
use Benchmark qw(cmpthese);
our $self = Person->new();
cmpthese(10_000_000,{
direct => 'my $var = $self->{job}',
method => 'my $var = $self->job()',
});
package Person;
sub new { bless { job => 'not a programmer' },shift };
sub job { $_[0]{job} };
Benchmark: timing 10000000 iterations of direct, method...
direct: 10 wallclock secs ( 9.70 usr + 0.00 sys = 9.70 CPU) @ 1030927.84/s (n=10000000)
method: 39 wallclock secs (39.57 usr + 0.00 sys = 39.57 CPU) @ 252716.70/s (n=10000000)
Rate method direct
method 252717/s -- -75%
direct 1030928/s 308% --