Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: How to pass data as STDIN to Capture::Tiny

by Anonymous Monk
on Jan 02, 2017 at 06:51 UTC ( [id://1178800]=note: print w/replies, xml ) Need Help??


in reply to Re: How to pass data as STDIN to Capture::Tiny
in thread How to pass data as STDIN to Capture::Tiny

This works, but you can't trust the exit value returned as the 3rd output value of capture and must instead inspect $?. Also, you must explicitly call close on the pipe.

Replies are listed 'Best First'.
Re^3: How to pass data as STDIN to Capture::Tiny
by kcott (Archbishop) on Jan 02, 2017 at 08:44 UTC
    "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.

    — Ken

Re^3: How to pass data as STDIN to Capture::Tiny
by haukex (Archbishop) on Jan 02, 2017 at 18:16 UTC

    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

Re^3: How to pass data as STDIN to Capture::Tiny
by NetWallah (Canon) on Jan 03, 2017 at 00:36 UTC
    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

      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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1178800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found