package Example::Module; use strict; # note, we don't actually have to use the reference that we # bless. we just need it for its unique reference id. sub new { bless {}, shift } { my %value; my %counter; sub accumulate { my $self = shift; for (@_) { $value{ $self } += $_; $counter{ $self }++; } } sub printvalue { my $self = shift; printf "%s has value %2d from %2d iterations.\n", $self, $value{$self}, $counter{$self}; } # prevent memory leaks. DESTROY { my $self = shift; delete $value{$self}; delete $counter{$self}; } } 1;