Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

@ARGV in while loop

by lightoverhead (Pilgrim)
on Feb 14, 2014 at 17:29 UTC ( [id://1074980]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I may ask a stupid question, but why the $ARGV[0] in while loop cannot be accessed?

I thought @ARGV is global variable and should be accessed anywhere, of course from within of while loop.

did I miss something?

print $ARGV[0],"\n"; #printed while(<>){ print "$ARGV[0]\n"; #cannot be printed }

Replies are listed 'Best First'.
Re: @ARGV in while loop
by BrowserUk (Patriarch) on Feb 14, 2014 at 17:38 UTC
    why the $ARGV[0] in while loop cannot be accessed?

    Because the diamond operator (when used with the null filehandle <>) consumes the contents of @ARGV.

    See the section of perlop I/O-Operators that starts with the sentence:

    The null filehandle <> is special:
    for the details.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thank you BrowserUK. You are right. It's good to learn.

      what about this:

      for(;;){ print "3:\t$ARGV[0]\n"; #cannot print either last; }
      Is $ARGV[0] consumed too?

        my fault

        I put for(;;;) after while().

        if run for(;;) first, it will print $ARGV[0]

Re: @ARGV in while loop
by Kenosis (Priest) on Feb 14, 2014 at 17:53 UTC

    However, if you want to print what <> is currently consuming, you can:

    use strict; use warnings; print $ARGV[0], "\n"; #printed while (<>) { print "$ARGV\n"; #can be printed }

      Thank you. You are right.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found