"This works, but you can't trust the exit value returned as the 3rd output value of capture and must instead inspect $?."
Well, the documentation for
Capture::Tiny v0.44
(the latest version, at the time of writing, and the version I used) states:
"The capture function takes a code reference and returns what is sent to STDOUT and STDERR as well as any return values from the code reference." [my emphasis]
I took this to be correct but maybe you have other information.
Please share.
I did a quick test.
The third value, when captured, did contain the return value of the code reference.
Could you provide some code showing this third value with something other than the return value of the code reference.
| [reply] [Watch: Dir/Any] [d/l] |
Hi,
but you can't trust the exit value returned as the 3rd output value of capture and must instead inspect $?
I'm guessing what you're comparing kcott's code to is this example from the Capture::Tiny doc:
($stdout, $stderr, $exit) = capture {
system( $cmd, @args );
};
In that code, the reason that you get $? stored into $exit is that system returns the same thing as $?, and capture returns the return value(s) from the block of code. Since in kcott's code the last statement in the block is print, its return value is what capture returns as the 3rd return value. If you wanted to use the above pattern of getting $? as the 3rd return value, just change kcott's code like this: sub run_external_command { ...; $? }.
Hope this helps, -- Hauke D
| [reply] [Watch: Dir/Any] [d/l] [select] |
FYI, Capture::Tiny::Extended resolves the collection of the return code.
From the doc:
Capture::Tiny::Extended automatically captures return values and returns them after the second return value (or first if you're using the merged functions).
use Capture::Tiny::Extended 'capture';
my ( $out, $err, $res ) = capture { system( 'ls' ) };
...it is unhealthy to remain near things that are in the process of blowing up. man page for WARP, by Larry Wall
| [reply] [Watch: Dir/Any] [d/l] |
Capture::Tiny can already do this; this feature was added in 2011 in a patch from Christian Walde, the author of Capture::Tiny::Extended (also released in 2011).
| [reply] [Watch: Dir/Any] |