I don't understand,
My fault. I misunderstood the problem. Basically, I made the same mistake you did.
Starting fresh:
If you wanted to read to EOF, you'd write
while (<STDIN>) { print; }
You want to read to EOF twice, so you'd write
for (1..2) { while (<STDIN>) { print; } }
You don't want to read a line at a time, so
local $/; for (1..2) { while (<STDIN>) { print; } }
Now count the number of times <STDIN> executed: 4.
local $/; $_ = <STDIN>; # Returns first chunk print; $_ = <STDIN>; # Returns EOF $_ = <STDIN>; # Returns second chunk print; $_ = <STDIN>; # Returns EOF
In all but the last snippet, you are calling readline (<>) in scalar context. It returns undef on eof (and on error).
readline (like glob and each) needs to signal when it has finished return results when used as an iterator. You're forgetting to absorb this signal.
The simplest solution is to call readline in list context.
local $/; ($_) = <STDIN>; # Returns first chunk ($_) = <STDIN>; # Returns second chunk
In reply to Re^3: how to read STDIN interactively
by ikegami
in thread how to read STDIN interactively
by Allasso
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |