package Xyzzy; { # scope of cache my $cached = {}; sub new { my $class = shift; # may be a derived subclass my @args = @_; # reuse singleton object iff a valid one is available. delete $cached->{$class} if ($class->cache_invalid()); return $cached->{$class} if ($cached->{$class}); my $obj = {}; # no object found in cache, bless( $obj, $class ); # so create a new one. $obj->_initialize( @args ); # expensive! $cached->{$class} = $obj; # cache data by class id return $obj; } } # scope of cache … other methods here …