Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: @_ & @ARGV

by NetWallah (Abbot)
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.
                   -SNL


Comment on Re: @_ & @ARGV
Re^2: @_ & @ARGV
by Athanasius (Monsignor) 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 http://hop.perl.plover.com/.

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-12-28 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls