in reply to Re: "Answer" questions in an interactive program?
in thread "Answer" questions in an interactive program?

... close CMD; waitpid $pid, 0; print "$progname ended: status: ", $? >>8;

I'm a little surprised that you get the correct status value here, because close already does wait for the subprocess at the other end of the pipe (so the waitpid is neither needed nor setting the $? correctly). At least on Unix.  Does this behave differently on Windows?

#!/usr/bin/perl -l my $cmd = q(perl -e 'chomp(my $ret = <STDIN>); exit $ret+1'); my $pid = open CMD, '|-', $cmd or die $!; print CMD 98; close CMD; print "status from close: ", $? >> 8; waitpid $pid, 0; print "status from waitpid: ", $? >> 8; __END__ $ ./839409.pl status from close: 99 status from waitpid: 72057594037927935

Replies are listed 'Best First'.
Re^3: "Answer" questions in an interactive program?
by BrowserUk (Patriarch) on May 11, 2010 at 11:59 UTC
    Does this behave differently on Windows?

    Seems so:

    c:\test>perl #!/usr/bin/perl -l my $cmd = q(perl -e "chomp(my $ret = <STDIN>); exit $ret+1"); my $pid = open CMD, '|-', $cmd or die $!; print CMD 98; close CMD; print "status from close: ", $? >> 8; waitpid $pid, 0; print "status from waitpid: ", $? >> 8; ^Z status from close: 99 status from waitpid: 99

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It worked! Thanks a lot to all of you!