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

I don't understand this following code:

defined(my $pid=fork) or die $!; unless($pid){ exec 'date'; die $!; } waitpid($pid,0);

when using fork,the fork function returns pid, which is non-zero number and true value. I don't know why I should use defined function in here. But without defined, this code just immediately die

Replies are listed 'Best First'.
Re: using defined on fork
by Corion (Patriarch) on Dec 13, 2014 at 08:48 UTC

    The return values of fork are listed in the documentation for fork. Fork can return three values - 0 for the parent, nonzero for the child and undef if the fork fails.

    So, the defined check is there to check if the fork was successful.