The second example has a bigger problem, eval {foo} works but foo() on it's own doesn't give a stack trace.
It doesn't?
use Carp qw(confess cluck);
BEGIN {
*CORE::GLOBAL::warn = \&cluck;
*CORE::GLOBAL::die = \&confess;
}
sub foo { bar() }
sub bar { print "in bar\n"; die "badness!" }
foo();
__output__
in bar
badness! at - line 8
main::bar() called at - line 7
main::foo() called at - line 10
The solution is not a one-liner, it needs to pay attention to $^S which indicates whether we're being eval()ed or not.
But this is dealt with by perl, so even with die and warn overridden the value of $^S will be unaffected.
It should also play well with other die/warn handlers.
And it does
use Carp qw(confess cluck);
BEGIN {
*CORE::GLOBAL::warn = \&cluck;
*CORE::GLOBAL::die = \&confess;
}
$SIG{__DIE__} = sub { print "I died: @_\n" };
$SIG{__WARN__} = sub { print "watch out: @_\n" };
sub foo { bar() }
sub bar { print "in &bar\n"; warn "it's behind you!" }
sub baz { qux() }
sub qux { print "in &qux\n"; die "ack!" }
eval { foo; baz; };
print "The End.\n";
__output__
in &bar
watch out: it's behind you! at - line 12
main::bar() called at - line 11
main::foo() called at - line 17
eval {...} called at - line 17
in &qux
I died: ack! at - line 15
main::qux() called at - line 14
main::baz() called at - line 17
eval {...} called at - line 17
The End.
My point is, is that overriding die and warn with the corresponding calls from Carp should work fine as they inevitably call die and warn anyhow. The only problems you should run into with these overrides is in existing code where exception handling could very well get muddled, but this shouldn't be a problem if you use subs to localise this behaviour to a given package e.g
{
package Expressive;
use Carp qw(confess cluck);
use subs qw( die warn );
*die = \&confess;
*warn = \&cluck;
sub test {
warn "warning in ".__PACKAGE__."::test\n";
die "dieing in ".__PACKAGE__."::test\n";
}
}
print "using Expressive::\n";
eval { Expressive::test };
print $@;
print "\nusing main::\n";
eval {
warn "warning in ".__PACKAGE__;
die "dieing in ".__PACKAGE__;
};
print $@;
print "\ndone.\n";
__output__
using Expressive::
warning in Expressive::test
Expressive::test() called at pmsopw_379099.pl line 18
eval {...} called at pmsopw_379099.pl line 18
dieing in Expressive::test
Expressive::test() called at pmsopw_379099.pl line 18
eval {...} called at pmsopw_379099.pl line 18
using main::
warning in main at pmsopw_379099.pl line 23.
dieing in main at pmsopw_379099.pl line 24.
done.
|