This is the sort of thing that Term::ReadLine is for. Here's a simple example -- try piping the output of "find" or "ls" to this script:
#!/usr/bin/perl use strict; use Term::ReadLine; my @list = <>; # get pipeline input print scalar @list, " file names found\n"; my $term = new Term::ReadLine; my $prmt = "would you like to see the next file name? [y] "; while ( defined( $_ = $term->readline($prmt))) { last if ( /^n/i ); print shift @list; }
The module takes care of keeping terminal input separate from pipeline/stdin input. If you want, you should be able to interleave the "<>" reads and the "$term->readline" prompted inputs (e.g. read some file names, and when one looks interesting, prompt the user for something). But unless the pipeline input is expected to get really huge, I'd prefer reading it all first, then going over the list in memory.

update: Another common approach, which might save the user some command line typing (and keep your script simple yet flexible) is to open your input pipeline command from inside the script:

... open( FIND, '-|', 'find', $basedir, @opts ) or die "find: $!"; while (<FIND>) { if ( theres_something_about( $_ )) { print "What do you want with $_ ? "; chomp ( my $desired_outcome = <> ); #(fixed missing close-pa +ren) do_something_to( $_, $desired_outcome ); } } close FIND;
Args that the user would have given to the upstream process could just as well be included among your own script's @ARGV and passed to the pipeline open.

one more update: (geez, I seem to do this a lot) There are good reasons for using the "-print0" option on the "find" command, and setting $/ (input_record_separator in perl) to "\x0" when reading input from "find ... -print0". It can happen that file names in a unix file system might contain unexpected characters (e.g. line-feed). Yes, really.


In reply to Re: piped AND prompted input by graff
in thread piped AND prompted input by bemfica

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.