Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: @_ & @ARGV

by NetWallah (Canon)
on Oct 07, 2012 at 14:56 UTC ( #997684=note: print w/replies, xml ) Need Help??

in reply to @_ & @ARGV

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.

Replies are listed 'Best First'.
Re^2: @_ & @ARGV
by Athanasius (Chancellor) on Oct 07, 2012 at 15:12 UTC

    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

Re^2: @_ & @ARGV
by tobyink (Abbot) on Oct 07, 2012 at 15:19 UTC

    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'
Re^2: @_ & @ARGV
by dushyant (Acolyte) on Oct 07, 2012 at 17:41 UTC

    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: note [id://997684]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-05-23 05:34 GMT
Find Nodes?
    Voting Booth?