monk2b has asked for the wisdom of the Perl Monks concerning the following question:

I like to test the error status of system() when I use that function. Recently I had reason to use the module Net::SSH:Expect in a program of mine. I have noticed that the value of $? after a system() call has changed to -1.

If I comment out the line "use Net:SSH:Expect" I get the results I am use to seeing a 0 for no error and 256 for error. If I uncomment "use Net::SSH:Expect" the value of $? is -1 on error or success. I am not sure what the real issue is here. Any info would be appreciated.

This is a simplistic code example of what is happening.

Bob
#!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my $command0 = "ls -l /tmp"; my $command1 = "ls -l /temperature"; RunCmd(); sub RunCmd{ system("$command0 > /dev/null"); print "System status info is $? \n"; system("$command1 > /dev/null 2> /dev/null"); print "System status info is $? \n"; }
I got 99 problems, but a @%$()_ ain't one.

Replies are listed 'Best First'.
Re: system() status weirdness
by pc88mxer (Vicar) on Mar 28, 2008 at 18:18 UTC
    That's because Net::SSH::Expect is installing the following SIGCHLD handler:
    sub reapChild { do {} while waitpid(-1,WNOHANG) > 0; }
    Doing this will give you your statuses back:
    use Net::SSH::Expect; ... $SIG{CHLD} = 'DEFAULT'; ... system(...);
    I could be wrong, but I doubt the reapChild handler is absolutely necessary, so you're probably safe just use default handler even when you make Net::SSH::Expect calls.
      This works great. Thanks alot ++.

      Bob

      I got 99 problems, but a @%$()_ ain't one.