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

@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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1074980]
Approved by BrowserUk
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-06-21 03:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.