in reply to Benchmark Runs Only Once

&$func does not give you a reference to the function, it calls the function:

> perl -wle "sub foo { print 'In foo' }; my $func='foo'; &$func" In foo

Had you used strict, Perl would have prevented you from making this error.

> perl -Mstrict -wle "sub foo { print 'In foo' }; my $func='foo'; &$fu +nc" Can't use string ("foo") as a subroutine ref while "strict refs" in us +e at -e line 1.

Replies are listed 'Best First'.
Re^2: Benchmark Runs Only Once
by ikegami (Patriarch) on Sep 30, 2010 at 17:06 UTC
    Using strict wouldn't have helped since he wants to get a reference to a symbol named in a variable. He should still use it, though, and just disable it where he needs it disable.
    timethese(..., do { no strict 'refs'; \&$func });

    An alternative solution comes from the realisation that timethese can also take a string of Perl code.

    timethese(..., $func.'()');