use Glib;
use Term::ReadKey;
use threads;
#use threads::shared;
$|++;
ReadMode('cbreak');
# works non-blocking if read stdin is in a thread
my $count = 0;
my $thr = threads->new(\&read_in)->detach;
my $main_loop = Glib::MainLoop->new;
my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 );
# can also have filehandle watches
#my $watcher;
#$watcher = Glib::IO->add_watch( fileno( $pty ), ['in', 'hup'], \&call
+back);
$main_loop->run;
ReadMode('normal'); # restore normal tty settings
sub timer_callback{
#do stuff
$count++;
print "\n$count\n";
return 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 :-)
}
}
}
- use threads;
- and use Term::ReadKey;
- and use Glib;
- and my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 );
- and (despite your "...without a cumbersome while(1) loop."), while(1){
And what does all of that get you? Not a lot!
You still have to write your own line editing, command line history, command line aliases etc. etc.
And if you need to do something that'l take a few seconds, your going to have to break it up into iddy biddy chunks, or lace it through with some do_one_event() call or similar.
when the alternative is:
my $Qstdin = new Thread::Queue;
async {
$Qstdin->enqueue( $_ ) while defined( $_ = <STDIN> );
}->detach;
...
my $kbinput = $Qstdin->dequeue;
Fully cross platform; one thread and no faffing around with iddy biddy callbacks; and all the command line handling your local shell provides--that works in exactly whatever way the local shell provides it.
AND to do other things in the main thread
Rubbish! As soon as you enter that MainLoop-run, your main "thread" is dead until something signals stop.
And you might want to revise your "It also works cross-platform,". From the GLib docs:
The GIOChannel data type aims to provide ... support for Windows is only partially complete.
which can't be said for the Win32 modules.
Oh! And what Win32 modules would that be?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|