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. |