Change 23627 by [EMAIL PROTECTED] on 2004/12/08 16:19:38

        Subject: [PATCH] Re: [perl #32949] FileCache only works in "main" 
package
        From: "Jos I. Boumans" <[EMAIL PROTECTED]>
        Date: Wed, 8 Dec 2004 14:24:19 +0100
        Message-Id: <[EMAIL PROTECTED]>

Affected files ...

... //depot/perl/MANIFEST#1198 edit
... //depot/perl/lib/FileCache.pm#17 edit
... //depot/perl/lib/FileCache/t/06export.t#1 add

Differences ...

==== //depot/perl/MANIFEST#1198 (text) ====
Index: perl/MANIFEST
--- perl/MANIFEST#1197~23598~   Wed Dec  1 15:13:32 2004
+++ perl/MANIFEST       Wed Dec  8 08:19:38 2004
@@ -1345,6 +1345,7 @@
 lib/FileCache/t/03append.t     See if FileCache works
 lib/FileCache/t/04twoarg.t     See if FileCache works
 lib/FileCache/t/05override.t   See if FileCache works
+lib/FileCache/t/06export.t     See if FileCache exporting works
 lib/File/CheckTree.pm          Perl module supporting wholesale file mode 
validation
 lib/File/CheckTree.t           See if File::CheckTree works
 lib/File/Compare.pm            Emulation of cmp command

==== //depot/perl/lib/FileCache.pm#17 (text) ====
Index: perl/lib/FileCache.pm
--- perl/lib/FileCache.pm#16~22971~     Tue Jun 22 14:00:10 2004
+++ perl/lib/FileCache.pm       Wed Dec  8 08:19:38 2004
@@ -84,15 +84,27 @@
 # These are not C<my> for legacy reasons.
 # Previous versions requested the user set $cacheout_maxopen by hand.
 # Some authors fiddled with %saw to overcome the clobber on initial open.
-use vars qw(%saw $cacheout_maxopen);
+use vars qw(%saw $cacheout_maxopen @EXPORT);
 my %isopen;
 my $cacheout_seq = 0;
 
 sub import {
     my ($pkg,%args) = @_;
-    $pkg = caller(1);
-    *{$pkg.'::cacheout'} = \&cacheout;
-    *{$pkg.'::close'}    = \&cacheout_close;
+
+    # Not using Exporter is naughty.
+    # Also, using caller(1) is just wrong.
+    #$pkg = caller(1);
+    #*{$pkg.'::cacheout'} = \&cacheout;
+    #*{$pkg.'::close'}    = \&cacheout_close;
+
+    # Use Exporter. %args are for us, not Exporter.
+    # Make sure to up export_to_level, or we will import into ourselves,
+    # rather than our calling package;
+    use base 'Exporter';
+    @EXPORT = qw[cacheout cacheout_close];
+
+    __PACKAGE__->export_to_level(1);
+    Exporter::import( $pkg );
 
     # Truth is okay here because setting maxopen to 0 would be bad
     return $cacheout_maxopen = $args{maxopen} if $args{maxopen};

==== //depot/perl/lib/FileCache/t/06export.t#1 (text) ====
Index: perl/lib/FileCache/t/06export.t
--- /dev/null   Tue May  5 13:32:27 1998
+++ perl/lib/FileCache/t/06export.t     Wed Dec  8 08:19:38 2004
@@ -0,0 +1,62 @@
+#!./perl
+BEGIN {
+    chdir 't' if -d 't';
+
+    #For tests within the perl distribution
+    @INC = '../lib' if -d '../lib';
+    END;
+
+    # Functions exported by FileCache;
+    @funcs  = qw[cacheout cacheout_close];
+    $i      = 0;
+    
+    # number of tests
+    print "1..8\n";
+}
+
+# Test 6: Test that exporting both works to package main and
+# other packages. Now using Exporter.
+
+# First, we shouldn't be able to have these in our namespace
+# Add them to BEGIN so the later 'use' doesn't influence this
+# test
+BEGIN {   
+    for my $f (@funcs) {
+        ++$i;
+        print 'not ' if __PACKAGE__->can($f);
+        print "ok $i\n"; 
+    }
+}
+
+# With an empty import list, we also shouldn't have them in
+# our namespace.
+# Add them to BEGIN so the later 'use' doesn't influence this
+# test
+BEGIN {   
+    use FileCache ();
+    for my $f (@funcs) {
+        ++$i;
+        print 'not ' if __PACKAGE__->can($f);
+        print "ok $i\n"; 
+    }
+}
+
+
+# Now, we use FileCache in 'main'
+{   use FileCache;
+    for my $f (@funcs) {
+        ++$i;
+        print 'not ' if !__PACKAGE__->can($f);
+        print "ok $i\n"; 
+    }
+}
+
+# Now we use them in another package
+{   package X;
+    use FileCache;
+    for my $f (@main::funcs) {
+        ++$main::i;
+        print 'not ' if !__PACKAGE__->can($f);
+        print "ok $main::i\n"; 
+    }
+}
End of Patch.

Reply via email to