http://www.perlmonks.org?node_id=86551


in reply to once again: program output and return code

eval tells me:
In both forms, the value returned is the value of the last expression evaluated inside the mini- program; a return statement may be also used, just as with subroutines. The expression providing the return value is evaluated in void, scalar, or list context, depending on the context of the eval itself. See the wantarray entry elsewhere in this document for more on how the evaluation context can be determined.
So you can get the return with $ret = eval ....

If you want to execute a shell, use system with redirection:

$ret = system('perl','return.pl',">$tmp_stdout","2&>$tmp_stderr");
Jeroen
"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: once again: program output and return code
by chipmunk (Parson) on Jun 07, 2001 at 19:55 UTC
    Adding an extra level of indirection with eval does noth solve the problem. eval returns the value of the last expression evaluated, but that last expression would return the same value without the help of eval:
    $x = 1; $y = 2; $return1 = $x + $y; $return2 = eval { $x = 1; $y = 2; $x + $y }
    If particle already knew what that last expression should be, to get the value he wants, using eval wouldn't gain him anything.

    I know how to get STDIN and STDOUT, or STDIN and the exit value, but I can't figure out how to get STDOUT, STDERR, and the exit value. :(

      > I know how to get STDIN and STDOUT, or STDIN and the exit value, but I can't figure out how to get STDOUT, STDERR, and the exit value. :(

      i'm just glad i'm not crazy. but maybe i can narrow the scope a little. i know i can get return code from system. but can i redirect the output of the system call in a portable way?

      =Particle

Re: Re: once again: program output and return code
by particle (Vicar) on Jun 07, 2001 at 19:25 UTC
    $@ is a shortcut to the return code from eval. and that i'm getting. it's the redirection that i can't figure out.

    i assume you meant double-quotes around $tmp_stdout and $tmp_stderr. i ran your corrected code on Win32, and i can't get this to work there.

    =Particle

      No, perlvar sayz:
      $@ The Perl syntax error message from the last eval() operator. If null, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion). (Mnemonic: Where was the syntax error "at"?) Warning messages are not collected in this variable. You can, however, set up a routine to process warnings by setting `$SIG{__WARN__}' as described below. Also see the Error Indicators entry elsewhere in this document.
      So that's something different (it returns the syntax error). use $ret=eval... for return values.

      And indeed, that redirection won't work under windows. But maybe a real shell helps in that respect: try cygwin or bash4win.

      Oh, and fixed those quotes. Sorry about that.

      Jeroen