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

@ARGV in while loop

by lightoverhead (Pilgrim)
on Feb 14, 2014 at 17:29 UTC ( #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 (Pope) 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
Node Status?
node history
Node Type: perlquestion [id://1074980]
Approved by BrowserUk
Front-paged by toolic
help
Chatterbox?
[Discipulus]: thanks choroba.. todo list need a pager..

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-11-24 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (348 votes). Check out past polls.

    Notices?