Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

using defined on fork

by Anonymous Monk
on Dec 13, 2014 at 08:31 UTC ( [id://1110261]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1110261]
Approved by atcroft
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found