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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:12:57+00:00 2026-05-18T01:12:57+00:00

I was always sure that if I pass a Perl subroutine a simple scalar,

  • 0

I was always sure that if I pass a Perl subroutine a simple scalar, it can never change its value outside the subroutine. That is:

my $x = 100;
foo($x);
# without knowing anything about foo(), I'm sure $x still == 100

So if I want foo() to change x, I must pass it a reference to x.

Then I found out this is not the case:

sub foo {
 $_[0] = 'CHANGED!';
}
my $x = 100;
foo($x);
print $x, "\n"; # prints 'CHANGED!'

And the same goes for array elements:

my @arr = (1,2,3);
print $arr[0], "\n"; # prints '1'
foo($arr[0]);
print $arr[0], "\n"; # prints 'CHANGED!'

That kinda surprised me. How does this work? Isn’t the subroutine only gets the value of the argument? How does it know its address?

  • 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-18T01:12:58+00:00Added an answer on May 18, 2026 at 1:12 am

    In Perl, the subroutine arguments stored in @_ are always aliases to the values at the call site. This aliasing only persists in @_, if you copy values out, that’s what you get, values.

    so in this sub:

    sub example {
       # @_ is an alias to the arguments
       my ($x, $y, @rest) = @_;  # $x $y and @rest contain copies of the values
       my $args = \@_;  # $args contains a reference to @_ which maintains aliases
    }
    

    Note that this aliasing happens after list expansion, so if you passed an array to example, the array expands in list context, and @_ is set to aliases of each element of the array (but the array itself is not available to example). If you wanted the latter, you would pass a reference to the array.

    Aliasing of subroutine arguments is a very useful feature, but must be used with care. To prevent unintended modification of external variables, in Perl 6 you must specify that you want writable aliased arguments with is rw.

    One of the lesser known but useful tricks is to use this aliasing feature to create array refs of aliases

    my ($x, $y) = (1, 2);
    
    my $alias = sub {\@_}->($x, $y);
    
    $$alias[1]++;  # $y is now 3
    

    or aliased slices:

    my $slice = sub {\@_}->(@somearray[3 .. 10]);  
    

    it also turns out that using sub {\@_}->(LIST) to create an array from a list is actually faster than
    [ LIST ] since Perl does not need to copy every value. Of course the downside (or upside depending on your perspective) is that the values remain aliased, so you can’t change them without changing the originals.

    As tchrist mentions in a comment to another answer, when you use any of Perl’s aliasing constructs on @_, the $_ that they provide you is also an alias to the original subroutine arguments. Such as:

    sub trim {s!^\s+!!, s!\s+$!! for @_}  # in place trimming of white space
    

    Lastly all of this behavior is nestable, so when using @_ (or a slice of it) in the argument list of another subroutine, it also gets aliases to the first subroutine’s arguments:

    sub add_1 {$_[0] += 1}
    
    sub add_2 {
        add_1(@_) for 1 .. 2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure I worded that correctly but python and time always confuses
This really has my stumped today. I'm sure its simple, but... Here is my
Is there a way to change the specs file so that it will pass
I'm trying to make sure my web application is always centered on the screen
hey guys was hoping you could help me out, Not sure if I always
I always read that one reason to chose a RESTful architecture is (among others)
I always thought that you could use OR in a LIKE statment to query
You always read that for-in loops should check o.hasOwnProperty(k) to skip over Object.prototype. Well,
Not entirely sure how to do this, but I noticed that Facebox works perfectly,
Say I'm targeting all images on a page and want to make sure that

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.