In a recent posting, it was suggested that using a $SIG{__WARN__} or $SIG{__DIE__} handler is a great way to add a timestamp to a normal death/warning message. I'd already written code to do that for one of my recent Linux Magazine columns, so here it is.

Each die or warn will be preceded by the timestamp and the process ID on each line of the output.

$SIG{__WARN__} = sub { my $message = shift; my $prefix = "[$$] [".localtime()."] "; $message =~ s/^/$prefix/gm; warn $message; }; $SIG{__DIE__} = sub { my $message = shift; my $prefix = "[$$] [".localtime()."] "; $message =~ s/^/$prefix/gm; die $message; };

Replies are listed 'Best First'.
RE: Timestamping your warns and dies
by Adam (Vicar) on Sep 13, 2000 at 23:50 UTC
    You can also use caller to get even more information about who died... but then you might as well use carp as I learned when I posted Trace Warnings.
RE: Timestamping your warns and dies
by fundflow (Chaplain) on Sep 14, 2000 at 05:46 UTC
    Short question:

    Why not use
    $message = $prefix . $message
    instead of the s///?

    It seems more readable.