BEGIN { use Benchmark; my ($oldb, $newb); # inside BEGIN for closure sub mybench { return unless ($opt_b); $newb = new Benchmark; if ($oldb) { # i.e. don't run first time through print timediff($oldb, $newb); } $oldb = $newb; } } Getopt::Std; getopts('b'); mybench(); # some code here mybench(); # some more code here mybench();