#test.pl use lib '.'; use Foo; use Benchmark ':all'; my $f = bless {}, 'Foo'; timethese(0, { set_inherited_class => sub {Foo->set_inherited('bar', 'baz')}, set_inherited_object => sub {$f->set_inherited('bar', 'baz')} }); ##Foo.pm package Foo; use Scalar::Util qw/blessed reftype/; sub set_inherited { my ($self, $set, $val) = @_; if (blessed $self) { if (reftype $self eq 'HASH') { return $self->{$set} = $val; } else { croak('Cannot set inherited value on an object instance that is not hash-based'); }; } else { no strict 'refs'; return ${$self.'::__cag_'.$set} = $val; }; } 1;