From: Rafael Kitover Date: Sun, 6 Dec 2009 05:19:14 +0000 (+0000) Subject: handle scripts that override $SIG{__DIE__} and $SIG{__WARN__} X-Git-Tag: 0.030~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-WrapCGI.git;a=commitdiff_plain;h=5464705b4645ee3f5c5694cbc8e1d9e8f12da983 handle scripts that override $SIG{__DIE__} and $SIG{__WARN__} --- diff --git a/Changes b/Changes index f136330..a564961 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Catalyst-Controller-WrapCGI +0.022 2009-12-06 05:18:08 + - handle scripts that override $SIG{__DIE__} and $SIG{__WARN__} + 0.021 2009-12-04 21:05:14 - better is_perl_cgi determination that also allows using MyApp->foo methods diff --git a/lib/Catalyst/Controller/CGIBin.pm b/lib/Catalyst/Controller/CGIBin.pm index 183befd..d1760a7 100644 --- a/lib/Catalyst/Controller/CGIBin.pm +++ b/lib/Catalyst/Controller/CGIBin.pm @@ -24,11 +24,11 @@ Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin =head1 VERSION -Version 0.021 +Version 0.022 =cut -our $VERSION = '0.021'; +our $VERSION = '0.022'; =head1 SYNOPSIS @@ -259,9 +259,13 @@ sub wrap_perl_cgi { my $code = slurp $cgi; - $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms; + $code =~ s/^__DATA__\n(.*)//ms; my $data = $1; + my $orig_exit = \*CORE::GLOBAL::exit; + my $orig_die = $SIG{__DIE__}; + my $orig_warn = $SIG{__WARN__}; + my $coderef = do { no warnings; # catch exit() and turn it into (effectively) a return @@ -269,17 +273,19 @@ sub wrap_perl_cgi { # overridden CORE::GLOBAL::exit in view # # set $0 to the name of the cgi file in case it's used there - eval ' + my $source = ' my $cgi_exited = "EXIT\n"; BEGIN { *CORE::GLOBAL::exit = sub (;$) { die [ $cgi_exited, $_[0] || 0 ]; } } package Catalyst::Controller::CGIBin::_CGIs_::'.$action_name.'; - sub {' - . 'local *DATA;' - . q{open DATA, '<', \$data;} - . qq{local \$0 = "\Q$cgi\E";} - . q/my $rv = eval {/ + sub {'."\n" + . 'local *DATA;'."\n" + . q{open DATA, '<', \$data;}."\n" + . qq{local \$0 = "\Q$cgi\E";}."\n" + . q/my $rv = eval {/."\n" + . 'local $SIG{__DIE__} = $SIG{__DIE__} || sub { die @_ };'."\n" + . 'local $SIG{__WARN__} = $SIG{__WARN__} || sub { warn @_ };'."\n" . $code . q/};/ . q{ @@ -292,11 +298,17 @@ sub wrap_perl_cgi { return $rv; } . '}'; + eval $source; }; + # clean up + *CORE::GLOBAL::exit = $orig_exit; + $SIG{__DIE__} = $orig_die; + $SIG{__WARN__} = $orig_warn; + croak __PACKAGE__ . ": Could not compile $cgi to coderef: $@" if $@; - $coderef + return $coderef } =head2 wrap_nonperl_cgi diff --git a/lib/Catalyst/Controller/WrapCGI.pm b/lib/Catalyst/Controller/WrapCGI.pm index 9fe7843..1a8447c 100644 --- a/lib/Catalyst/Controller/WrapCGI.pm +++ b/lib/Catalyst/Controller/WrapCGI.pm @@ -20,11 +20,11 @@ Catalyst::Controller::WrapCGI - Run CGIs in Catalyst =head1 VERSION -Version 0.021 +Version 0.022 =cut -our $VERSION = '0.021'; +our $VERSION = '0.022'; =head1 SYNOPSIS diff --git a/lib/CatalystX/GlobalContext.pm b/lib/CatalystX/GlobalContext.pm index 879ff16..fd4fbfa 100644 --- a/lib/CatalystX/GlobalContext.pm +++ b/lib/CatalystX/GlobalContext.pm @@ -15,11 +15,11 @@ CatalystX::GlobalContext - Export Catalyst Context =head1 VERSION -Version 0.021 +Version 0.022 =cut -our $VERSION = '0.021'; +our $VERSION = '0.022'; =head1 SYNOPSIS