开发者

Perl: cmpthese text vs anonymous sub problems with parameters passed

开发者 https://www.devze.com 2023-01-25 19:38 出处:网络
If you read about cmpthese in the Perl Benchmark module\'s documentation, it states that cmpthese or timethese can be used with code in either text or subroutine references. The documentation seems to

If you read about cmpthese in the Perl Benchmark module's documentation, it states that cmpthese or timethese can be used with code in either text or subroutine references. The documentation seems to imply these forms are completely interchangeable:

# Use Perl code in strings...
timethese($count, {
'Name1' => '...code1...',
'Name2' => '...code2...',
});
# ... or use subroutine references.
timethese($count, {
'Name1' => sub { ...code1... },
'Name2' => sub { ...code2... },
});

I am having difficulties with passed parameters with the string form versus subroutine references form with cmpthese. E开发者_开发知识库ither the values in @array do not get passed or I have a run-time error.

I have the following code:

#!/usr/bin/perl
use strict; use warnings;
use Benchmark qw(:all);

my @array = qw( first second third );

sub target {
    my $str =  $_[0];
    print "str=$str\n";
}

sub control {
    print "control: array[0]=$array[0]\n";
}

my $sub_ref=\⌖
my $control_ref=\&control;

print "\n\n\n";

# ERROR: array does not get passed...
cmpthese(1, {
    'target text' => 'target(@array)',
    'control 1' => 'control()', 
});

# This is OK...
cmpthese(1, {
    'sub code ref' => sub { target(@array) },
    'control 2' => sub { control() },
});

# This is OK too...
cmpthese(1, {
    'target sub' => sub { $sub_ref->(@array) },
    'control 3' => sub { $control_ref->() },
});

# fixed paramenters work:
cmpthese(1, {
    'target text fixed' => 'target("one", "two", "three")',
    'control 4' => 'control()', 
});

# Run time error...
cmpthese(1, {
    'text code ref' => '$sub_ref->(@array)',
    'control 5' => '$control_ref->()',
});

All the forms I have work correctly with eval so I think this may be an issue with Benchmark? I have used all my google foo to try and find some documented difference between the two forms but I cannot.

Does anyone know the reason that my simple examples above do not seem to work as expected? The comments in the code indicate the problems I am having on OS X, Perl 5.10.0.


The text passed to cmpthese and timethese gets propogated to an eval statement deep in the bowels of Benchmark. Unless the arguments in the text are literals or global variables, they won't be in scope by the time they are evaluated, and you get a run-time error.

Use the anonymous sub version of the arguments to provide lexical closure for your arguments and all will be well.


I haven't looked in too much detail at this, but my guess is that when Benchmark evals the strings into code, the lexical variable @array is not in scope. Things would probably work if you made @array an our variable.

But in general, I find it is easier just to use code refs.

0

精彩评论

暂无评论...
验证码 换一张
取 消