Preserve $_ (RT#66661)
Peter Rabbitson [Sat, 19 Mar 2011 23:55:56 +0000 (23:55 +0000)]
Changes
lib/Class/C3/Componentised.pm
t/03-unstable_dollar_underscore.t [new file with mode: 0644]
t/lib/DestroyDollarUnderscore.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index 0f95060..f6d3fe6 100644 (file)
--- 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
index a86108a..a6a9c16 100644 (file)
@@ -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 (file)
index 0000000..0c657cd
--- /dev/null
@@ -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 (file)
index 0000000..f514035
--- /dev/null
@@ -0,0 +1,8 @@
+package DestroyDollarUnderscore;
+
+use warnings;
+use strict;
+
+undef ($_);
+
+1;