Support builtin::export_lexically
Toby Inkster
1 year, 9 months ago
135 | 135 |
approach which doesn't just work with Exporter::Tiny, but B<all>
|
136 | 136 |
exporters.
|
137 | 137 |
|
138 | |
=head2 Lexical subs
|
|
138 |
=head2 Lexical subs on Perl 5.37.2 and above
|
139 | 139 |
|
140 | 140 |
Often you want to make use of an exported function, but don't want
|
141 | 141 |
it to "pollute" your namespace.
|
|
142 |
|
|
143 |
On newer versions of Perl, Exporter::Tiny can use C<export_lexically>
|
|
144 |
from L<builtin> to give you lexical versions of exports.
|
|
145 |
|
|
146 |
{
|
|
147 |
use MyUtils -lexical, "frobnicate";
|
|
148 |
|
|
149 |
frobnicate(...); # ok
|
|
150 |
}
|
|
151 |
|
|
152 |
frobnicate(...); # not ok
|
|
153 |
|
|
154 |
=head2 Lexical subs on Perl older than 5.37.2
|
142 | 155 |
|
143 | 156 |
There is this L<Sub::Exporter::Lexical> thing that was designed as a
|
144 | 157 |
plugin for L<Sub::Exporter>, but Exporter::Tiny's API is close enough
|
260 | 260 |
|
261 | 261 |
my $into = $globals->{into};
|
262 | 262 |
my $installer = $globals->{installer} || $globals->{exporter};
|
263 | |
|
|
263 |
|
|
264 |
if ( $into eq '-lexical' or $globals->{lexical} ) {
|
|
265 |
$] ge '5.036002'
|
|
266 |
or _croak( 'Lexical export requires Perl 5.37.2 or above' );
|
|
267 |
$installer ||= sub {
|
|
268 |
my ( $sigilname, $sym ) = @{ $_[1] };
|
|
269 |
no warnings ( $] ge '5.036002' ? 'experimental::builtin' : () );
|
|
270 |
builtin::export_lexically( $sigilname, $sym );
|
|
271 |
};
|
|
272 |
}
|
|
273 |
|
264 | 274 |
$name =
|
265 | 275 |
ref $globals->{as} ? $globals->{as}->($name) :
|
266 | 276 |
ref $value->{-as} ? $value->{-as}->($name) :
|
|
494 | 504 |
|
495 | 505 |
=head1 COPYRIGHT AND LICENCE
|
496 | 506 |
|
497 | |
This software is copyright (c) 2013-2014, 2017 by Toby Inkster.
|
|
507 |
This software is copyright (c) 2013-2014, 2017, 2022 by Toby Inkster.
|
498 | 508 |
|
499 | 509 |
This is free software; you can redistribute it and/or modify it under
|
500 | 510 |
the same terms as the Perl 5 programming language system itself.
|
|
0 |
=pod
|
|
1 |
|
|
2 |
=encoding utf-8
|
|
3 |
|
|
4 |
=head1 PURPOSE
|
|
5 |
|
|
6 |
Tests support for lexical imports on Perl 5.37.2 and above.
|
|
7 |
|
|
8 |
=head1 AUTHOR
|
|
9 |
|
|
10 |
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
|
|
11 |
|
|
12 |
=head1 COPYRIGHT AND LICENCE
|
|
13 |
|
|
14 |
This software is copyright (c) 2022 by Toby Inkster.
|
|
15 |
|
|
16 |
This is free software; you can redistribute it and/or modify it under
|
|
17 |
the same terms as the Perl 5 programming language system itself.
|
|
18 |
|
|
19 |
=cut
|
|
20 |
|
|
21 |
use strict;
|
|
22 |
use warnings;
|
|
23 |
use Test::More;
|
|
24 |
use Test::Requires '5.037002';
|
|
25 |
|
|
26 |
BEGIN {
|
|
27 |
package My::Utils;
|
|
28 |
use Exporter::Shiny qw( foo $bar );
|
|
29 |
our $bar = 42;
|
|
30 |
sub foo { return $bar }
|
|
31 |
};
|
|
32 |
|
|
33 |
{
|
|
34 |
use My::Utils -lexical, qw( foo $bar );
|
|
35 |
is( foo(), 42 );
|
|
36 |
is( $bar, 42 );
|
|
37 |
ok ! main->can( 'foo' );
|
|
38 |
}
|
|
39 |
|
|
40 |
ok ! eval ' foo() ';
|
|
41 |
ok ! eval ' $bar ';
|
|
42 |
|
|
43 |
done_testing;
|