Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

@_ & @ARGV

by dushyant (Acolyte)
on Oct 07, 2012 at 14:15 UTC ( #997680=perlquestion: print w/ replies, xml ) Need Help??
dushyant has asked for the wisdom of the Perl Monks concerning the following question:

Please explain how and why value of @_ is keeps changing in same subroutine "formatlist" in below code

[root@mail ~]# perl -w -w out return Command Line in return Command Line: -w [root@mail ~]# cat sub formatlist { my @list = @_; print @_, ' out return', "\n"; return sub { print @_, ' in return', "\n"; my $title = shift; print "$title: ", join(' ',@list),"\n"; } } $arguments = formatlist(@ARGV); &$arguments('Command Line'); [root@mail ~]#

Comment on @_ & @ARGV
Download Code
Replies are listed 'Best First'.
Re: @_ & @ARGV
by moritz (Cardinal) on Oct 07, 2012 at 14:48 UTC

    @_ contains the arguments to the current (inner-most) subroutine, which is why the @_ in the inner subroutine is different from that of the outer subroutine. shift acts on @_ by default, which is why it gets the first argument to the inner-most function.

    Strictly speaking,

    my @list = @_; print @_, ' out return', "\n";

    is the only content directly in sub formatlist, the rest is in an inner functio which has its own @_.

Re: @_ & @ARGV
by NetWallah (Abbot) on Oct 07, 2012 at 14:56 UTC
    The @_ parameter to a subroutine gets populated at RUN TIME (or the time the subroutine gets called), with whatever is passed to it at that time.

    In tHe first call, you pass @ARGV to the 'formlist' sub.
    This returns an ANONYMOUS sub that is assigned to $arguments.
    You call the Anonymous sub, pasisng it "Command Line", which, as you can see is received and printed just fine.

    Perhaps we can explain it better if you tell us what you were expecting, and why.

    In case you were unaware, you created a "closure" around @list, preserving it's value.

                 I hope life isn't a big joke, because I don't get it.

      Closures are discussed in perlsub, but the best explanation of closures that I know of is in Chapter 3 of Higher-Order Perl by Dominus, available free online at

      Hope that helps,

      Athanasius <°(((><contra mundum

      Further, only lexical variables can be closed over. @_ is not a lexical variable (it's not declared with my) so can't be closed over.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks for the replies guys, now I got it. I was expecting the value of @_ will remain same (“-w”) throughout the code. But for subroutine formatlist the value of @_ is “-w” and for anonymous subroutine @_ is “Command Line”. I removed the last line  &$arguments(‘Command Line’) from above code, and got “-w out return” printed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997680]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-11-29 01:44 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls