Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: input problem

by edan (Curate)
on Dec 19, 2004 at 09:21 UTC ( [id://415966]=note: print w/replies, xml ) Need Help??


in reply to input problem

I think you mean that you want to loop "non-blocking", meaning that you want to continue processing even if there is no user input. To do that, one usually uses 4-arg select, or the OO-wrapper interface IO::Select. Here's a trivial example to get you started.

use IO::Select; # loop every 1 second my $timeout = 1; my $s = IO::Select->new(); $s->add(\*STDIN); while ( 1 ) { if ( my @ready = $s->can_read($timeout) ) { # we got input for my $fh ( @ready ) { my $input = <$fh>; print "got: $input"; } } else { # no input } # just to show that we're looping print scalar localtime; }

Update: I just remembered what select says about mixing buffered reading and "select", so even though the above code works, you might want to substitute the read via <$fh> with:

my $input; sysread( $fh, $input, 1024);
--
edan

Replies are listed 'Best First'.
Re^2: input problem
by BUU (Prior) on Dec 19, 2004 at 14:34 UTC
    Just to note, this won't work on win32.

    (To make it work, you'd probably need something like Win32::Console or Term::ReadKey that can read in a nonblocking way from stdin, something like getline(0))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://415966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found