如何让 Capture::Tiny 在失败时打印 stderr 和 stdout?
How to get Capture::Tiny to print stderr and stdout upon failure?
我正在尝试通过 Capture::Tiny
获取命令失败时的输出,
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Carp 'confess';
use Capture::Tiny 'capture';
sub execute {
my $cmd = shift;
my ($stdout, $stderr, $exit) = capture {
system( $cmd ); # the script dies here
};
if ($exit != 0) { # the script should die here
say "exit = $exit";
say "STDOUT = $stdout";
say "STDERR = $stderr";
confess "$cmd failed";
}
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0
}
execute("ls fakedir");
但问题是当我执行脚本时,
con@V:~/Scripts$ perl execute.pl
"ls fakedir" unexpectedly returned exit value 2 at /home/con/perl5/perlbrew/perls/perl-5.32.0/lib/site_perl/5.32.0/Capture/Tiny.pm line 382.
我得到的只是退出值,它没有提供 fakedir
不存在的有价值信息。也就是说,我只在脚本成功时得到 STDOUT 和 STDERR。
无论我使用 die
还是 confess
我都会遇到同样的问题 -> 脚本不打印输出 $stderr
和 $stdout
我已经按照 How do you capture stderr, stdout, and the exit code all at once, in Perl?, which does what I want, but the author https://metacpan.org/pod/IO::CaptureOutput 上的建议尝试了 IO::CaptureOutput
说“维护者不再推荐此模块 - 请参阅 Capture::Tiny。”这很奇怪,IO::CaptureOutput 似乎工作得更好!
如何让这个脚本在 $stdout
、$stderr
和 $exit
印有 confess 的情况下消失?
Capture::Tiny is working properly. system
is raising an error as if use autodie "system"
已开启。 Capture::Tiny 只捕获 STDOUT 和 STDERR,它不捕获错误。
您必须进行错误处理。你可以用一个 eval 块来捕获它。
my ($stdout, $stderr, $exit) = capture {
eval {
system( $cmd );
}
};
if (my $e = $@) { # the script should die here
# Can't get the system exit code from autodie.
#say "exit = $exit"
say "STDOUT = $stdout";
say "STDERR = $stderr";
confess "$cmd failed";
}
else {
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0;
}
在这种特定情况下,您已经在做 autodie 所做的事情。关闭捕获块内的 autodie 更简单。
my ($stdout, $stderr, $exit) = capture {
no autodie "system";
system( $cmd );
};
if ($exit) {
say "exit = $exit";
say "STDOUT = $stdout";
say "STDERR = $stderr";
# the script should die here
confess "$cmd failed";
}
else {
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0;
}
或者,既然你无论如何都会出错,你可以让 autodie 做它的事情。
my ($stdout, $stderr, $exit) = capture {
system( $cmd );
};
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
我正在尝试通过 Capture::Tiny
获取命令失败时的输出,
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Carp 'confess';
use Capture::Tiny 'capture';
sub execute {
my $cmd = shift;
my ($stdout, $stderr, $exit) = capture {
system( $cmd ); # the script dies here
};
if ($exit != 0) { # the script should die here
say "exit = $exit";
say "STDOUT = $stdout";
say "STDERR = $stderr";
confess "$cmd failed";
}
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0
}
execute("ls fakedir");
但问题是当我执行脚本时,
con@V:~/Scripts$ perl execute.pl
"ls fakedir" unexpectedly returned exit value 2 at /home/con/perl5/perlbrew/perls/perl-5.32.0/lib/site_perl/5.32.0/Capture/Tiny.pm line 382.
我得到的只是退出值,它没有提供 fakedir
不存在的有价值信息。也就是说,我只在脚本成功时得到 STDOUT 和 STDERR。
无论我使用 die
还是 confess
我都会遇到同样的问题 -> 脚本不打印输出 $stderr
和 $stdout
我已经按照 How do you capture stderr, stdout, and the exit code all at once, in Perl?, which does what I want, but the author https://metacpan.org/pod/IO::CaptureOutput 上的建议尝试了 IO::CaptureOutput
说“维护者不再推荐此模块 - 请参阅 Capture::Tiny。”这很奇怪,IO::CaptureOutput 似乎工作得更好!
如何让这个脚本在 $stdout
、$stderr
和 $exit
印有 confess 的情况下消失?
Capture::Tiny is working properly. system
is raising an error as if use autodie "system"
已开启。 Capture::Tiny 只捕获 STDOUT 和 STDERR,它不捕获错误。
您必须进行错误处理。你可以用一个 eval 块来捕获它。
my ($stdout, $stderr, $exit) = capture {
eval {
system( $cmd );
}
};
if (my $e = $@) { # the script should die here
# Can't get the system exit code from autodie.
#say "exit = $exit"
say "STDOUT = $stdout";
say "STDERR = $stderr";
confess "$cmd failed";
}
else {
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0;
}
在这种特定情况下,您已经在做 autodie 所做的事情。关闭捕获块内的 autodie 更简单。
my ($stdout, $stderr, $exit) = capture {
no autodie "system";
system( $cmd );
};
if ($exit) {
say "exit = $exit";
say "STDOUT = $stdout";
say "STDERR = $stderr";
# the script should die here
confess "$cmd failed";
}
else {
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";
return 0;
}
或者,既然你无论如何都会出错,你可以让 autodie 做它的事情。
my ($stdout, $stderr, $exit) = capture {
system( $cmd );
};
say "STDOUT = $stdout";
say "STDERR = $stderr";
say "exit code = $exit";