package Foo; sub new { my ($class) = @_; return bless [], $class; } sub val { my $self = shift; $self->[0] = $_[0] if @_; "Foo($self->[0])"; } package Bar; sub new { my ($class) = @_; return bless { val => 'nothing' }, $class; } sub val { my $self = shift; $self->{val} = $_[0] if @_; "Bar($self->{val})"; }