Chapter_01 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a piece of code that will look at a named pipe and move on if there is nothing to read from the pipe. I am using "select" and "vec" examples straight from the documentation (which honestly is leaving me a little confused). Obviously I am doing something wrong here and any insight is greatly appreciated. Here is my code :
#!/usr/bin/perl -w use strict; my $FiFo='/opt/WFmon/logs/cpu.comm'; my $read=''; my $timeout=1; my ($nfound); open(FIFO, "<$FiFo") || warn "cannot open fifo\n"; vec($read, fileno(FIFO), 1) = 1; $nfound = select($read, undef, undef, $timeout); if($nfound){ if(vec($read, fileno(FIFO), 1)){ print "here - $nfound\n"; } else{ print "not here - $nfound\n"; } } else{ print "nothing\n"; } close(FIFO);
This works in the sence that when I copy/print/echo a string to the pipe (/opt/WFmon/logs/cpu.comm), the script proceeds to the first "if" statement. Otherwise, the script just hangs there waiting for the pipe to complete and never times out.

Thank you!
Bryan

Replies are listed 'Best First'.
Re: timeout on dead pipe
by Thelonius (Priest) on Feb 27, 2003 at 02:54 UTC
    The process is blocking on the open() call. You can try using sysopen with the O_NONBLOCK flag.
      You sir, are a gentleman and a scholar! Using "sysopen(FIFO, $FiFo, O_NONBLOCK|O_RDONLY);" coupled with "use Fcntl qw(:DEFAULT :flock);" did exactly what I needed Thank you!