【问题标题】:Displaying error messages on the console when running an executable file inside a Perl script在 Perl 脚本中运行可执行文件时在控制台上显示错误消息
【发布时间】:2013-08-22 02:38:30
【问题描述】:

我制作了一个非常简单的 Perl 脚本,它使用“system”命令运行另一个可执行文件。 以下是它的粗略骨架。

#!/usr/bin/perl

# Doing some processing.....
# blah...blah...blah...

# $bin_file is an executable file
system("$bin_file $arguments");

我想从我的 shell 控制台查看结果。 $bin_file 生成的所有标准输出和标准错误都可以正确显示,但问题是如果 $bin_file 出现段错误等错误,则根本不显示。

您能否告诉我如何使这些操作系统错误消息也出现在控制台上?

【问题讨论】:

    标签: perl system


    【解决方案1】:

    嗯?段错误不会导致任何输出。不过,您可以自己检查错误:

    die "Can't launch child: $!\n"                 if $? == -1;
    die "Child killed by signal ".($? & 0x7F)."\n" if $? & 0x7F;
    die "Child exited with error ".($? >> 8)."\n"  if $? >> 8;
    

    【讨论】:

    • (可以使用 Config 模块的 $Config{sig_num}$Config{sig_name} 的组合将信号编号映射到信号名称。)
    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多