I apologize for the partial, unrunnable code in my post; I shouldn't post when sleep-deprived and frustrated. If I had taken the time to figure out a minimal script that reproduces the problem, I could have asked a much more specific question -- which I'll do now.
The sample script provided by jethro worked fine on my system. I then tried to figure out what was the salient difference between his script and mine, and quickly remembered that my script was (because of the huge number of text files it might need to handle) reading a long list of filenames from stdin rather than the command line. It's normally run in a pipe from a find command, like so:
find $HOME -name \*.txt | textual-slideshow.pl $*
It slurps the filenames from standard input, then reads random sample paragraphs from some of them, prints random paragraphs a line at a time, and repeats. My simplified version that's partway between jethro's script and mine looks like this:
#! /usr/bin/perl
use Term::ReadKey;
use Time::HiRes qw(time);
my @slurp_stdin = <>;
ReadMode 3;
while (1) {
my $key;
my $wait_until = time + 3;
while ( time < $wait_until ) {
$key = ReadKey( -1 );
if ( defined $key ) {
print STDERR "keystroke $key\t";
}
}
print "Something\n";
}
And it has the same problem as my script, not surprisingly. The problem clearly has to do with running in an environment where stdin is redirected.
So I tried to fix that by closing STDIN and reopening it after reading the filenames. That didn't help. I tried adding an explicit STDIN argument to the ReadMode and ReadKey calls; that didn't help either. This is what I've got now, and it also has the same problem as my original script or the above modification of jethro's script:
#! /usr/bin/perl -w
use Term::ReadKey;
use Time::HiRes qw(time);
my @slurp_stdin = <STDIN>;
close STDIN;
open STDIN, "-";
while (1) {
my $key;
my $wait_until = time + 3;
while ( time < $wait_until ) {
ReadMode 3, STDIN; # 'noecho';
$key = ReadKey( -1, STDIN );
if ( defined $key ) {
print STDERR "keystroke $key\t";
}
ReadMode 0, STDIN;
}
print "Something\n";
}
This repeatedly prints "Something"; but when I press keys, I don't get the "keystroke (key value)" message, only the value of the key.
My full original script is at
http://jimhenry.conlang.org/scripts/textual-slideshow.zip.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.