input.
Josef Moellers replied:
No, it won't. You did try your solution? What happened?
I tried, and it didn't work.
Hmmm... it appears you're right. I just tried it with feeding input
from a pipe, and it didn't work, just as you said.
I did have a solution, however. But instead of piping the input in
I specified the file (with the input) as a parameter to be read in with
Perl's diamond operator ("<>"). Since the diamond operator can be used
to read STDIN and to open files specified at the command line, I always
thought they were functionally interchangeable. But as you explained,
if the diamond operator is used to read STDIN, any subsequent read of
STDIN will return EOF.
The reason I tested it by reading the input from a file (instead of
from a pipe) was because I've sometimes had problems with Perl running
under Win32 reading from a pipe (but more on that later in this
post...).
The direct solution to Tong's question is operating system
dependent. Unter Unix/Linux, Chris' suggestion of
(re-) opening "/dev/tty" is obviously a valid solution (afair,
this is how programs like "cpio" work when requiring user
interaction). Under another OS, (re-) opening "CON:"
might work.
For the record, I tested Win32 ActiveState Perl with both "CON" and
"CON:" and they both seem to work.
IMHO a much better solution is to avoid redirection of STDIN and
require the input file to be passed as an argument.
I agree with you there, as that's what I used to test my sample
data...
As I mentioned earlier, I found a problem reading piped input on
Win32. You can reproduce this problem by creating a short, one-line
file named "cat.pl" that consists of the line:
print <>;
Then type, at the Win32-DOS prompt:
cat.pl < cat.pl
You'd think it would print out its contents, right? Instead, I get
nothing (I see the DOS prompt right away).
Today I figured out that I can "fix" this problem by explicitly
calling the Perl interpreter, like this:
perl cat.pl < cat.pl
This works as it should.
So here's a question: Why does omitting "perl" prevent the input
from being read in correctly? I'm sure this is a Win32-DOS-MS issue
and not a Perl bug, but I'm not absolutely certain.
Whatever is causing this, this behavoir is the reason I avoided
testing my original solution with actual piped input (as it seemed
unpredictable at the time), and instead opted to test with the file
specified at the command line, thinking that it would make no
difference. I was wrong, of course, about it making no difference.
(For those interested, my "perl -v" output is:
This is perl, v5.8.0 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2002, Larry Wall
Binary build 805 provided by ActiveState Corp.
http://www.ActiveState.com
Built 18:08:02 Feb 4 2003
)
Anyway, Josef, thanks for pointing out my error.
-- Jean-Luc