develooper Front page | perl.perl5.porters | Postings from December 2015

Re: Proposal: Add {-as => 'new_name'} feature to Exporter.pm

Thread Previous | Thread Next
From:
Kent Fredric
Date:
December 22, 2015 10:04
Subject:
Re: Proposal: Add {-as => 'new_name'} feature to Exporter.pm
Message ID:
CAATnKFC=Hj9Sxd77xd-J_h-nCY5Z=FfHVd_=HjE+k=ZKcHA-Cw@mail.gmail.com
On 22 December 2015 at 13:12, Ricardo Signes <perl.p5p@rjbs.manxome.org> wrote:
> tl;dr -- I think adding this feature as described is the right choice.  We'll
> probably want to make a couple little tweaks and make sure we've got good test
> coverage.
...
> 2.  Is the { -as => "newname" } implementation the right one?
...
>
>     I'm not really sold.  I think the hashref is okay.  We can validate that
>     the only entry in it is "-as", to prevent crazy mistakes.
>
>     Someone mentioned that this doesn't support groups. This is correct.  In
>     Sub::Exporter, groups can take a similar argument, which can contain
>     -prefix or -suffix.  We could also provide that.
>
>       use Pies ':savory' => { -prefix => 'yummy_' };
>       use Cake::Const "/^CK_/" => { -suffix => '_CONST' };
>
>     I'm not too worried about making this look like anything else that uses it.
>     That's a nice little benefit, but there will be other differences, so
>     people will want to know what's under the hood.  I just want it to be easy
>     to skim and understand, and this passes that test.

While we're comparing features Sub::Exporter has but Exporter lacks,
lets remember that very useful ability to just pass a string of the
intended target packages name to `import()` and have it just work:
http://grep.cpan.me/?q=-%3Eimport\%28.{0%2C200}into

This is much much nicer than needing to dick around with `eval`.

Given such a feature is clearly in high enough demand that we may also
want it some day, and that it might also change the import()
interface, it makes sense to at least consider how we'd have both
features available in some future.

Looking in the internals shows me that there already *is* some glue in
Exporter to make this possible, but its currently blocked by all the
user facing API's refusing to communicate an arbitrary target:

- https://metacpan.org/source/TODDR/Exporter-5.72/lib/Exporter.pm#L80-82
- https://metacpan.org/source/TODDR/Exporter-5.72/lib/Exporter/Heavy.pm#L217

But it looks like you can replace:

    eval "package Foo; Bar->import(@args)"

With

     Exporter::Heavy::heavy_export("Bar","Foo",@args);

But it turns out there's almost nobody using that syntax on CPAN:
http://grep.cpan.me/?q=heavy_export

But one of the 2 cases there that actually calls that is really interesting....

- https://metacpan.org/source/ANNO/Exporter-Renaming-1.19/lib/Exporter/Renaming.pm

Yeah. Prior Art of exactly the sort of thing we want to be doing, with
some superficial implementation similarities

Its kinda gross because it requires temporarily monkey patching
Exporter to give the new syntax, and then hoping you didn't break
anything you didn't mean to tweak, and then putting the burden on you
to unbreak it when you're done.

- https://metacpan.org/source/ANNO/Exporter-Renaming-1.19/lib/Exporter/Renaming.pm#L19-24
- https://metacpan.org/source/ANNO/Exporter-Renaming-1.19/lib/Exporter/Renaming.pm#L28-33

But still interesting as research material.


-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About