package Stopwatch; use strict; use warnings; use DateTime; use Class::InsideOut qw( public register id ); public timer => my %timer => { set_hook => { $_->isa('DateTime') or die "must be a DateTime object" } }; sub new { my $self = register( bless \(my $s) ); $timer{ id $self } = DateTime->now; return $self; } sub diff { my $self = shift; $time{ id $self } - DateTime->now; } # ... my $sw = Stopwatch->new; # ... wait a moment print $sw->diff;