From: John Francini <francini@...>
> The example is incorrect.
>
> The correct syntax should be
>
> $pid = fork() or die "Fork ERROR $!\n";)
>
> Using "or" instead of "||" is crucial.
>
> The || form of the or operator has higher precedence than the "or"
> version, and consequently causes both sides of the conditional to be
> evaluated -- that is, executed.
Nope. While sometimes the "or" versus "||" does matter it doesn't in
this case. The problem lies in something else. The fork() returns
three different things in three different cases.
- if it fails it returns an undef
- if it succeeds and you are in the parent process it returns the ID
of the created process
- if it succeeds and you are in the created process it returns 0
And the catch is that neither "or" nor "||" is able to distinguish
between the first and third case. Both 0 and undef are false.
So the code needs to do something like
my $pid = fork();
die "Fork ERROR $!\n" if !defined($pid);
if ($pid) {
print "I am the parent process.\n";
...
} else {
print "I am the new, created process.\n";
...
}
Jenda
===== Jenda@... === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery