Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring a perl die message

Tags:

perl

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.

like image 627
ianc1215 Avatar asked Apr 17 '11 04:04

ianc1215


2 Answers

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";
like image 145
Brian Roach Avatar answered Nov 14 '22 15:11

Brian Roach


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.)

like image 7
Sdaz MacSkibbons Avatar answered Nov 14 '22 15:11

Sdaz MacSkibbons