Benchmark: using code refs
authorHugo van der Sanden <hv@iii.co.uk>
Tue, 4 Mar 1997 11:32:11 +0000 (11:32 +0000)
committerChip Salzenberg <chip@atlantic.net>
Sat, 1 Mar 1997 06:40:49 +0000 (18:40 +1200)
It often irritates me that I can't supply a coderef to the Benchmark::
routines: here's a small patch to allow them.

p5p-msgid: 199703041132.LAA07613@tyree.iii.co.uk

lib/Benchmark.pm

index c382fcb..ee7cf74 100644 (file)
@@ -329,7 +329,9 @@ sub runloop {
        last if $pack ne $curpack;
     }
 
-    my $subcode = "sub { package $pack; my(\$_i)=$n; while (\$_i--){$c;} }";
+    my $subcode = (ref $c eq 'CODE')
+       ? "sub { package $pack; my(\$_i)=$n; while (\$_i--){&\$c;} }"
+       : "sub { package $pack; my(\$_i)=$n; while (\$_i--){$c;} }";
     my $subref  = eval $subcode;
     croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@;
     print STDERR "runloop $n '$subcode'\n" if ($debug);