<?xml version="1.0" encoding="windows-1252"?>
<node id="928062" title="Re: Ask for STDIN but don't pause for it" created="2011-09-27 06:04:57" updated="2011-09-27 06:04:57">
<type id="11">
note</type>
<author id="131741">
zentara</author>
<data>
<field name="doctext">
There is alot of code around for doing this, here are a few methods to save you the search. They all involve running a loop of some kind. In the Glib examples, you can substitute another event loop of your choosing.
&lt;p&gt;
&lt;readmore&gt;
Term-ReadKey in a thread
&lt;c&gt;
#!/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 $count = 0;
my $thr = threads-&gt;new(\&amp;read_in)-&gt;detach;

while(1){
print "test\n";
sleep 1;
}

ReadMode('normal'); # restore normal tty settings


sub read_in{
        while(1){
         my $char;
         if (defined ($char = ReadKey(0)) ) {
            print "\t\t$char-&gt;", ord($char),"\n";    
            #process key presses here
            if($char eq 'q'){exit}
            #if(length $char){exit}  # panic button on any key :-)
            }
          }
}

__END__ 
&lt;/c&gt;
&lt;p&gt;And an event loop based example using [cpan://Glib]
&lt;c&gt;
#!/usr/bin/perl 
use warnings;
use strict;
use Glib;

my $main_loop = Glib::MainLoop-&gt;new;

Glib::IO-&gt;add_watch (fileno 'STDIN', [qw/in/], \&amp;watch_callback, 'STDIN');	

#just to show it's non blocking
my $timer1  = Glib::Timeout-&gt;add (1000, \&amp;testcallback, undef, 1 );

$main_loop-&gt;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__

&lt;/c&gt;
&lt;p&gt;Another one combining an eventloop [cpan://Glib] with threads
&lt;c&gt;
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
use Term::ReadKey;
use threads;

$|++;
ReadMode('cbreak');

# works non-blocking if read stdin is in a thread
my $count = 0;
my $thr = threads-&gt;new(\&amp;read_in)-&gt;detach;

my $main_loop = Glib::MainLoop-&gt;new;

my $timer  = Glib::Timeout-&gt;add (1000, \&amp;timer_callback, undef, 1 );
# can also have filehandle watches
#my $watcher;
#$watcher = Glib::IO-&gt;add_watch( fileno( $pty ), ['in', 'hup'], \&amp;callback);

# must be done after main_loop is running
#Glib::Idle-&gt;add( sub{});
#print "$ps\n";

my $timer1  = Glib::Timeout-&gt;add (10, \&amp;testcallback, undef, 1 );

$main_loop-&gt;run;

ReadMode('normal'); # restore normal tty settings

sub testcallback{
    my $ps = `ps auxww`;
    print "$ps\n";
    return 0; #only run once
 }

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-&gt;", ord($char),"\n";    
            #process key presses here
            if($char eq 'q'){exit}
            #if(length $char){exit}  # panic button on any key :-)
        
            if($char eq 'p'){
             Glib::Idle-&gt;add( 
                   sub{
                     my $ps = `ps auxww`;
                     print "$ps\n";
                     return 0;  # run only once
                   } );
               }
                
            }
          }
}

__END__ 
&lt;/c&gt;
&lt;p&gt;And finally an IO::Select based solution to avoid threads
&lt;c&gt;
#!/usr/bin/perl
use warnings;
use strict;
use IO::Select;

# remember what select says about mixing 
# buffered reading and "select", so even though the 
# code works, you might want to substitute 
# the read via &lt;$fh&gt; with:
#    my $input;
#    sysread( $fh, $input, 1024);


# loop every 5 second
my $timeout = 5;

my $s = IO::Select-&gt;new();
$s-&gt;add( \*STDIN );

while (1) {
    if ( my @ready = $s-&gt;can_read($timeout) ) {

        # we got input

        for my $fh (@ready) {
            print "$fh\n";
            my $input = &lt;$fh&gt;;
            print "got: $input";
        }
    }
    else {

        # no input
    }

    # just to show that we're looping
    print scalar localtime,"\n";
}

&lt;/c&gt;
&lt;/readmore&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-131741"&gt;
&lt;hr /&gt;
I'm not really a human, but I play one on earth.&lt;br&gt;
[id://630805] ................... &lt;a href=http://zentara.net/japh.html&gt; flash japh &lt;/a&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
927943</field>
<field name="parent_node">
927943</field>
</data>
</node>
