in reply to Re: quoted execution not working
in thread quoted execution not working

my $pid=fork(); if ( $pid == 0 } { : child process goes here } elsif { $pid > 0 ) { : parent process wait(); # Wait for the child to exit } else { # WOAH! We should never get here! die "fork returned $pid"; }
There are two things wrong with that. If the fork fails, $pid is undef, but will still compare true to 0. Second, if it fails, the error is in $!, not $pid. So you actually want:
my $pid=fork(); if (not defined $pid) { die "fork failed: $!\n"; } elsif ( $pid == 0 } { : child process goes here } else { : parent process wait(); # Wait for the child to exit }

Dave.

Replies are listed 'Best First'.
Re^3: quoted execution not working
by blue_cowdawg (Monsignor) on Jul 28, 2004 at 00:10 UTC

        If the fork fails, $pid is undef, but will still compare true to 0. Second, if it fails, the error is in $!, not $pid.

    Good catch! You are correct. My bad. Instead of

    die "fork returned $pid";
    I should have invoked
    die $!;
    as I have in code that I have written in the past where I have done forking. Too much C-code lately the if-tree is a C-ism. As in
    pid = fork(); switch (pid){ -1: perror("Fork failed"); exit(-1); 0: /* Child */ break; default: /* parent */ break; }
    anyway, good catch.

    All all this code is off the top of my head.