use warnings; use strict; { my @btime; sub BEGIN_TIME { push @btime, time; } sub END_TIME { @btime or die "error: END_TIME without BEGIN_TIME"; my($btime, $etime) = (pop @btime, time); warn "elapsed time was ", $etime - $btime, " s\n"; } END { @btime and warn "warning: BEGIN_TIME without END_TIME"; } } sub foo { sleep 2; END_TIME; sleep 2; } BEGIN_TIME; sleep 3; foo(); __END__