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

Re: Testing <> for undefined

by graff (Chancellor)
on Jul 08, 2004 at 23:38 UTC ( [id://373000]=note: print w/replies, xml ) Need Help??


in reply to Testing <> for undefined

Here's something that is relevant to your question, but has not been explained yet in the other replies that I read. Part of the magic of  while (<>) is that it will handle all of the following types of command line usage:
# name one file: my_script.pl input.file # name multiple files: my_script.pl first.file second.file ... # use redirection from a file to STDIN: my_script.pl < some.file # use a pipeline to STDIN: other_process | my_script.pl # e.g.: cat *.file | my_script.pl
That is, if one or more files are named as args to the script, a  while (<>) loop will step through and read each file in turn. On the other hand, if data is being fed to the script on its STDIN (via redirection or pipeline),  while (<>) will read that.

(The default behavior does not let you do both: if files are named as args, the plain, empty diamond operator reads the files and does not read STDIN.)

Anyway, the reason why you have to check @ARGV first, and not test the diamond operator, is that when @ARGV is empty, the script will wait until there is either input data (i.e. one whole line) or an EOF condition on STDIN, and you won't be able to check the result of the diamond operator until one of those things happens.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://373000]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found