http://www.perlmonks.org?node_id=478140


in reply to Re: select($rin,undef,undef,undef) only blocking once
in thread select($rin,undef,undef,undef) only blocking once

sysseek() does not work on a FIFO it seems.
would it be a solution to reopen the FIFO before the select statement ?
it works, but i dont think that is the proper way....
use strict; use POSIX qw(mkfifo); use Fcntl qw(O_RDONLY O_NONBLOCK SEEK_SET); use IO::Handle; my $fifofile = "fifo"; my $inputbuf; unlink($fifofile); mkfifo($fifofile, 0666) or die (' can\'t create FIFO:'.$fifofile); while (1){ sysopen(FIFOFD, $fifofile, O_RDONLY | O_NONBLOCK ) or die ("can\'t + read FIFO:'.$fifofile"); my $rin =''; vec($rin,fileno(FIFOFD),1)=1; select( $rin, undef, undef, undef ); print "select returning\n"; my $fromfifo; while ( sysread(FIFOFD, $fromfifo, 1) == 1 ){ $inputbuf .= $fromfifo ; } my $command; my $index; while ( $index = index($inputbuf,"\n" )+1 ){ my $command = substr($inputbuf,0,$index,""); if ( defined ($command) ) { print $command; } } }

Replies are listed 'Best First'.
Re^3: select($rin,undef,undef,undef) only blocking once
by Tanalis (Curate) on Jul 26, 2005 at 12:09 UTC
    It'd seem to be.

    I still favour a blocking read over blocking using select, though - you're using two system calls (which are relatively expensive) to do the job of one (unless there's something I'm missing).

    Paraphrasing the Cookbook a little, replacing your loop with

    for( ;; ) { open FIFO; "<", $fifofile or die $!; my $buf = <FIFO>; # blocks next unless defined $buf; chomp $buf; print $buf; close FIFO; }
    would seem to work as expected.

    Just as a final point, I'm not 100% sure what you're trying to achieve with your calls to index and substr - all you seem to be doing there is stripping the newline, which you can achieve using chomp.

    Hope that helps.

      the code i posted is just an example to demonstrate my problem with fifos. i am using select() because sometimes i want to return from waiting for input.

      select($rin,undef,undef,$timeout)
      i am not using $buf = <FH> because i thought your not supposed to use it when you use select()
        You shouldn't use select with buffered reads, no - in that case, I'd stick with your solution, especially if you want to use explicit read timeouts.

        My code was an example showing how you could avoid the use of select, if your circumstances permitted it - which it seems they don't :)