Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Using File::Tail::select with STDIN?

by zentara (Archbishop)
on Aug 12, 2012 at 11:57 UTC ( #986955=note: print w/ replies, xml ) Need Help??


in reply to Using File::Tail::select with STDIN?

You can also put your STDIN read into a thread. Here is an example where each key is detected, but you can write it more simply if you are willing to let STDIN to be line buffered, where the line comes in only after you hit a Return.

#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; use threads; $|++; # works non-blocking if read stdin is in a thread my $thr = threads->new(\&read_in)->detach; while(1){ print "test\n"; sleep 1; } sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; #process key presses here if($char eq 'q'){exit} #if(length $char){exit} # panic button on any key :-) } } } __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh


Comment on Re: Using File::Tail::select with STDIN?
Download Code
Re^2: Using File::Tail::select with STDIN?
by phillipdhall (Initiate) on Aug 25, 2012 at 11:07 UTC

    For anyone else searching, what I ended up doing was a little more simple. Looking into forking and threading was intimidating, since I'd need variables to be passed between the two and didn't understand how that worked.

    Since both standard select() and File::Tail::select have timeouts, I loaded STDIN and the File::Tail object in the appropriate one, and read from each inside a loop. Kinda silly how simple that seems to me now.

    I was initially concerned about performance when doing so, but then I guess all that each select method is doing is reading in a timed loop anyway. My method seems to be working great. Thank you to those who provided input.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-27 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls