#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; my $input_q = Thread::Queue -> new(); sub stdin_reader { while ( my $line = ) { if ( defined $input_q -> pending() ) { $input_q -> enqueue ( $line ); } else { last; } } } sub doing_stuff_thread { my $count = 0; while ( $count < 10 ) { my $item = $input_q -> dequeue_nb(); if ( defined $item ) { print "Got: $item"; $count++; } else { print "Doing something else, because nothing on STDIN\n"; print "Pending items:", $input_q -> pending(), "\n"; sleep 1; } } $input_q -> end(); } threads -> create ( \&stdin_reader ); threads -> create ( \&doing_stuff_thread ); foreach my $thr ( threads -> list() ) { print "Waiting for ",$thr -> tid()," to join\n"; $thr -> join(); } #### #!/usr/bin/perl use strict; use warnings; use IO::Handle; use IO::Select; my $nb_stdin = new IO::Select( *STDIN ); while ( 1 ) { if ( $nb_stdin -> can_read(0) ) { my $line = ; print "Got: ", $line,"\n"; } else { print "Nothing on STDIN, doing something else\n"; sleep 1; } }