package HashRefAsObject; sub new1 { # using a hash ref as an object my $class = shift; my $self = { }; # hash ref bless( $self, $class ); $self->{varA} = 123; # set "instvar" $self->{varB} = 456; # another return $self; } package ArrayRefAsObject; sub new2 { # using an array ref as an object my $class = shift; my $self = []; # array ref bless( $self, $class ); $self->[0] = 123; # set "instvar" $self->[1] = 456; # another return $self; } package Foo; # Using pseudo-hashes # from fields manpage: use fields qw(foo bar _Foo_private); sub new { my Foo $self = shift; unless (ref $self) { $self = fields::new($self); $self->{_Foo_private} = "this is Foo's secret"; } $self->{foo} = 10; $self->{bar} = 20; return $self; }