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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|