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. Either 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.
I haven’t looked in too much detail at this, but my guess is that when
Benchmarkevals the strings into code, the lexical variable@arrayis not in scope. Things would probably work if you made@arrayanourvariable.But in general, I find it is easier just to use code refs.