in reply to How to code Pascal's keypressed function in Perl

You can use the select function like this for non-blocking input. This isn't exactly what you asked for because it won't respond to a simple keypress, you have to enter a string ending with a newline (Enter key).

YuckFoo

#!/usr/bin/perl use strict; my $timeout = 5; while (1) { my $str = getstr($timeout); if ($str ne '') { print "Got it! $str"; } else { print "I got nothing.\n"; } } #----------------------------------------------------------- sub getstr { my ($timeout) = @_; my ($rin, $rout); vec($rin,fileno(STDIN),1) = 1; print "You have $timeout seconds to enter a string:\n"; my $input = select($rout=$rin, undef, undef, $timeout); if ($input) { return <STDIN>; } else { return ''; } }

Replies are listed 'Best First'.
Re: Re: How to code Pascal's keypressed function in Perl
by Jenda (Abbot) on Aug 04, 2002 at 23:08 UTC

    Plus it would not work at all under Windows since there select() (the 4 parameter form) works only on sockets :-(((

      Jenda