Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986955]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-06-24 19:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.