- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 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
不存在的有值(value)信息。也就是说,当脚本成功时,我只会得到 STDOUT 和 STDERR。
无论我使用 die
还是 confess
我都会遇到同样的问题 -> 脚本不打印输出 $stderr
和 $标准输出
我已经按照 How do you capture stderr, stdout, and the exit code all at once, in Perl? 上的建议尝试了 IO::CaptureOutput
,这就是我想要的,但是作者 https://metacpan.org/pod/IO::CaptureOutput说“维护者不再推荐此模块 - 请参阅 Capture::Tiny。”这很奇怪,IO::CaptureOutput 似乎工作得更好!
我怎样才能让这个脚本在 $stdout
、$stderr
和 $exit
上打印 confess?
最佳答案
Capture::Tiny工作正常。 system
正在引发错误,就好像 use autodie "system"
在。 Capture::Tiny 仅捕获 STDOUT 和 STDERR,它不会捕获错误。
您必须进行错误处理。您可以使用 eval block 捕获它。
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 所做的事情。关闭捕获 block 内的 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";
关于perl - 如何让 Capture::Tiny 在失败时打印 stderr 和 stdout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66016077/
我喜欢 idea behind tiny-tiny rss :它是轻量级的、开源的、经常更新的,并且 seems to work very well .此外,客户端只需要一个浏览器。我想玩弄源代码并对
我在使用 TinySlider 时遇到问题,我想如果我可以使用重建()和销毁()方法,我也许能够修复问题/错误。 但是,你到底如何在像这样的组件上使用 .destroy() ? https://git
我正在尝试为 Tiny Tiny RSS 构建一个阅读器,但几乎陷入了第一步。我正在将应用程序设置为通过prototype.js (1.6.1) 使用ajax,并尝试简单登录应用程序以检索 key 。
我是 OpenCV 新手,我的 Windows XP 计算机上仅配置了 Tiny C 编译器。我可以使用这个微型 C 编译器来编译用于图像处理的 opencv 程序吗?我已经在我的 windows x
所以我有以下 Dockerfile: FROM ubuntu:latest WORKDIR /vault COPY run.sh /vault/run.sh COPY docker-entrypoin
我希望我的体重文件能够检测人,汽车和自定义对象。我知道YOLOv4-tiny的预训练重量文件是针对80个物体(包括人和汽车)进行训练的。 如果我想让体重文件检测到人,汽车和我的自定义对象,我想从一开始
我正在学习 Tiny OS 教程第 4 课“Mote-PC 串行通信和 SerialForwarder”,但我卡在了它所说的线路上“安装 TestSerial 后,您需要运行相应的 Java 应用程序
我原以为这段代码会出现段错误: char * foo (char my_ascii[10]) { strcpy (my_ascii, "0123456789"); return my_asci
为什么带有“$encoded =encode($encoding, $character, Encode::FB_WARN | Encode::LEAVE_SRC);”行的第二个示例没有捕获编码警告?
我已经下载了 Tiny MCE 的“全套”版本。我按照指示将它放在我的 php 表单中。但我只得到准系统的基本设置。我在插件文件夹中有所有插件???我按照说明使用以下代码:
我想根据自己的需要定制tiny mce。如果有人对此有任何想法,请帮助我。 问题是当您将微型 mce 添加到您的页面时,它位于 HTML dom 中的文本区域之后。 IE。 ......... 我想
我正在使用tiny table V3对表格进行排序,并在表格顶部添加一个搜索框。 当前排序是根据列名进行的,即第 1 列这是工作 fiddle ,http://jsfiddle.net/2vdh2ac
我有一个文本框和文本区域,我已经尝试在您集中注意力时实现在元素上,会显示 Tiny MCE 栏。 问题是它无法正常工作,在我自己的元素中,它甚至会自行更改文本框和文本区域的宽度和高度。 我在 jsFi
我有一个自定义 DateTime 类型,它定义了从字符串到 DateTime 的强制转换,如下所示: package Library; use Type::Library -base, -declar
假设我有以下 YAML 文件: --- task: 1: command: '' desc: '' 2: command: '' desc: '' 我使用 Pe
如何在 Tiny Colorpicker 中编写更改事件代码 请帮助我! 最佳答案 colorPicker github $(function() { $('#colorA').colorPick
我正在为一位音乐 Composer 建立一个网站。他想通过 Soundcloud 发布他的作品样本,并将其中一些样本嵌入到他的网站上。他有一个高级帐户。 我想让这对他来说尽可能简单(他不是极客)。基本
我正在为一位音乐 Composer 建立一个网站。他想通过 Soundcloud 发布他的作品样本,并将其中一些样本嵌入到他的网站上。他有一个高级帐户。 我想让这对他来说尽可能简单(他不是极客)。基本
我用 tinymce 4 替换了 tinymce 3。Tinymce 4 占据了所有可用的宽度: tinyMCE.init({ mode: 'textareas', theme: 'mo
我想创建一个基于字符串的类型,该类型将具有长度上限和 - 可选 - 长度下限。即,参数化类型,其中长度范围将是一个参数。 在我的实现中我想要什么: 字符串长度范围的单独类型。 不使用 MooseX::
我是一名优秀的程序员,十分优秀!