[Israel.pm] STDIN

Gaal Yahas gaal at forum2.org
Sun May 9 04:58:05 PDT 2010


What do you want your program to be doing, if not to block?

stdin is just another file. You can put it in nonblocking mode, though
that probably means you have to give up using readline (<>) and read
on it (use sysread instead). I'm guessing something like (totally
untested, adapted from fcntl in perlfunc):

use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
my $flags = fcntl(\*STDIN, F_GETFL, 0)
                               or die "Can't get flags for stdin: $!\n";
$flags = fcntl(\*STDIN, F_SETFL, $flags | O_NONBLOCK)
                               or die "Can't set flags for stdin: $!\n";


Of course, now you also need to think up a scheme for what constitutes
a full applicative read: it can be some delimiter, message size, or
perhaps a timeout.

On Sun, May 9, 2010 at 2:11 PM, Levenglick Dov-RM07994
<RM07994 at freescale.com> wrote:
> Hi,
> Is there a way to pipe the keyboard input (STDIN) to a subroutine without forking or blocking the main program?
>
>
> Best Regards,
> Dov Levenglick
> ü SAVE PAPER - THINK BEFORE YOU PRINT
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/


More information about the Perl mailing list