Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: The @_ array and the lexical variables

by choroba (Cardinal)
on Apr 16, 2018 at 10:40 UTC ( [id://1212980]=note: print w/replies, xml ) Need Help??


in reply to The @_ array and the lexical variables

Elements of @_ are aliases to the arguments, i.e. when you change $_[0] directly, you change the original argument, which in this case happens to be $num, respectively %hash.

See perlsub:

The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable).

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: The @_ array and the lexical variables
by G4143 (Novice) on Apr 16, 2018 at 11:15 UTC
    Could you demonstrate that behaviour with a normal array or how I would arrive at that behaviour? Something like:
    my @arr = (my $one, my $two, my $three);
      No, it's the magic behind @_ and calling a subroutine that causes the aliasing. Normal arrays don't alias.

      Another aliasing construct if for:

      my $x = 1; for my $y ($x) { $y++; } print $x; # 2

      See also Data::Alias .

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        OK. Thanks for the clarification.
      our @arr; local *arr = sub { \@_ }->( my $one, my $two, my $three );
      or
      use Data::Alias qw( alias ); alias my @arr = ( my $one, my $two, my $three );
      or
      use feature qw( refaliasing ); my @arr; \$arr[@arr] = \my $one; \$arr[@arr] = \my $two; \$arr[@arr] = \my $three;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found