【问题标题】:Why does die execute even if the macro is executed successfully为什么即使宏执行成功die也会执行
【发布时间】:2014-03-28 04:13:29
【问题描述】:

我尝试使用 perl 执行宏,但遇到以下问题,如果代码是

$myObject->{DoCmd}->RunMacro("Delete") or die "Could not run macro\n";

程序会成功执行,包括宏和 die 说 Could not run macro 虽然执行成功,但 die 表现得很奇怪?有没有其他方法可以检查宏是否被执行?

【问题讨论】:

  • $myObject->{DoCmd}->RunMacro("Delete")返回了什么?如果它是0 成功,那么我认为程序会死是有道理的。 (0 or die)
  • @Matt 我尝试了$a=$myObject->{DoCmd}->RunMacro("Delete"); 并打印了$a,但我得到一个空行(我的意思是没有价值)
  • @Programmer Perl 会将空行视为错误。

标签: perl die


【解决方案1】:

这里的问题是 $myObject->{DoCmd}->RunMacro("Delete") 返回的内容在 Perl 中的计算结果为 false。

当这段代码运行时,

$myObject->{DoCmd}->RunMacro("Delete") or die "Could not run macro\n";

Perl 将首先运行or 左侧的代码。如果它评估为 false,那么它将评估右边的代码(这会使程序死掉)。

这是 perldoc 关于 Perl 中 falsehood 的引用:

数字 0、字符串 '0' 和 "" 、空列表 () 和 undef 在布尔上下文中都是错误的。所有其他值都为真。 对真实值的否定!或不返回一个特殊的假值。 当评估为字符串时,它被视为 "" ,但作为数字,它 被视为 0。大多数返回 true 或 false 的 Perl 运算符的行为 这边。

【讨论】:

  • 如果0表示“OK”,其他表示错误,可以将代码改写为:$myObject->{DoCmd}->RunMacro("Delete")==0 or die "Could not run macro\n";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
相关资源
最近更新 更多