Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:25:25+00:00 2026-05-18T04:25:25+00:00

If you read about cmpthese in the Perl Benchmark module’s documentation, it states that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:25:26+00:00Added an answer on May 18, 2026 at 4:25 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read about a string/word that did so the Routing crashed but I can't
I read about virtual constructors are used for implementing some design patterns, but didn't
Read about Server push here . I want to push data to client from
I read about the Conditional attribute today. According to MSDN: Applying ConditionalAttribute to a
I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm
I read about the dynamic control creation in ASP.NET this piece of text: ...When
I read about WPF and I have following concerns: Normally when we develop applications
I read about Compound property names in the The Spring Framework (2.5) - Reference
Ive read about it and to be honest it all seems like a bunch
I read about HTTP persistent connection but somehow I don't seem to understand what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.