From: Peter Rabbitson <ribasushi@cpan.org>
Date: Sat, 19 Mar 2011 23:55:56 +0000 (+0000)
Subject: Preserve $_ (RT#66661)
X-Git-Tag: v1.001000~9
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=15b7d164b9387c72413b53f0ebaa4f1671895220;p=p5sagit%2FClass-C3-Componentised.git

Preserve $_ (RT#66661)
---

diff --git a/Changes b/Changes
index 0f95060..f6d3fe6 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,8 @@ Revision history for Class-C3-Componentised
 
         Stop importing Carp functions (and thus polluting the inheritor
         namespaces)
+        Make sure $_ is properly preserved even if the loaded module
+        is negligent enough to change it (RT#66661)
 
 1.0008   24 Feb 2011
         Fix inject_base regression introduced during optimizations in
diff --git a/lib/Class/C3/Componentised.pm b/lib/Class/C3/Componentised.pm
index a86108a..a6a9c16 100644
--- a/lib/Class/C3/Componentised.pm
+++ b/lib/Class/C3/Componentised.pm
@@ -142,9 +142,9 @@ sub ensure_class_loaded {
     return if ( *{"${f_class}::$_"}{CODE} );
   }
 
-
   # require always returns true on success
-  eval { require($file) } or do {
+  # ill-behaved modules might very well obliterate $_
+  eval { local $_; require($file) } or do {
 
     $@ = "Invalid class name '$f_class'" if $f_class =~ $invalid_class;
 
diff --git a/t/03-unstable_dollar_underscore.t b/t/03-unstable_dollar_underscore.t
new file mode 100644
index 0000000..0c657cd
--- /dev/null
+++ b/t/03-unstable_dollar_underscore.t
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+
+use Class::C3::Componentised;
+use Test::More;
+use FindBin;
+
+use lib "$FindBin::Bin/lib";
+
+my @mods = 'DestroyDollarUnderscore';
+
+for (@mods) {
+  Class::C3::Componentised->ensure_class_loaded($_);
+}
+
+is_deeply(\@mods, [ 'DestroyDollarUnderscore' ], '$_ untouched');
+
+done_testing;
diff --git a/t/lib/DestroyDollarUnderscore.pm b/t/lib/DestroyDollarUnderscore.pm
new file mode 100644
index 0000000..f514035
--- /dev/null
+++ b/t/lib/DestroyDollarUnderscore.pm
@@ -0,0 +1,8 @@
+package DestroyDollarUnderscore;
+
+use warnings;
+use strict;
+
+undef ($_);
+
+1;