App-Env
view release on metacpan or search on metacpan
0.24 2010-06-07T17:14:37-0400
* the SysFatal option will now be passed on to cloned environments
created with the Temp option.
0.23 2010-06-07T16:44:26-0400
* capture() is no longer just an alias for qexec; it can now
return both stderr and stdout.
0.22 2010-06-03T12:44:43-0400
* if CacheID is the string 'AppID', the cache id does not take
into account the contents of the AppOpts hash.
* new feature from v. 0.20 2015-05-20T16:52:10-0400 AppOpts hash)
now actually works.
0.21 2010-06-02T10:28:42-0400
README.mkdn view on Meta::CPAN
split into lines.
If the **SysFatal** flag is set for this environment,
**IPC::System::Simple::capture** is called, which will cause this
method to throw an exception if the command returned a non-zero exit
value. It also avoid invoking a shell to run the command if possible.
- capture
$stdout = $env->capture( $command, @args );
($stdout, $stderr) = $env->capture( $command, @args );
Execute the passed command in the environment defined by **$env** and
returns content of its standard output and (optionally) standard error
streams.
If the **SysFatal** flag is set for this environment,
**IPC::System::Simple::capture** is called, which will cause this
method to throw an exception if the command returned a non-zero exit
value. It also avoid invoking a shell to run the command if possible.
lib/App/Env.pm view on Meta::CPAN
# requires an explicit block or sub{}. So, explicitly
# ignore prototypes.
my @return;
eval {
## no critic (AmpersandSigils,AmpersandSubCalls )
if ( $redirect ) {
my %redirect;
for my $stream ( 'stdout', 'stderr' ) {
next unless exists $redirect->{$stream};
my $handle = $redirect->{$stream};
$handle = IO::File->new( $handle, '>' )
if ref $handle eq q{};
$redirect{$stream} = $handle;
}
&Capture::Tiny::capture( $sub, %redirect );
$return[0] = $exit;
}
elsif ( $wantarray ) {
lib/App/Env.pm view on Meta::CPAN
=item *
Return the standard output stream
$stdout = $env->capture( $command, @args );
=item *
Return output, error and command exit value.
($stdout, $stderro, $exit) = $env->capture( $command, @args );
=item *
Return exit value and redirect standard output or error streams.
$exit = $env->capture( $command, @args,
{ stdout => $out, stderr => $err }
);
C<$out> and C<$err> may either by filehandles (e.g. L<IO::File>
objects) or file names.
=back
For even more control of the output streams, consider calling
L<Capture::Tiny/capture> directly, e.g.
t/00-compile.t view on Meta::CPAN
use File::Spec;
use IPC::Open3;
use IO::Handle;
open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!";
my @warnings;
for my $lib (@module_files)
{
# see L<perlfaq8/How can I capture STDERR from an external command?>
my $stderr = IO::Handle->new;
diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} }
$^X, @switches, '-e', "require q[$lib]"))
if $ENV{PERL_COMPILE_TEST_DEBUG};
my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-e', "require q[$lib]");
binmode $stderr, ':crlf' if $^O eq 'MSWin32';
my @_warnings = <$stderr>;
waitpid($pid, 0);
is($?, 0, "$lib loaded ok");
shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/
and not eval { +require blib; blib->VERSION('1.01') };
if (@_warnings)
{
warn @_warnings;
push @warnings, @_warnings;
t/00-compile.t view on Meta::CPAN
{ SKIP: {
open my $fh, '<', $file or warn("Unable to open $file: $!"), next;
my $line = <$fh>;
close $fh and skip("$file isn't perl", 1) unless $line =~ /^#!\s*(?:\S*perl\S*)((?:\s+-\w*)*)(?:\s*#.*)?$/;
@switches = (@switches, split(' ', $1)) if $1;
close $fh and skip("$file uses -T; not testable with PERL5LIB", 1)
if grep { $_ eq '-T' } @switches and $ENV{PERL5LIB};
my $stderr = IO::Handle->new;
diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} }
$^X, @switches, '-c', $file))
if $ENV{PERL_COMPILE_TEST_DEBUG};
my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-c', $file);
binmode $stderr, ':crlf' if $^O eq 'MSWin32';
my @_warnings = <$stderr>;
waitpid($pid, 0);
is($?, 0, "$file compiled ok");
shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/
and not eval { +require blib; blib->VERSION('1.01') };
# in older perls, -c output is simply the file portion of the path being tested
if (@_warnings = grep { !/\bsyntax OK$/ }
grep { chomp; $_ ne (File::Spec->splitpath($file))[2] } @_warnings)
{
t/capture.t view on Meta::CPAN
use Test2::V0;
use Test::Lib;
use File::Temp 'tempfile';
use File::Spec::Functions qw[ catfile ];
use App::Env;
my $script = catfile( qw [ t bin capture.pl ] );
subtest '($stdout, $stderr, $exit)' => sub {
my $app1 = App::Env->new( 'App1', { Cache => 0 } );
my ( $stdout, $stderr, $exit ) = $app1->capture( $^X, $script );
is( $exit, 0, 'exit' );
is( $stdout, "STDOUT\n", 'STDOUT' );
is( $stderr, "STDERR\n", 'STDERR' );
};
subtest 'redirect' => sub {
subtest 'filehandles' => sub {
my $app1 = App::Env->new( 'App1', { Cache => 0 } );
my ( $out_fh, $out_fname ) = tempfile();
my ( $err_fh, $err_fname ) = tempfile();
my $exit = $app1->capture(
$^X, $script,
{
stdout => $out_fh,
stderr => $err_fh,
},
);
is( $exit, 0, 'exit' );
$out_fh->flush;
$out_fh->seek( 0, 0 );
$err_fh->flush;
$err_fh->seek( 0, 0 );
is( $out_fh->getline, "STDOUT\n", 'STDOUT' );
is( $err_fh->getline, "STDERR\n", 'STDERR' );
t/capture.t view on Meta::CPAN
};
subtest 'file names' => sub {
my $app1 = App::Env->new( 'App1', { Cache => 0 } );
my ( $out_fh, $out_fname ) = tempfile;
my ( $err_fh, $err_fname ) = tempfile;
my $exit = $app1->capture(
$^X, $script,
{
stdout => $out_fname,
stderr => $err_fname,
},
);
is( $exit, 0, 'exit' );
is( $out_fh->getline, "STDOUT\n", 'STDOUT' );
is( $err_fh->getline, "STDERR\n", 'STDERR' );
};
};
( run in 0.391 second using v1.01-cache-2.11-cpan-beeb90c9504 )