It would be better to use <STDIN> instead of <>, as <> has magical properties when there are command line arguments. It will treat the command line arguments as a list of files to be read in sequence.
In the future please post all of the relevant code needed to reproduce your problem. Please make sure it is only the relevant code - trim it down until the problem stops occurring. And of course, the posted code should be runnable.
Also, please read the following:
How (not) to ask a question - Only Post Relevant Code | [reply] [d/l] [select] |
For perl command line switches, see perlrun. These apply when calling perl directly from the command line, or when put on the shebang line, e.g. #!/usr/bin/perl.
As for switches to your script - perl doesn't differentiate between switches and files, all command line arguments show up in @ARGV, which are taken as files to be opened and read in sequence, if you use <> - you have to process command line switches yourself, and remove switches and switch arguments from @ARGV. There are modules for that task - I recommend Getopt::Std or Getopt::Long.
Regarding <STDIN> or <>, see perlop:
The null filehandle <> is special: it can be used to emulate the behavior
of sed and awk. Input from <> comes either from standard input, or
from each file listed on the command line. Here's how it works: 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 <> operates on STDIN only if the array @ARGV is empty.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] [select] |
If you are passing in command line args AND are redirecting/piping STDIN into your perl script, that you must not use <> to loop over the STDIN. Instead, use <STDIN> and refer to your arguments via the @ARGV array. Otherwise, Perl thinks that $ARGV[0] is a file name when it encounters just a <>.
This works:
% prepend.pl bob < some.txt
while <STDIN> {
print "$ARGV[0]: $_"
}
This doesn't:
% prepend.pl bob < some.txt
while <> {
print "$ARGV[0]: $_"
}
You may also want to check out Multiple STDIN sources. | [reply] [d/l] [select] |