Thanks to all of you
My purpose was to browse incomming records one at a time, and let the user decide if he wanted to see one more.
I combined several of the ideas suggested here:
- using <> for reading incomming records, so I don't have to care if data comes from pipe or redirection or files supplied on command line,
- -t STDIN for determining if STDIN is a terminal,
- open /dev/tty to get the user input (which was my original problem)
- and Term::Readkey to avoid the need of hitting an extra RETURN.
Finally the script turned out like this (only skeleton showned):
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
# Is data comming?
usage() unless ( ( ! -t STDIN ) || ((defined($ARGV[0])) && ( -f $ARGV[
+0] )) );
open(TTY,"+</dev/tty") or die "Couldn't open tty: $!\n";
# input record separator is \x1d
$/ = "\x1D";
while ( my $rec = <>) {
next unless defined $rec;
show_formatted_record();
print TTY "\n\nOne more? y/n ";
ReadMode('cbreak', *TTY);
my $answer = ReadKey(0, *TTY);
ReadMode('normal', *TTY);
last if $answer !~ /^[yY]$/;
}
print "\n";
#---- functions:
sub usage { print "usage: ...\n"; exit 1; }
sub show_formatted_record { print "..."; }
Thanks again,
L
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.