And the implementation:use warnings::colored; warn "warning"; # yellow system "non-existant-command"; # red say "test"; # none eval { die "caught" }; # none say $@; # none die "died"; # red
package warnings::colored; use strict; use warnings; use Term::ANSIColor; # Color die and warn messages. # NOTE: Doesn' play well with Carp::Always because it replaces our die + and # warn sighandlers. $ENV{NO_COLOR} = 1 if ! exists $ENV{NO_COLOR} and (! -t STDIN or ! -t STDERR); BEGIN { my $died; *CORE::GLOBAL::die = sub { $died = 1, print STDERR color('bold red') if @_ and ! $^S; END { $died && print STDERR color('reset') } $] >= 5.016 && goto &CORE::die; CORE::die @_; }; *CORE::GLOBAL::warn = sub { unshift @_, color('bold yellow'); # $SIG{__WARN__} will print the reset sequence. $] >= 5.016 && goto &CORE::warn; CORE::warn @_; }; } # Unintentional warning messages should display with high severity. # E.g.`system 'nonexistent-command'`. $SIG{__WARN__} = sub { my $msg = shift; if ($] < 5.016 and __FILE__ eq (caller)[1]) { my ($file, $line) = (caller(1))[1, 2]; $msg =~ s/( at .*? line \d+\.$)/ at $file line $line/m; } CORE::warn color('bold red'), $msg; print STDERR color('reset'); }; $SIG{__DIE__} = sub { my $msg = shift; if (__FILE__ eq (caller)[1]) { my ($file, $line) = (caller(1))[1, 2]; $msg =~ s/( at .*? line \d+\.$)/ at $file line $line/m; die $msg; } } if $] < 5.016 ; 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Color die and warn messages
by dbuckhal (Chaplain) on Mar 02, 2024 at 06:28 UTC | |
by Anonymous Monk on Sep 16, 2024 at 16:44 UTC | |
by Anonymous Monk on Oct 31, 2025 at 16:16 UTC | |
|
Re: Color die and warn messages
by Anonymous Monk on Jul 31, 2025 at 22:00 UTC | |
|
Re: Color die and warn messages
by Anonymous Monk on Aug 01, 2025 at 07:20 UTC | |
by sleet (Pilgrim) on Sep 04, 2025 at 07:01 UTC | |
|
Re: Color die and warn messages
by Anonymous Monk on Mar 05, 2025 at 01:05 UTC | |
by Anonymous Monk on Apr 09, 2025 at 10:35 UTC | |
by Anonymous Monk on Jul 13, 2025 at 23:30 UTC |