in reply to $? stuck returning -1...

The back ticks don't set $? (see perlop). That is the correct behavior. If you want the return value, look at system() instead. It turns out that I'm wrong about that. According to perlvar it is indeed supposed to set $?, but it's not the return value, it's more.

$exit = $? >> 8 and $sig = $? & 128...

-Paul

Replies are listed 'Best First'.
Re^2: $? stuck returning -1...
by cmv (Chaplain) on Jun 20, 2007 at 16:05 UTC
    Paul-

    I'm confused. If it is supposed to set $?, then doesn't the sample program show that it's not working? My output looks like this:

    exit1=0 ret1=Wed Jun 20 15:09:04 CDT 2007 24862: parent pid=24864 24864: child exit2=-1 ret2=Wed Jun 20 15:09:04 CDT 2007 exit2=-1 ret2=Wed Jun 20 15:09:04 CDT 2007 exit2=-1 ret2=Wed Jun 20 15:09:04 CDT 2007 exit2=-1 ret2=Wed Jun 20 15:09:04 CDT 2007 exit2=-1 ret2=Wed Jun 20 15:09:04 CDT 2007
    Thanks -Craig
      You're setting $SIG{CHLD} yourself so the wait() implicit in the backticks command will report there are no children.

      Set $SIG{CHLD} to undef before the foreach loop to see that that fixes the problem.

      update:

      $|=1; my $ret1 = `date`; my $exit1 = $?; print STDERR "exit1=$exit1 ret1=$ret1"; $SIG{CHLD} = sub {print "Sigchld\n";wait}; foreach my $i (1..5) { my $ret2 = `date`; my $exit2 = $?; print STDERR "exit2=$exit2 ret2=$ret2"; }
      output:
      exit1=0 ret1=Wed Jun 20 22:29:13 CEST 2007 Sigchld exit2=-1 ret2=Wed Jun 20 22:29:13 CEST 2007 Sigchld exit2=-1 ret2=Wed Jun 20 22:29:13 CEST 2007 Sigchld exit2=-1 ret2=Wed Jun 20 22:29:13 CEST 2007 Sigchld exit2=-1 ret2=Wed Jun 20 22:29:13 CEST 2007 Sigchld exit2=-1 ret2=Wed Jun 20 22:29:13 CEST 2007