http://qs321.pair.com?node_id=1157557

stevieb has asked for the wisdom of the Perl Monks concerning the following question:

While doing a benchmark between a C implementation of some file work to a pure Perl implementation, I found what I think might be an issue in Benchmark cmpthese, whereby it seems to remove a . character from input param to the subs being tested.

Here's an extremely minimalistic version that demonstrates the issue:

use warnings; use strict; use Benchmark qw(:all); my $fname = 'a.txt'; cmpthese( 1, { p_read => "p_read($fname)", }); sub p_read { my $fname = shift; print "$fname\n"; } __END__ atxt # cmpthese output redacted

This happens with Benchmark v1.12 on perl v5.14.4 and Benchmark v1.2 on perl v5.22.1.

Have I overlooked something in the docs perhaps that anyone knows about?

Replies are listed 'Best First'.
Re: Benchmark cmpthese() removing '.' char from input param
by dave_the_m (Monsignor) on Mar 12, 2016 at 16:10 UTC
    The code gets evalled, so you need quotes:
    p_read => "p_read('$fname')",
    Otherwise you get the bareword 'a' concatenated with the bareword 'txt'.

    Dave.

      D'oh! Of course :) Thanks guys!

      -stevieb

Re: Benchmark cmpthese() removing '.' char from input param
by 1nickt (Canon) on Mar 12, 2016 at 16:07 UTC

    Hi stevieb, that's odd!

    Same results here on 5.22 and 5.8.8 under darwin. It seems to be an issue with variable interpolation by Benchmark:

    use strict; use warnings; use Benchmark qw(:all); cmpthese( 1, { p_read => "p_read('a.txt')", }); sub p_read { my $fname = shift; print "$fname\n"; } __END__
    Output:
    a.txt ...

    The way forward always starts with a minimal test.