in reply to quoted execution not working

Well... here's a few thoughts: first change how you are doing your forks:

: : handwaving : 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"; }
Within your child process instead of using backticks you couold do the following
: child open(PIPE,"some_command 2>&1 |") or die $!; my @output=<PIPD>; close PIPE; print @output;
This way if you fail to fork off the grandchild you will see a diagnostic from die.

Replies are listed 'Best First'.
Re^2: quoted execution not working
by dave_the_m (Monsignor) on Jul 27, 2004 at 23:44 UTC
    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.

          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.