From: Sawyer X Date: Thu, 23 Feb 2012 12:56:23 +0000 (+0200) Subject: use Capture::Tiny to allow returning captured trace X-Git-Tag: v0.9.7~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e0cd89ab4b4053002483edd6d4b119cd2ffcb22c;hp=276a30c97c5932b6bdc4b0246102caec559d9ab8;p=p5sagit%2FApp-FatPacker.git use Capture::Tiny to allow returning captured trace --- diff --git a/lib/App/FatPacker.pm b/lib/App/FatPacker.pm index a6a01e3..ebb295a 100644 --- a/lib/App/FatPacker.pm +++ b/lib/App/FatPacker.pm @@ -4,6 +4,7 @@ use strict; use warnings FATAL => 'all'; use 5.008001; use Getopt::Long; +use Capture::Tiny (); use Cwd qw(cwd); use File::Find qw(find); use File::Spec::Functions qw( @@ -101,15 +102,36 @@ sub trace { my $use = defined $opts{'use'} ? $opts{'use'} : []; my $args = defined $opts{'args'} ? $opts{'args'} : []; my $output = $opts{'output'}; + my $capture; + + # if the user doesn't provide output, they want to actually + # capture the output and receive it back + if (!$output) { + # throw to STDOUT to differ from STDERR + $output .= '>&STDOUT'; + + # raise capture flag + $capture++; + } if(@$use) { $output .= "," . join ",", @$use; } - { - local $ENV{PERL5OPT} = '-MApp::FatPacker::Trace='.$output; - system $^X, @$args; - } + my $trace_sub = sub { + local $ENV{PERL5OPT} = '-MApp::FatPacker::Trace='.$output; + system $^X, @$args; + }; + + if ($capture) { + # capture both STDOUT and STDERR so we could throw away STDERR + # STDOUT will contain the trace + # STDERR will contain the "syntax OK" statement + my ($stdout, $stderr) = Capture::Tiny::capture {$trace_sub->()}; + return $stdout; + } else { + $trace_sub->(); + } } sub script_command_packlists_for {