in reply to Reading from STDIN

$resp = <>; is reading the next line in the next file in @ARGV, not from STDIN. You probably mean $resp = <STDIN>; there.

See "I/O Operators" in perlop to see exactly what <> is doing.

Replies are listed 'Best First'.
Re: Re: Reading from STDIN
by jweed (Chaplain) on Nov 27, 2003 at 18:44 UTC
    To clarify holo's response, <> will read from STDIN only when all the files on the commandline have been exausted. In scripts without arguments, this will always read from STDIN. But since your script takes arguments, it won't DWIM (at least, not right away...)


    Who is Kayser Söze?

      <> 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.

        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?