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

  • SEARCH
  • Home
  • 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 8979057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:45:28+00:00 2026-06-15T19:45:28+00:00

I came across the following code in List::Util for reduce subroutine. my $caller =

  • 0

I came across the following code in List::Util for reduce subroutine.

my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;

I could understand that reduce function is called as:

my $sum = reduce { $a + $b } 1 .. 1000;

So, I understood the code is trying to reference $a mentioned in the subroutine. But, I am unable to understand the intent correctly.

For reference, I am adding the complete code for subroutine

sub reduce (&@) {
  my $code = shift;
  require Scalar::Util;
  my $type = Scalar::Util::reftype($code);
  unless($type and $type eq 'CODE') {
    require Carp;
    Carp::croak("Not a subroutine reference");
  }
  no strict 'refs';

  return shift unless @_ > 1;

  use vars qw($a $b);

  my $caller = caller;
  local(*{$caller."::a"}) = \my $a;
  local(*{$caller."::b"}) = \my $b;

  $a = shift;
  foreach (@_) {
    $b = $_;
    $a = &{$code}();
  }

  $a;
}
  • 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-06-15T19:45:30+00:00Added an answer on June 15, 2026 at 7:45 pm

    The following aliases package variable $foo to variable $bar.

    *foo = \$bar;
    

    Any change to one changes the other as both names refer to the same scalar.

    $ perl -E'
       *foo = \$bar;
       $bar=123; say $foo;
       $foo=456; say $bar;
       say \$foo == \$bar ? 1 : 0;
    '
    123
    456
    1
    

    Of course, you can fully qualify *foo since it’s a symbol table entry. The following aliases package variable $main::foo to $bar.

    *main::foo = \$bar;
    

    Or, if you don’t know the name at compile time

    my $caller = 'main';
    *{$caller."::foo"} = \$bar;   # Symbolic reference
    

    $bar, of course, can just as easily be a lexical variable as a package variable. And since my $bar; actually returns the variable begin declared,

    my $bar;
    *foo = \$bar;
    

    can be written as

    *foo = \my $bar;
    

    So,

    my $caller = caller;
    local(*{$caller."::a"}) = \my $a;
    local(*{$caller."::b"}) = \my $b;
    

    declares and aliases lexical variables $a and $b the similarly named package variables in the caller’s namespace.

    local simply causes everything to return to their original state once the sub is exited.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following code: var f = function () { var args
I came across the following code fragment in a CouchDB book . function(doc) {
I came across the following code snippet on the Scala mailing list: scala> class
I am using a bulletin board called mybb and came across the following code:-
I came across the following code in a book recently. It says that we
I've been working in backbone.js and came across the following snippet of code. _(view.buttonViews).each(function(button)
I came across following code function currentSlideFromParams() { var result; if (result = window.location.hash.match(/#([0-9]+)/))
I came across the following code, and was told that it means that COL_8888_RED
Recently, I came across the following piece of code in perl that returns the

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.