This Glib event loop is console based. Below is the same basic script with a while loop instead of a Glib loop.
Glib loops are nice because you can launch filewatchers and timers too.
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
use Glib qw/TRUE FALSE/;
use Term::ReadKey;
$|++;
ReadMode('cbreak');
my $main_loop = Glib::MainLoop->new;
Glib::Idle->add(
sub{
my $char;
if (defined ($char = ReadKey(0)) ) {
print "$char->", ord($char),"\n";
#process key presses here
}
return TRUE; #keep this going
});
$main_loop->run;
ReadMode('normal'); # restore normal tty settings
__END__
with a plain while loop
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
$|=1;
ReadMode('cbreak');
my $key = '';
while ( $key ne 'q' ) {
while (not defined ($key = ReadKey(-1))) {
# No key yet
}
print "Get key '$key'\n";
print ord($key);
print "\n";
}
__END__
while(1){
my $char;
if (defined ($char = ReadKey(0)) ) {
print "$char->", ord($char),"\n"; # input was waiting and i
+t was $char
} else {
# no input was waiting
}
}
ReadMode('normal'); # restore normal tty settings
|