handle scripts that override $SIG{__DIE__} and $SIG{__WARN__}
Rafael Kitover [Sun, 6 Dec 2009 05:19:14 +0000 (05:19 +0000)]
Changes
lib/Catalyst/Controller/CGIBin.pm
lib/Catalyst/Controller/WrapCGI.pm
lib/CatalystX/GlobalContext.pm

diff --git a/Changes b/Changes
index f136330..a564961 100644 (file)
--- 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
index 183befd..d1760a7 100644 (file)
@@ -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
index 9fe7843..1a8447c 100644 (file)
@@ -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
 
index 879ff16..fd4fbfa 100644 (file)
@@ -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