【发布时间】:2011-08-07 04:30:15
【问题描述】:
我想在我的 perl 脚本中更改 die 消息的颜色。我目前正在使用Term::ANSIColor 在我的脚本中进行其他颜色更改。我遇到的死亡消息问题是,一旦脚本终止,它就无法将颜色重置为终端默认值,并且终端提示符是我的脚本中最后使用的任何颜色。在这种情况下,它会变成红色。
知道我怎样才能让脚本死掉但仍然改变颜色吗?
这里是有问题的代码块;
#!/usr/bin/perl
use strict;
use warnings;
require Term::ANSIColor;
use Term::ANSIColor;
print "Loading configuration file\n";
# Check if the specified configuration file exists, if not die
if (! -e $config_file_path) {
print color 'red';
die "$config_file_path not found!\n";
print color 'reset';
} else {
print color 'green';
print "$config_file_path loaded\n";
print color 'reset';
}
更新
它有效,但现在我无法摆脱 die 语句中说明它发生在哪一行的部分。
Loading configuration file
/etc/solignis/config.xml not found!
at discovery.pl line 50.
通常我只是添加一个换行的 die 函数,它消除了 die 的任何正常错误输出。知道为什么要这样做吗?
更新 2
根据您的所有建议,我拼凑起来。
print STDERR RED, "$config_file_path not found!";
die RESET, "\n";
它似乎工作正常。使用 Term::ANSIColor1 的常量是一件完美的事情,我需要简单的事情。
【问题讨论】:
标签: perl