select, unbuffered pipes 
Author Message
 select, unbuffered pipes

Hello,

My 20-line perl program below tries to do non-blocking reads from a
pipe.  Unfortunately, my select() call seems to always return 1, even
when there is nothing more to read.  I'd like to handle a >1
filehandles eventually, but I can't even make one work so far.

Anyone have any tips?

I've tried reading:
perldoc -f select
man 2 select
http://www.*-*-*.com/ ~pvhp/ptk/doc/fileevent.htm
http://www.*-*-*.com/

Here's the program:

use strict ;
select STDOUT ; $| = 1 ;

open(FH1, "ls |") or die $! ;

my $rin = '' ;
vec($rin, fileno(FH1), 1) = 1 ;

while (1) {
  my $rout ;
  my $nfound = select($rout=$rin, undef, undef, 0) ;
  if ($nfound) {
    if (vec($rout, fileno(FH1), 1)) {
      my $s ;
      sysread(FH1, $s, 1) ;
      print $s ;
    }
  }

Quote:
}

__END__

Thanks

A

--
it's raf not rfa



Wed, 11 Aug 2004 22:52:44 GMT  
 select, unbuffered pipes

Quote:

> Hello,

> My 20-line perl program below tries to do non-blocking reads from a
> pipe.  Unfortunately, my select() call seems to always return 1, even
> when there is nothing more to read.

Select considers a socket "readable" if a handle has data waiting, or is
at EOF.  It considers a handle "not readable" if the handle has no data
waiting and is not yet at EOF.

[snip]

Quote:
> open(FH1, "ls |") or die $! ;

> my $rin = '' ;
> vec($rin, fileno(FH1), 1) = 1 ;

> while (1) {
>   my $rout ;
>   my $nfound = select($rout=$rin, undef, undef, 0) ;
>   if ($nfound) {
>     if (vec($rout, fileno(FH1), 1)) {
>       my $s ;
>       sysread(FH1, $s, 1) ;
>       print $s ;

Change to:
     my ($s, $n);
     defined( $n = sysread( FH1, $s, 1 ) ) or die "sysread: $!";
     if( $n == 0 ) { print "EOF!\n"; last }
     print $s;

Quote:
>     }
>   }
> }
> __END__

Oh, and the IO::Select module provides a much simpler interface to
select.  Using it, the code would be:

use strict;
use warnings;
use IO::Select;
use IO::File;
my $sel = IO::Select->new;
$sel->add(IO::File->new("ls|"));
my $timeout = 5;
while( 1 ) {


      my ($s, $n);
      defined( $n = sysread( $fh, $s, 1 ) ) or die "sysread: $!";
      if( $n == 0 ) { print "EOF!\n"; last }
      print $s;
   }

Quote:
}

[untested]

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Thu, 12 Aug 2004 02:51:12 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. IO::Socket, IO::Select, and buffered/unbuffered IO

2. Non-blocking unbuffered reads from pipes

3. Help reading pipes "unbuffered"?

4. Using 4-arg select with a pipe

5. Help: pipe() & select()

6. Calling select on a Win32::Pipe...

7. select and pipes ?

8. select ip-adress out of pipe delimited file

9. select() reading from mult. tail pipes

10. PLEASE AT LEAST ACK : re: select() on named pipe

11. Win32::Pipe and select

12. select doesn't detect pipe close

 

 
Powered by phpBB® Forum Software