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.