开发者

Coloring a perl die message

开发者 https://www.devze.com 2023-02-25 03:44 出处:网络
I want to change the color of a die message in my perl script. I am currently using Term::ANSIColor to do other color changing in my script. The problem I run into with die messages is once the script

I want to change the color of a die message in my perl script. I am currently using Term::ANSIColor to do other color changing in my script. The problem I run into with die messages is once the script dies it cannot reset the color to the terminal default and the terminal prompt is whatever color was last used in my script. In this case it turns it red.

Any idea how I can have the script die but still change the color back?

Here is the code block in question;

#!/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';
}

Update

It works but now I am not able to get rid of the part of the die statment that says what line it happened on.

Loading configuration file
/etc/solignis/config.xml not found!
 at discovery.pl line 50.

Normally I just add a line break the die function and that eliminates any of the normal error output from die. Any idea why its doing that?

Update 2

Based on all of your recommendations, I cobbled this together.

print STDERR RED, "$config_file_path not found!";
die RESET, "\n";

It seems to work correctly. Using the constants for Term::ANSIColor1 was a perfect thing I needed to simply things.


die is printing to STDERR while print is going to STDOUT.

print STDERR color 'red';
die "$config_file_path not found!\n";

Note that ... you just died. Your 'reset' isn't going to be printed

You want to concatenate it onto your die:

die color 'red' . "$config_file_path not found!" . color 'reset';

You can also use the constants:

use Term::ANSIColor qw(:constants);

die RED, "THis is death!", RESET, "\n";

EDIT: Sorry - And to get rid of the "where it happened" part, concatenate a \n to the end:

die color 'red' . "$config_file_path not found!" . color 'reset' . "\n";


There's a few ways to do this.

Use Term::ANSIColor's colored function, which seems to automatically add the ANSI reset sequence at the end:

die colored( "$config_file_path not found!\n", 'red' );

Use the constants interface from Term::ANSIColor:

use Term::ANSIColor qw( :constants );
$Term::ANSIColor::AUTORESET = 1;
die RED "$config_file_path not found!\n";

or

die RED, "$config_file_path not found!\n", RESET;

You can also trap $SIG{__DIE__}, or use an END block, with a coderef and reset it after printing its arguments. (These are probably not the greatest ideas, but they would allow you to reset the colors under almost any exit circumstance.)


I’m sure an END{} block in which you reset the terminal back to its blackness is the cleanest solution. That’s what such things are for.

Do NOT screw around with $SIG{__DIE__}, or you will be unhappy. Nobody realizes it gets called even for trapped exceptions!!


You can use the __DIE__ handler:

$SIG{'__DIE__'} = sub {
    print color 'reset';
};

From perldoc perlvar:

Due to an implementation glitch, the $SIG{__DIE__} hook is called even inside an eval().

Hence, you should probably not use this technique.

0

精彩评论

暂无评论...
验证码 换一张
取 消