Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

The @_ array and the lexical variables

by G4143 (Novice)
on Apr 16, 2018 at 10:35 UTC ( [id://1212979]=perlquestion: print w/replies, xml ) Need Help??

G4143 has asked for the wisdom of the Perl Monks concerning the following question:

Perl noob here... I thought I understood the @_ array and its purpose with subroutines but then I found these usages that made run here for answers..
Readonly::Hash my %hash => (data => $data); const my $min => 1; const my $max => 10; open (my $infile, '<', "datafile"); chomp(my $line = <STDIN>);
Now I read and experimented and arrived at the simple subroutine below which reproduces the above but it makes me raise a simple question - What kind of Perl voodoo is happening when you pass my $num to a subroutine parameter list?
sub doIt { $_[0] = $_[1]; } doIt my $num => 4143;
So what is happening with the @_?

Replies are listed 'Best First'.
Re: The @_ array and the lexical variables
by choroba (Cardinal) on Apr 16, 2018 at 10:40 UTC
    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,
      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,
        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;
Re: The @_ array and the lexical variables
by LanX (Saint) on Apr 16, 2018 at 10:48 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212979]
Approved by hdb
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found