
Perl's open() pukes when trying to open a read-type pipe
: I've narrowed down a pernicious bug in a local Perl script to the one
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: exhibited in the following snippet:
^^^^^^^^^^^^^^^^^
Wonderful, clever, effective!!! Oh that more posters to c.l.p.m were
so courteous (and self-helpful).
: #!/usr/um/bin/perl
: open(ABC, "/bin/date |") || die "$!";
Perhaps what's throwing you off is the fact that you may not get
a filehandle NOR the 'die' message.
from the perl FAQ:
5.16) Why doesn't open return an error when a pipe open fails?
These statements:
open(TOPIPE, "|bogus_command") || die ...
open(FROMPIPE, "bogus_command|") || die ...
will not fail just for lack of the bogus_command. They'll only
fail if the fork to run them fails, which is seldom the problem.
If you're writing to the TOPIPE, you'll get a SIGPIPE if the child
exits prematurely or doesn't run. If you are reading from the
FROMPIPE, you need to check the close() to see what happened.
If you want an answer sooner than pipe buffering might otherwise
afford you, you can do something like this:
$kid = open (PIPE, "bogus_command |"); # XXX: check defined($kid)
(kill 0, $kid) || die "bogus_command failed";
This works fine if bogus_command doesn't have shell metas in it, but
if it does, the shell may well not have exited before the kill 0. You
could always introduce a delay:
$kid = open (PIPE, "bogus_command </dev/null |");
sleep 1;
(kill 0, $kid) || die "bogus_command failed";
but this is sometimes undesirable, and in any event does not guarantee
correct behavior. But it seems slightly better than nothing.
Similar tricks can be played with writable pipes if you don't wish to
catch the SIGPIPE.
--
Tad McClellan, Logistics Specialist (IETMs and SGML guy)
Interesting trivia: If you took all the sand in North Africa and spread
it out... it would cover the Sahara desert.