Hello, I'm trying to use STDIN first to read from the output of the previous process and then to read from the terminal. According to http://perldoc.perl.org/perlop.html#I%2fO-Operators I should be able to do this:

"The <> symbol will return undef for end-of-file only once. If you call it again after this, it will assume you are processing another @ARGV list, and if you haven't set @ARGV , will read input from STDIN."

That's what I need! My simplified program, called pgm, is the following:

#!/opt/local/tools/bin/perl use 5.24.0; use strict; use warnings; my $response; unless (-t STDIN) { while (<STDIN>) { chomp; print "input line is $_\n"; } } print "Now enter something: "; $response = <>; print "You entered -->", $response, "<--\n";

and I invoke it as follows:

echo Hello | pgm

But it doesn't wait for anything to be entered at the <>:

echo Hello | pgm input line is Hello Use of uninitialized value $response in print at /userdata/cfor/utils/ +worklib/pgm line 25. Now enter something: You entered --><--

Wouldn't the while (<STDIN>) end because it encountered EOF, after which it should then read input from STDIN?


In reply to Unable to reuse STDIN by fireblood

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.