in reply to Re: Re: Reading from STDIN
in thread Reading from STDIN

<> will not read from STDIN if there are arguments; not even when it runs out of files. Proof:

$ echo "one" > file1; echo "two" > file2 $ echo "three" | perl -e 'print while <>' file1 file2 one two

This script requires at least one argument to work so I assume that unless @ARGV is cleared, <> will never read from STDIN.

Replies are listed 'Best First'.
Re: Re: Re: Re: Reading from STDIN
by jweed (Chaplain) on Nov 28, 2003 at 19:33 UTC
    According to perlop, we are both sorta right:
    The first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames.
    So if there is nothing in @ARGV to begin with, it will read from STDIN. But as it reads from @ARGV, it unshifts the files from the array. So when the loop ends, $#ARGV = 0. But...
    The <> symbol will return undef for end-of-file only once. If you call it again after this, it will assume you are processing another @ARGV list, and if you haven't set @ARGV, will read input from STDIN.
    So it will return false once and then begin to read from STDIN.


    Who is Kayser Söze?