Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Benchmark cmpthese() removing '.' char from input param

by stevieb (Canon)
on Mar 12, 2016 at 15:51 UTC ( #1157557=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1157557]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-05-22 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?