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 3935638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:51:11+00:00 2026-05-19T23:51:11+00:00

In this question the poster asked how to do the following in one line:

  • 0

In this question the poster asked how to do the following in one line:

sub my_sub {
    my $ref_array = shift;
    my @array = @$ref_array;
}

which with my knowledge of the basic Perl magic I would avoid by simply using something like:

sub my_sub {
    my $ref_array = shift;
    for (@$ref_array) {
      #do somthing with $_ here
    };

    #use $ref_array->[$element] here
}

However in this answer one of SO’s local monks tchrist suggested:

sub my_sub {
  local *array = shift();
  #use @array here
}

When I asked

In trying to learn the mid-level Perl
magic, can I ask, what is it that you
are setting to what here? Are you
setting a reference to @array to the
arrayref that has been passed in? How
do you know that you create @array and
not %array or $array? Where can I
learn more about this * operator
(perlop?). Thanks!

I was suggested to ask it as a new post, though he did give nice references. Anyway, here goes? Can someone please explain what gets assigned to what and how come @array gets created rather than perhaps %array or $array? Thanks.

  • 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-19T23:51:11+00:00Added an answer on May 19, 2026 at 11:51 pm

    Assignment to a glob

    *glob = VALUE
    

    contains some magic that depends on the type of VALUE (i.e., return value of, say, Scalar::Util::reftype(VALUE)). If VALUE is a reference to a scalar, array, hash, or subroutine, then only that entry in the symbol table will be overwritten.

    This idiom

    local *array = shift();
    #use @array here
    

    works as documented when the first argument to the subroutine is an array reference. If the first argument was instead, say, a scalar reference, then only $array and not @array would be affected by the assignment.

    A little demo script to see what is going on:

    no strict;
    
    sub F {
      local *array = shift;
    
      print "\@array = @array\n";
      print "\$array = $array\n";
      print "\%array = ",%array,"\n";
      print "------------------\n";
    }
    
    $array = "original scalar";
    %array = ("original" => "hash");
    @array = ("orignal","array");
    
    $foo = "foo";
    @foo = ("foo","bar");
    %foo = ("FOO" => "foo");
    
    F ["new","array"];        # array reference
    F \"new scalar";          # scalar reference
    F {"new" => "hash"};      # hash reference
    F *foo;                   # typeglob
    F 'foo';                  # not a reference, but name of assigned variable
    F 'something else';       # not a reference
    F ();                     # undef
    

    Output:

    @array = new array
    $array = original scalar
    %array = originalhash
    ------------------
    @array = orignal array
    $array = new scalar
    %array = originalhash
    ------------------
    @array = orignal array
    $array = original scalar
    %array = newhash
    ------------------
    @array = foo bar
    $array = foo
    %array = FOOfoo
    ------------------
    @array = foo bar
    $array = foo
    %array = FOOfoo
    ------------------
    @array =
    $array =
    %array =
    ------------------
    @array = orignal array
    $array = original scalar
    %array = originalhash
    ------------------
    

    Additional doc at perlmod and perldata. Back in the days before references were a part of Perl, this idiom was helpful for passing arrays and hashes into subroutines.

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

Sidebar

Related Questions

I've seen this question asked before and tried following the solutions posted. Unfortunately, they
This same question was asked here and an answer was given which is workable,
I know this question has been asked a million of times here at stackoverflow,
So, I previously asked this question: Can someone help me compare using F# over
I asked a question regarding Bi linear transformations and received this answer: From that
I know this has got to be the simplest-sounding question ever asked about ASP.Net
I know this question has been kind of asked and have looked into the
I believe this question is slightly different than similar ones asked on here before
In line with this question: Lightweight web app server for PHP? The above question
This question has been asked before but the accepted solution (given by the question

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.