3
0
mirror of https://github.com/pragma-/pbot.git synced 2026-03-09 16:47:58 +01:00
pbot/PBot/StdinReader.pm
Pragmatic Software f725743ccb == MAJOR NEW BETA RELEASE ==
Converted single large "amalgamate" monolithic pbot2.pl script into multiple Perl packages/modules.

Tons of refactoring and clean-ups.

Consider this version to be beta.  Use at your own risk.
2010-03-17 06:36:54 +00:00

34 lines
757 B
Perl

package PBot::StdinReader;
use warnings;
use strict;
use vars qw($VERSION);
$VERSION = '1.0.0';
use IO::Select;
use POSIX qw(tcgetpgrp getpgrp); # to check whether process is in background or foreground
# used to listen for STDIN in non-blocking mode
my $stdin = IO::Select->new();
$stdin->add(\*STDIN);
# used to check whether process is in background or foreground, for stdin reading
open TTY, "</dev/tty" or die $!;
my $tty_fd = fileno(TTY);
my $foreground = (tcgetpgrp($tty_fd) == getpgrp()) ? 1 : 0;
sub check_stdin {
# make sure we're in the foreground first
$foreground = (tcgetpgrp($tty_fd) == getpgrp()) ? 1 : 0;
return if not $foreground;
if ($stdin->can_read(.5)) {
chomp(my $input = <STDIN>);
return $input;
}
}
1;