Following discussions in various branches of this thread, here's a more complete example.

It reports on the following:

As well as testing with different commands, I also (temporarily) added lines like the following in different places:

kill PIPE => $$; kill PIPE => $pid; kill TERM => $pid; die 'Test Death';

Here's the code:

#!/usr/bin/env perl -l use strict; use warnings; use Capture::Tiny qw{capture}; use Try::Tiny; my $retvals_for; { my ($stdout, $stderr, $exit); try { ($stdout, $stderr, $exit) = capture \&run_external_command; } catch { die "FATAL! $_" if defined $_; } finally { print_results($stdout, $stderr, $exit); }; die "ERROR! External command failed.\n" unless $exit; } sub run_external_command { my $external_command = 'cat'; push @{$retvals_for->{command}}, $external_command; { my $cmd_pipe; push @{$retvals_for->{signal}}, 'SIGPIPE not received.'; local $SIG{PIPE} = sub { pop @{$retvals_for->{signal}}; push @{$retvals_for->{signal}}, 'SIGPIPE received.', @_; if (exists $retvals_for->{open} and $retvals_for->{open}[0 +]) { kill TERM => $retvals_for->{open}[0] if kill 0 => $retvals_for->{open}[0]; my $close_ok = close $cmd_pipe; if ($close_ok) { push @{$retvals_for->{signal}}, "Closed pipe: $clo +se_ok"; } else { push @{$retvals_for->{signal}}, "Can't close pipe: + $!, $?"; } } die 'SIGPIPE received.'; }; my $pid = open $cmd_pipe, '|-', $external_command; if (defined $pid) { push @{$retvals_for->{open}}, $pid; } else { push @{$retvals_for->{open}}, "Can't open pipe: $!"; return 0; } my $print_ok = print $cmd_pipe "password\n"; if ($print_ok) { push @{$retvals_for->{print}}, $print_ok; } else { push @{$retvals_for->{print}}, "Can't print pipe: $!"; return 0; } my $close_ok = close $cmd_pipe; if ($close_ok) { push @{$retvals_for->{close}}, $close_ok; } else { push @{$retvals_for->{close}}, "Can't close pipe: $!, $?"; return 0; } } return 1; } sub print_results { my ($stdout, $stderr, $exit) = @_; print 'STDOUT:'; print defined $stdout ? $stdout : '<undef>'; print 'STDERR:'; print defined $stderr ? $stderr : '<undef>'; print 'EXIT:'; print defined $exit ? $exit : '<undef>'; print 'Return Values'; for my $source (qw{command open print close signal}) { print ucfirst($source), ':'; print for @{$retvals_for->{$source}}; } }

Sample output:

STDOUT: password STDERR: EXIT: 1 Return Values Command: cat Open: 5906 Print: 1 Close: 1 Signal: SIGPIPE not received.

— Ken


In reply to Re: How to pass data as STDIN to Capture::Tiny [more complete example] by kcott
in thread How to pass data as STDIN to Capture::Tiny by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.