perl - system() 返回错误的退出代码

标签 perl

我正在使用 system 调用外部应用程序,我需要解释它的退出代码。

我知道 system 从命令返回退出代码乘以 256,但是在 foo.bat 中我写 exit 256结果为零。

为什么会这样?

最佳答案

Windows 使用 32 位退出代码,因此 exit 256 完全有效。

>cmd /c exit 256

>echo %ERRORLEVEL%
256

但是,Perl 只保留最低有效的 8 位。

>perl -e"system 'exit 256'; CORE::say $?>>8"
0

>perl -e"system 'exit 266'; CORE::say $?>>8"
10

这是没有充分理由的 Perl 缺陷。如果你使用 Win32::Process而不是 system,您可以获得正确的退出代码。

>perl -MWin32::Process=NORMAL_PRIORITY_CLASS,INFINITE -e"Win32::Process::Create(my $proc, $ENV{COMSPEC}, 'cmd /c exit 256', 0, NORMAL_PRIORITY_CLASS, '.') or die $^E; $proc->Wait(INFINITE); $proc->GetExitCode(my $exit_code); CORE::say $exit_code;"
256

关于perl - system() 返回错误的退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604983/

相关文章:

perl - POE::Kernel->run() 大约需要 15 秒才能停止/

regex - Perl:找不到文件时抑制反引号的输出

perl - 使用 split 提取月、日、年

perl - 从 Perl 中读取变量

perl - 是否有一个 perl 模块来验证存储在 "{crypt}hashedpassword" "{ssha}hashedpassword" "{md5}hashedpassword"中的密码

perl - 如何使用 Perl 轻松批量重命名文件?

perl - 如何在 Perl 中比较 2 个文件中的数据?

python - 计算表中存在或不存在某个因素的组合

perl - 被 perl 中一个非常简单的 if/else 语句弄糊涂了

perl - 在没有插件的模板工具包中调用外部子和模块?