#!/usr/bin/perl use strict; use warnings; package HashBased; sub new { my $class = shift; return bless {@_}, $class; } sub noise { my $self = shift; printf "The %s goes %s!\n", $self->{name}, $self->{sound} } sub name { my $self = shift; return $self->{name}; } package InsideOut; { use Scalar::Util 'refaddr'; my %legs; our @ISA = 'HashBased'; sub new { my $class = shift; my $legs = pop; my $obj = bless $class->SUPER::new(@_), $class; $legs{refaddr $obj} = $legs; return $obj; } sub show_legs { my $self = shift; printf "The %s has %d legs\n", $self->name, $legs{refaddr $self}; } sub DESTROY { my $self = shift; delete $legs{$self} } } package main; my $dog = InsideOut->new(name => 'dog', sound => 'bark', 4); my $bird = InsideOut->new(name => 'bird', sound => 'peep', 2); $dog->noise; $dog->show_legs; $bird->noise; $bird->show_legs; __END__ The dog goes bark! The dog has 4 legs The bird goes peep! The bird has 2 legs