gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

Ok, so I've googled and searched here but have not found the answer, I know I must be missing something simple. I have an open line where I open a Unix named pipe for reading. The problem is the open of the pipe hangs until data is sent into the pipe. I'm not reading it at this time, just trying to open it, here is the sub that opens the pipe
sub open_named_pipe { my $INFILE = shift; open (IN_FH,"<$INFILE") || die "Cannot open input file $INFILE for r +eading!\n"; IN_FH->autoflush(1); return \*IN_FH; }
How do I just open this and then continue on?

Replies are listed 'Best First'.
Re: open ($FH,"<named pipe") blocking
by polettix (Vicar) on May 12, 2005 at 18:29 UTC
    The problem is the open of the pipe hangs until data is sent into the pipe.
    The open of the FIFO file blocks until another process opens the file for writing, not until data is available (which blocks read/sysreads).

    In alternative to previous suggestions, you can sysopen using the O_NONBLOCK flag provided in Fcntl. Update: I see only now that Fletch already gave you this suggestion, apologies for the repetition.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: open ($FH,"<named pipe") blocking
by ikegami (Patriarch) on May 12, 2005 at 18:28 UTC
    The problem is the open of the pipe hangs until data is sent into the pipe.

    I've played with names pipes a long time ago on AIX or SunOS/Solaris. If I remember correctly, that's how it behaved for me too, and I wasn't using Perl. Are you sure this isn't how your OS handles opening named pipes?

Re: open ($FH,"<named pipe") blocking
by dynamo (Chaplain) on May 12, 2005 at 18:18 UTC
    Change your open to open(IN_FH,"$INFILE|"). That tells perl to treat the filehandle as a pipe. UPDATE: Sorry, this is incorrect. see replies.

      I think you misunderstood what they're asking. They're trying to open a named pipe, which is a special pipe which lives in the filesystem. When you try and open it the open(2) system call underneath will block if there's no writer on the other end.

      They need to look at the O_NONBLOCK flag for sysopen rather than using open.

      Update: Excepting of course that that will just make the sysopen fail with a return code of EAGAIN (I believe). That's just the way named pipes work; the call blocks until there's something on the other end.

        You nailed it. I knew it was something simple, but it has been soooooo long since I had to use pipe for anything I forgot about sysopen. Thanks.

      No. That tells perlto execute the command stored in $INFILE and open IN_FH as a pipe to the command's output. That doesn't tell perl to open the named pipe whose name is in $INFILE.

      That actually tells perl to exec the thing in $INFILE and read from it as if it were a pipe. like if I were to do:
      $INFILE="/usr/bin/cat /var/adm/messages"; open($FH,"$INFILE|")
Re: open ($FH,"<named pipe") blocking
by Anonymous Monk on May 13, 2005 at 16:56 UTC
    And now the easy way:
    open(X,"+<$INFILE");open(IN_FH,"<$INFILE");close(X); # ^please note the +