sub prompt { my( $prompt, $validator, $reprompt ) = @_; # Close out previous pager (if any): select STDOUT; close PAGER; # Prompt the user for a response: my $response; while( 1 ) { print STDERR $prompt; $response = <STDIN>; die "End of input.\n" if ! defined $response; chomp $response; last if $validator->( $response ); $prompt = $reprompt; } # Run output (until next prompt) through pager: my $pager = $ENV{PAGER} || 'less'; open PAGER, '|-', $pager or die "Can't run $pager: $!\n"; select PAGER; return $response; }
If you have output that isn't done by bare 'print', then you probably want to instead save and re-open STDOUT to be just another file handle that goes to PAGER. See open for examples of how to do that correctly.
- tye
In reply to Re: Pipping a script with prompts into less (edit script)
by tye
in thread Pipping a script with prompts into less
by xorl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |