Remove local $_ from Exporter
authorFather Chrysostomos <sprout@cpan.org>
Sun, 11 Dec 2011 00:36:03 +0000 (16:36 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 11 Dec 2011 00:39:43 +0000 (16:39 -0800)
This was added in commit 732bb7c2d4f.

The changes in that commit did not necessitate the addition of
local $_.  So the localisation is wasting CPU cycles.  Worse, it
causes bugs in 5.12 and earlier.  (local $_ is always wrong if you
don’t control what is in $_ already, because it could be a read-only
tied variable.)  Actually, it causes bugs in 5.14-15 still, because
it seems that the changes to ‘local $_’ still weren’t sufficient (it
still calls FETCH, but not STORE).  That itself needs fixing, but that
should not obviate the need for this change, as Exporter has been liv-
ing a double life.

lib/Exporter.pm
lib/Exporter.t

index faa36c6..40af220 100644 (file)
@@ -44,7 +44,6 @@ sub import {
   my $export_cache = ($Cache{$pkg} ||= {});
   my $args = @_ or @_ = @$exports;
 
-  local $_;
   if ($args and not %$export_cache) {
     s/^&//, $export_cache->{$_} = 1
       foreach (@$exports, @{"$pkg\::EXPORT_OK"});
index 0e69cb1..06c4b05 100644 (file)
@@ -25,7 +25,7 @@ sub ok ($;$) {
 
 BEGIN {
     $test = 1;
-    print "1..30\n";
+    print "1..31\n";
     require Exporter;
     ok( 1, 'Exporter compiled' );
 }
@@ -233,3 +233,20 @@ eval { Carp::croak() };
 ::ok($Carp::Internal{Exporter}, "Carp recognizes Exporter");
 ::ok($Carp::Internal{'Exporter::Heavy'}, "Carp recognizes Exporter::Heavy");
 
+package Exporter::for::Tied::_;
+
+@ISA = 'Exporter';
+@EXPORT = 'foo';
+
+package Tied::_;
+
+sub TIESCALAR{bless[]}
+# no tie methods!
+
+{
+ tie my $t, __PACKAGE__;
+ for($t) { # $_ is now tied
+  import Exporter::for::Tied::_;
+ }
+}
+::ok(1, 'import with tied $_');