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

Hi there,
I've got a question about the system or backticks functions.
What I need is the output of the systemcall and also the returncode.
What can I do?

Use both :

$result=`myprog`; $return= system("myprog");
I think it don't make any sense to call it two times.
But is there an other way ?
Thanks.
----------------------------------- --the good, the bad and the physi-- -----------------------------------

Replies are listed 'Best First'.
Re: need both from system-call
by tomhukins (Curate) on Apr 27, 2001 at 15:18 UTC
      Thanks, I did a search before, but i haven't found that one
      Maybe I need some glasses :-)
      ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: need both from system-call
by physi (Friar) on Apr 27, 2001 at 15:27 UTC
    Upps I discovered $?. But there is a strange behavior in the following codes:
    xx.pl:
    #!/usr/bin/perl print"STATUS:$?\n"; $result=`./xxx.pl`; $status=$?; print "RES:$result\nSTA:$status\n";
    xxx.pl:
    #!/usr/bin/perl print"Wonderful world on Friday\n"; exit 200;
    This results in:
    STATUS:0 RES:Wonderful world on Friday STA:25600
    Why ???
    Is 255 the return code from the second #!/usr/bin/perl ?
    I'm a little bit confused ...
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      Look under "exit status" of the camel book. $? is the entire 16 bit status code. You only want the last 8 bits.

      This will fix your code: $status = $? >> 8; bb

      As perlvar documents, $? is "just the 16-bit status word returned by the wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ('$? >> 8')". It looks like perl thinks the exit value it got was 100.

      hdp.

Re: need both from system-call
by diarmuid (Beadle) on Apr 27, 2001 at 17:17 UTC
    Well, it's not very elegant but what I would do is use the system call and redirect the output to a file and then open that file
    my $return_code = system("system_command > /tmp/program.out"); open(FILE,"/tmp/program.out") || die "Cant open /tmp/program.out $!\n" +; my @output_file = <FILE>; close FILE; unlink("/tmp/program.out"); print STDOUT "return code ",$return_code>>8,"\n"; print STDOUT "@output_file";
      While others have posted links to other code that achieves physi's desired result, there is something which I think needs to be addressed in your code. While it certainly works and allows for a range of exit result eventualities, it does raise the age-old ire of using temporary files on systems. That is, the use of tempoary files with either predictable or known file names can allow for the trapping and redirection of output code.

      Issues pertaining to the use of temporary files have been covered in a number of threads both on Perl Monks and on news groups, including here and here. I would also recommend having a look at the POD documentation for the File::Temp module which discusses temporary file issues.

      I just wish I had saved the thread from comp.lang.perl.moderated that dealt with this issue in detail. At the very least I hope I have got you thinking ...

        I just wish I had saved the thread from comp.lang.perl.moderated that dealt with this issue in detail. At the very least I hope I have got you thinking ...

        This thread from comp.lang.perl.modules disusses temporary files in detail.