Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Override "null filehandle"

by philipbailey (Curate)
on Sep 04, 2023 at 08:49 UTC ( [id://11154239] : note . print w/replies, xml ) Need Help??


in reply to Re: Override "null filehandle"
in thread Override "null filehandle"

That's not quite true:

% echo This is from STDIN|perl -e'print for <>' This is from STDIN % echo This is from ARGV > input1 % echo This is also from ARGV > input2 % perl -e'print for <>' input1 input2 This is from ARGV This is also from ARGV

The <> operator looks first at at @ARGV and iterates line by line through each file specified as arguments to the script. But if there are no arguments (@ARGV is empty) then it falls back to reading from STDIN. This behaviour is documented in perlop, already linked to by the OP.

Replies are listed 'Best First'.
Re^3: Override "null filehandle"
by haukex (Archbishop) on Sep 04, 2023 at 09:40 UTC
    The diamond operator reads from ARGV, not STDIN.
    That's not quite true

    choroba is correct though, in that <> is always equivalent to <ARGV>, as is described in the very documentation you referenced. The magical behavior you describe is not that of the <> operator in general, it is that of the ARGV filehandle, which modifies @ARGV in order for STDIN to be read automatically via the ARGV handle.

    Reworded for clarity, shortly after posting.

Re^3: Override "null filehandle"
by ikegami (Patriarch) on Sep 05, 2023 at 22:06 UTC

    <> always reads from ARGV. It's just that ARGV might in turn read from STDIN. Whether it does or not depends on @ARGV.