From e1778049ea3b945efb8103d41e0413931f4d9c20 Mon Sep 17 00:00:00 2001 From: Chad Granum Date: Tue, 12 Jul 2016 21:53:09 -0700 Subject: Add :ALL tag --- Changes | 2 ++ lib/Importer.pm | 9 ++++++++- t/all_tag.t | 47 +++++++++++++++++++++++++++++++++++++++++++++++ t/units.t | 4 +++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 t/all_tag.t diff --git a/Changes b/Changes index 25525d8..77448c8 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ {{$NEXT}} + - Add :ALL tag when none is pre-defined + 0.013 2016-07-09 16:00:05-07:00 America/Los_Angeles - Add extra params to custom symbol setters diff --git a/lib/Importer.pm b/lib/Importer.pm index ee3ffd2..39e9407 100644 --- a/lib/Importer.pm +++ b/lib/Importer.pm @@ -2,7 +2,7 @@ package Importer; use strict qw/vars subs/; # Not refs! use warnings; no warnings 'once'; -our $VERSION = '0.013'; +our $VERSION = '0.014'; my %SIG_TO_SLOT = ( '&' => 'CODE', @@ -400,6 +400,9 @@ sub reload_menu { 'DEFAULT' => [ @$export ], }; + # Add 'ALL' tag unless already specified. We want to normalize it. + $tags->{ALL} ||= [ sort grep {m/^[\&\$\@\%\*]/} keys %$lookup ]; + my $fail = @$export_fail ? { map { my ($sig, $name) = (m/^(\W?)(.*)$/); @@ -860,6 +863,10 @@ You can define/import subsets of symbols using predefined tags. use Importer 'Some::Thing' => ':tag'; +L will automatically populate the C<:DEFAULT> tag for you. +L will also give you an C<:ALL> tag with ALL exports so long as the +exporter does not define a C<:ALL> tag already. + =head2 /PATTERN/ or qr/PATTERN/ You can import all symbols that match a pattern. The pattern can be supplied a diff --git a/t/all_tag.t b/t/all_tag.t new file mode 100644 index 0000000..f8a8468 --- /dev/null +++ b/t/all_tag.t @@ -0,0 +1,47 @@ +use Test::More; +use strict; +use warnings; + +BEGIN { + $INC{'My/Exporter/A.pm'} = __FILE__; + $INC{'My/Exporter/B.pm'} = __FILE__; + + package My::Exporter::A; + + our @EXPORT = qw/foo bar/; + our @EXPORT_OK = qw/baz bat/; + + sub foo { 'foo' } + sub bar { 'bar' } + sub baz { 'baz' } + sub bat { 'bat' } + + package My::Exporter::B; + + our @EXPORT = qw/foo bar/; + our @EXPORT_OK = qw/baz bat/; + + our %EXPORT_TAGS = ( + ALL => [qw/foo/], + ); + + sub foo { 'foo' } + sub bar { 'bar' } + sub baz { 'baz' } + sub bat { 'bat' } +} + +subtest "define ALL tag if missing" => sub { + package Importer::A; + use Importer 'My::Exporter::A' => ':ALL'; + main::can_ok(__PACKAGE__, qw/foo bar baz bar/); +}; + +subtest "do not override ALL tag if defined" => sub { + package Importer::B; + use Importer 'My::Exporter::B' => ':ALL'; + main::can_ok(__PACKAGE__, qw/foo/); + main::ok(!__PACKAGE__->can($_), "Did not import $_") for qw/bar baz bat/; +}; + +done_testing; diff --git a/t/units.t b/t/units.t index 35da589..850da4e 100644 --- a/t/units.t +++ b/t/units.t @@ -287,7 +287,7 @@ subtest reload_menu => sub { { lookup => {}, exports => {}, - tags => { DEFAULT => [] }, + tags => { DEFAULT => [], ALL => [] }, fail => undef, generate => undef, magic => {}, @@ -386,6 +386,7 @@ subtest reload_menu => sub { tags => { b => [qw/bar baz/], DEFAULT => [qw/foo &bar $ZAP %ZAP @ZAP/], + ALL => [sort qw/&foo &bar &baz &ick &missing &x &z &gena &genb %ZAP $ZAP @ZAP/], }, fail => { '&ick' => 1, ick => 1 }, magic => {}, @@ -472,6 +473,7 @@ subtest reload_menu => sub { tags => { b => [qw/bar baz/], DEFAULT => [qw/foo &bar $ZAP %ZAP @ZAP/], + ALL => [sort qw/&foo &bar &baz &ick &missing &x &z &gena &genb %ZAP $ZAP @ZAP/], }, fail => { '&ick' => 1, ick => 1 }, magic => {}, -- cgit v1.2.3