perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stas Bekman <s...@stason.org>
Subject Re: CGI::Carp
Date Mon, 12 May 2003 01:21:16 GMT
Lincoln Stein wrote:
> Sorry, but even when placed in a BEGIN {} block, CORE::GLOBAL::die won't catch 
> compile-time errors the way that $SIG{__DIE__} does. 
> 
> Do you have a workaround?  Otherwise I'm going to go back to using the %SIG 
> magic in CGI::Carp.
> 
> Here's my test script:
> 
> #!/usr/bin/perl
> 
> BEGIN {
>   *CORE::GLOBAL::die = \&mydie; 
> }
> foo bar baz;
> print STDERR "VERSION = $]\n"; print STDERR "line 1\n"; 
> die "dieing here\n"; 
> print STDERR "line 2\n";
> sub mydie {   
>   print STDERR "In die handler\n";   
>   CORE::die $@; 
> }
> 
> Its output is 
> 
> 	Can't locate object method "foo" via package "bar" (perhaps you forgot to 
> load "bar"?) at foo.pl line 6.
> 
> When I replace the BEGIN block with $SIG{__DIE__}=\&mydie, i get:
> 
> In die handler
> Died at foo.pl line 11.

Indeed. *CORE::GLOBAL::die = \&mydie; overrides calls to die(), when it's 
explicitly called. It doesn't provide a replacement for situations where perl 
dies elsewhere. A call to:

foo bar baz

calls Perl_vcroak, which sees no PL_diehook ($SIG{__DIE__}) defined, neither 
it's executed in the eval {} block, so it writes the error message to the 
console and exits.

Perhaps you want to execute this code in the eval block? Then it'll work as 
expected:

#!/usr/bin/perl

BEGIN {
     *CORE::GLOBAL::die = sub {
         print STDERR "In die handler\n";
         CORE::die $@;
     }
}

eval { foo bar baz; };
die $@ if $@;

print STDERR "VERSION = $]\n"; print STDERR "line 1\n";
die "dieing here\n";
print STDERR "line 2\n";

Otherwise using $SIG{__DIE__} is the only catch-all way to do that. Though if 
you do that, you have to make sure to respect $^S flag/ using caller() as 
explained here:
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#Alternative_Exception_Handling_Techniques

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Mime
View raw message