I want to write a small subroutine that can decorate all error messages in a consistent way instead of having to copy it all around my program.
However I want the line numbers to be from where it was called, not from where the die
/warn
occurred.
In C I would just use a preprocessor macro, but Perl does not have those. What is the best way to implement this?
Use Carp for warnings/errors. You can use
__WARN__
and__DIE__
hooks to affect whatwarn
prints to theSTDERR
stream and howdie
is thrown. Note that they are quite different.prints to
STDOUT
If you want this to still go to
STDERR
then useprint STDERR "WARNING: @_";
Make sure to carefully read about
%SIG
in perlvar and warn, at least.While it seems that you want this to be global, I'd like to mention that local-izing changes like this is generally what one wants, if possible. There's an example in this post, and more out there.
Adding the following will add stack traces to exceptions and warnings:
For the occasional use, use the following to avoid modifying your program:
or