It smells like a homework assignment to me, but here are some things you will need to consider. First, in order to have your program be both responsive to key entries, and at the same time be running other subroutines, you will need to use fork or threads. Second, if you want to enter data like AB<ENTER>, you will be looking for readline routines, not readkey routines. Readline waits for an <ENTER> before sending it to STDIN. Here are a few basic examples. There are many other event systems besides Glib, but I prefer it.
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
my $main_loop = Glib::MainLoop->new;
Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback, 'STDI
+N');
#just to show it's non blocking
my $timer1 = Glib::Timeout->add (10000, \&testcallback, undef, 1 );
$main_loop->run;
sub watch_callback {
# print "@_\n";
my ($fd, $condition, $fh) = @_;
my $line = readline STDIN;
print $line;
#always return TRUE to continue the callback
return 1;
}
sub testcallback{
print "\t\t\t".time."\n";
}
__END__
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
use threads;
$|++;
#ReadMode('cbreak');
# works non-blocking if read stdin is in a thread
my $thr = threads->new(\&read_in)->detach;
while(1){
print "test\n";
sleep 5;
}
#ReadMode('normal'); # restore normal tty settings
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__
|