Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] Add renaming capabilities to Exporter.pm in blead perl #15095

Closed
p5pRT opened this issue Dec 18, 2015 · 10 comments
Closed

[PATCH] Add renaming capabilities to Exporter.pm in blead perl #15095

p5pRT opened this issue Dec 18, 2015 · 10 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 18, 2015

Migrated from rt.perl.org#126953 (status was 'rejected')

Searchable as RT126953$

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

From @exodist

Add renaming capabilities to Exporter.pm

* Uses the same syntax as newer exporters for consistency.
* Added tests.
* Added documentation.
* Added another exporter to the list at the end.

(Sorry if this is a duplicate, I did not include the proper subject last
time so I think it was rejected)

Adding blead and perl to body so that it is hopefully not rejected this
time.

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

From @exodist

oops, here is the patch

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

From @exodist

0001-Add-renaming-capabilities-to-Exporter.pm.patch
From 1957f9e25f435d39a6e9d444db17deeee887f587 Mon Sep 17 00:00:00 2001
From: Chad Granum <exodist7@gmail.com>
Date: Thu, 17 Dec 2015 19:15:23 -0800
Subject: [PATCH] Add renaming capabilities to Exporter.pm

 * Uses the same syntax as newer exporters for consistency.
 * Added tests
 * Added documentation
 * Added another exporter to the list at the end
---
 dist/Exporter/lib/Exporter.pm       | 14 +++++++++++++-
 dist/Exporter/lib/Exporter/Heavy.pm | 23 ++++++++++++++++-------
 dist/Exporter/t/Exporter.t          | 20 +++++++++++++++++++-
 3 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/dist/Exporter/lib/Exporter.pm b/dist/Exporter/lib/Exporter.pm
index 0b3db21..4e94c70 100644
--- a/dist/Exporter/lib/Exporter.pm
+++ b/dist/Exporter/lib/Exporter.pm
@@ -9,7 +9,7 @@ require 5.006;
 our $Debug = 0;
 our $ExportLevel = 0;
 our $Verbose ||= 0;
-our $VERSION = '5.72';
+our $VERSION = '5.73';
 our (%Cache);
 
 sub as_heavy {
@@ -449,6 +449,17 @@ If you are writing a package that C<AUTOLOAD>s, consider forcing
 an C<AUTOLOAD> for any constants explicitly imported by other packages
 or which are usually used when your package is C<use>d.
 
+=head2 RENAMING SYMBOLS ON IMPORT
+
+As of version 5.73 you can rename symbols that you import.
+
+    use Some::Exporter some_sub => { -as => 'new_name' };
+
+    new_name(); # sub 'some_sub' is now present as 'new_name'.
+
+This syntax is consistent with newer exporting tools such as L<Sub::Exporter>
+and L<Exporter::Declare>.
+
 =head1 Good Practices
 
 =head2 Declaring C<@EXPORT_OK> and Friends
@@ -579,6 +590,7 @@ a sample list of such modules.
     Exporter::Tidy
     Sub::Exporter / Sub::Installer
     Perl6::Export / Perl6::Export::Attrs
+    Exporter::Declare
 
 =head1 LICENSE
 
diff --git a/dist/Exporter/lib/Exporter/Heavy.pm b/dist/Exporter/lib/Exporter/Heavy.pm
index 21b67c1..9f48e8b 100644
--- a/dist/Exporter/lib/Exporter/Heavy.pm
+++ b/dist/Exporter/lib/Exporter/Heavy.pm
@@ -145,7 +145,7 @@ sub heavy_export {
 			$cache_is_current = 1;
 		    }
 
-		    if (!$export_cache->{$sym}) {
+ 		    if (!$export_cache->{$sym} && ref($sym) ne 'HASH') {
 			# accumulate the non-exports
 			push @carp,
 			  qq["$sym" is not exported by the $pkg module\n];
@@ -194,13 +194,22 @@ sub heavy_export {
     warn "Importing into $callpkg from $pkg: ",
 		join(", ",sort @imports) if $Exporter::Verbose;
 
-    foreach $sym (@imports) {
-	# shortcut for the common case of no type character
-	(*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
-	    unless $sym =~ s/^(\W)//;
-	$type = $1;
+    while (my $sym  = shift @imports) {
+        # First split into sigil and name
+        # We need to take the sigil from the symbol, not the new name where a
+        # sigil is not used.
+        my $type = ($sym =~ s/^(\W)//) ? $1 : '';
+
+        # Update name if an alternative was specified
+        my $spec = (@imports && ref($imports[0]) eq 'HASH') ? shift @imports : undef;
+        my $name = ($spec && $spec->{'-as'}) ? $spec->{'-as'} : $sym;
+
+        # shortcut for the common case of no type character
+        (*{"${callpkg}::$name"} = \&{"${pkg}::$sym"}, next)
+            unless $type;
+
 	no warnings 'once';
-	*{"${callpkg}::$sym"} =
+	*{"${callpkg}::$name"} =
 	    $type eq '&' ? \&{"${pkg}::$sym"} :
 	    $type eq '$' ? \${"${pkg}::$sym"} :
 	    $type eq '@' ? \@{"${pkg}::$sym"} :
diff --git a/dist/Exporter/t/Exporter.t b/dist/Exporter/t/Exporter.t
index d6ac63f..2a30ca8 100644
--- a/dist/Exporter/t/Exporter.t
+++ b/dist/Exporter/t/Exporter.t
@@ -18,7 +18,7 @@ sub ok ($;$) {
 
 BEGIN {
     $test = 1;
-    print "1..31\n";
+    print "1..34\n";
     require Exporter;
     ok( 1, 'Exporter compiled' );
 }
@@ -243,3 +243,21 @@ sub TIESCALAR{bless[]}
  }
 }
 ::ok(1, 'import with tied $_');
+
+package main::TestRename;
+
+BEGIN {
+    $INC{'main/TestRename/Foo.pm'} = 1;
+    package main::TestRename::Foo;
+
+    our @EXPORT = qw/foo/;
+    use base 'Exporter';
+
+    sub foo { 'foo' }
+}
+
+use main::TestRename::Foo 'foo' => {-as => 'bar'};
+
+::ok(__PACKAGE__->can('bar'), "imported foo as bar");
+::ok(!__PACKAGE__->can('foo'), "Did not import 'foo'");
+::ok(bar() eq 'foo', "bar does what we expect");
-- 
1.9.1

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

From @exodist

Looks like my patch causes a POD test failure, but when I check the file I edited I do not see any pod errors, not sure how to fix that...

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

From @jkeenan

On Thu Dec 17 19​:36​:24 2015, exodist7@​gmail.com wrote​:

Looks like my patch causes a POD test failure, but when I check the
file I edited I do not see any pod errors, not sure how to fix that...

While this is being discussed on p5p ;-) ...

let's start to smoke it. POD problem addressed and smoking in this branch​:

smoke-me/jkeenan/exodist/126953-exporter

Note​: Exporter is a "dual-life, blead upstream" distribution. That suggests that before committing, we should try this out on older versions of Perl. Based on its Makefile.PL on CPAN, we expect it to work on Perl 5.6+.

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Dec 18, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jan 14, 2016

From @tonycoz

On Thu Dec 17 19​:33​:54 2015, exodist7@​gmail.com wrote​:

oops, here is the patch

The mailing list discussion has newer patches, do you have a newer patch that I should be looking at instead?

Tony

@p5pRT
Copy link
Author

p5pRT commented Jan 27, 2016

From @exodist

This ticket can be closed in favor of #127384

@p5pRT
Copy link
Author

p5pRT commented Feb 2, 2016

From @tonycoz

On Tue Jan 26 20​:08​:41 2016, exodist7@​gmail.com wrote​:

This ticket can be closed in favor of #127384

Done.

Tony

@p5pRT
Copy link
Author

p5pRT commented Feb 2, 2016

@tonycoz - Status changed from 'open' to 'rejected'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant