what happens if open fails?
I came across the following code:
my $pid = open (my $han开发者_JAVA技巧dler, "-|");
unless ($pid)
{
#child stuff
exec $command;
}
else
{
#parent stuff
}
what would happen if the implicit fork fails? will the command be executed anyway?
Yes the command will be executed, unless the code uses something like autodie.
open($fh, '-|')
returns undef
on failure, which is false, so the (unforked) process would exec the command (and thus terminate itself).
(On an unrelated note, I, and many other programmers, feel that unless ... else ...
is bad style, but it is valid Perl.)
精彩评论