diff options
author | Chad Granum <exodist7@gmail.com> | 2016-01-22 19:30:47 -0800 |
---|---|---|
committer | Chad Granum <exodist7@gmail.com> | 2016-01-22 19:30:47 -0800 |
commit | 69f82ac8c8e82efaac13af2c222b39e8cb5f3374 (patch) | |
tree | d11d7e186bccbc0a90a0a84e1a25162e94af7b60 | |
parent | 41a42c218fe4e2849d9a298f5065d1af45c54c74 (diff) |
Remove cruft, fix docs
-rw-r--r-- | Changes | 4 | ||||
-rw-r--r-- | README | 13 | ||||
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | lib/Importer.pm | 102 | ||||
-rw-r--r-- | lib/Importer/Exporter.pm | 110 | ||||
-rw-r--r-- | lib/Importer/Exporter/Heavy.pm | 68 | ||||
-rw-r--r-- | t/units.t | 22 |
7 files changed, 58 insertions, 276 deletions
@@ -1,5 +1,9 @@ {{$NEXT}} + - Doc updates + - Remove Exporter.pm clone + - Further optimize _optimal_import + 0.005 2016-01-22 09:21:17-08:00 America/Los_Angeles - Fix for older perls @@ -12,7 +12,6 @@ DESCRIPTION *** EXPERIMENTAL *** This module is still experimental. Anything can change at any time. - Testing is currently VERY insufficient. SYNOPSYS # Import defaults @@ -167,6 +166,8 @@ SUPPORTED VARIABLES here are exported by default. If possible you should put symbols in @EXPORT_OK instead. + our @EXPORT = qw/foo bar &baz $BAT/; + @EXPORT_OK This is used exactly the way Exporter uses it. @@ -174,6 +175,8 @@ SUPPORTED VARIABLES Symbols listed here are not exported by default. This is preferred over @EXPORT. + our @EXPORT_OK = qw/foo bar &baz $BAT/; + %EXPORT_TAGS This module supports tags exactly the way Exporter does. @@ -189,6 +192,8 @@ SUPPORTED VARIABLES "$from->export_fail(@items)" callback to try to resolve the issue. See Exporter.pm for documentation of this feature. + our @EXPORT_FAIL = qw/maybe_bad/; + %EXPORT_ANON This is new to this module, Exporter does not support it. @@ -284,8 +289,10 @@ USING WITH OTHER EXPORTER IMPLEMENTATIONS return $ref; } - All exports must be listed in either @EXPORT or @EXPORT_OK to be - allowed. %EXPORT_TAGS, @EXPORT_FAIL, and "\&GENERATE" are optional. + All exports must be listed in either @EXPORT or @EXPORT_OK, or be keys + in %EXPORT_GEN or %EXPORT_ANON to be allowed. 'export_tags', + 'export_fail', 'export_anon', 'export_gen', and 'generate' are optional. + You cannot combine 'generate' and 'export_gen'. Note: If your GENERATE sub needs the $class, $into, or $caller then your "IMPORTER_MENU()" method will need to build an anonymous sub that closes @@ -13,8 +13,7 @@ other variables. # \*\*\* EXPERIMENTAL \*\*\* -This module is still experimental. Anything can change at any time. Testing is -currently VERY insufficient. +This module is still experimental. Anything can change at any time. # SYNOPSYS @@ -188,6 +187,8 @@ List of symbols to export. Sigil is optional for subs. Symbols listed here are exported by default. If possible you should put symbols in `@EXPORT_OK` instead. + our @EXPORT = qw/foo bar &baz $BAT/; + ## @EXPORT\_OK This is used exactly the way [Exporter](https://metacpan.org/pod/Exporter) uses it. @@ -195,6 +196,8 @@ This is used exactly the way [Exporter](https://metacpan.org/pod/Exporter) uses List of symbols that can be imported. Sigil is optional for subs. Symbols listed here are not exported by default. This is preferred over `@EXPORT`. + our @EXPORT_OK = qw/foo bar &baz $BAT/; + ## %EXPORT\_TAGS This module supports tags exactly the way [Exporter](https://metacpan.org/pod/Exporter) does. @@ -212,6 +215,8 @@ to import one of these, Importer will hit your `$from->export_fail(@items)` callback to try to resolve the issue. See [Exporter.pm](https://metacpan.org/pod/Exporter.pm) for documentation of this feature. + our @EXPORT_FAIL = qw/maybe_bad/; + ## %EXPORT\_ANON This is new to this module, [Exporter](https://metacpan.org/pod/Exporter) does not support it. @@ -315,8 +320,10 @@ to support Importer by putting this sub in your package: return $ref; } -All exports must be listed in either `@EXPORT` or `@EXPORT_OK` to be allowed. -`%EXPORT_TAGS`, `@EXPORT_FAIL`, and `\&GENERATE` are optional. +All exports must be listed in either `@EXPORT` or `@EXPORT_OK`, or be keys in +`%EXPORT_GEN` or `%EXPORT_ANON` to be allowed. 'export\_tags', 'export\_fail', +'export\_anon', 'export\_gen', and 'generate' are optional. You cannot combine +'generate' and 'export\_gen'. **Note:** If your GENERATE sub needs the `$class`, `$into`, or `$caller` then your `IMPORTER_MENU()` method will need to build an anonymous sub that closes diff --git a/lib/Importer.pm b/lib/Importer.pm index a92f82e..3a7a7d1 100644 --- a/lib/Importer.pm +++ b/lib/Importer.pm @@ -2,7 +2,7 @@ package Importer; use strict; use warnings; -our $VERSION = 0.005; +our $VERSION = 0.006; my %SIG_TO_SLOT = ( '&' => 'CODE', @@ -12,7 +12,7 @@ my %SIG_TO_SLOT = ( '*' => 'GLOB', ); -my %IMPORTED; +our %IMPORTED; # This will be used to check if an import arg is a version number my %NUMERIC = map { $_ => 1 } 0 .. 9; @@ -25,33 +25,6 @@ my %NON_OPTIMAL = ( '$' => 1, '@' => 1, '%' => 1, '*' => 1 ); ########################################################################### # -# This is a method intended to be used as 'import' by packages that want to -# export on use. -# - -our @EXPORT_OK = qw/exporter_import/; -sub exporter_import { - my $from = shift; - - my @caller = caller(0); - - return unless @_; - - my $file = _mod_to_file($from); - _load_file(\@caller, $file) unless $INC{$file}; - - return if _optimal_import($from, $caller[0], \@caller, @_); - - my $self = __PACKAGE__->new( - from => $from, - caller => \@caller, - ); - - $self->do_import($caller[0], @_); -} - -########################################################################### -# # These are class methods # import and unimport are what you would expect. # import_into and unimport_from are the indirect forms you can use in other @@ -580,44 +553,35 @@ sub _optimal_import { my %new_style = $from->can('IMPORTER_MENU') ? $from->IMPORTER_MENU : (); - my %final; no strict 'refs'; + return 0 if $new_style{export_fail} || @{"$from\::EXPORT_FAIL"}; @args = @{$new_style{export} || "$from\::EXPORT"} unless @args; - my %allowed = map +($_ => 1), @{$new_style{export} || "$from\::EXPORT"}, @{$new_style{export_ok} || "$from\::EXPORT_OK"}; - use strict 'refs'; - - for my $arg (@args) { - # Get sigil, or first letter of name - my $sig = substr($arg, 0, 1); - - # Return if non-sub sigil - return 0 if $NON_OPTIMAL{$sig}; - - # Strip sigil (if sub) - my $name = $arg; - substr($name, 0, 1, '') if $sig eq '&'; - - # Check if the name is allowed (with or without sigil) - return 0 unless $allowed{$name} || $allowed{$arg}; - - no strict 'refs'; - $final{$name} = \&{"$from\::$name"}; - } - - # This is necessary for the eval. - my $IMPORTED = \%IMPORTED; + my %allowed = map +(substr($_, 0, 1) eq '&' ? substr($_, 1) : $_ => 1), + @{$new_style{export} || "$from\::EXPORT"}, + @{$new_style{export_ok} || "$from\::EXPORT_OK"}; + + # The things we do for optimization, this is to avoid adding a scope. + # We have a conditionalm if it is true we return 0 so we can do a complex import. + # If the conditional is false we add the export to the list of exports. + # This lets us abort as soon as possible for a complex import. For an + # import that is not complex it lets us continue quickly. + # Conditional first checks if it has a sigil other than '&' + # Conditional then checks if the item is in the allowed list. The item may + # have a '&' sigil which will make it fail the check, in which case we + # strip the sigil and try again. + my %final = map +( + $NON_OPTIMAL{substr($_, 0, 1)} || !($allowed{$_} || (substr($_, 0, 1, "") && $allowed{$_})) + ? return 0 + : ($_ => \&{"$from\::$_"}) + ), @args; - # This effects the eval, which is what we want. Putting this here means it - # runs once, at build time. Putting it inside the eval means it runs each - # time _optimal_import is called, which is costly. - no strict 'refs'; eval <<" EOT" || die $@; # If the caller has redefine warnings enabled then we want to warn them if # their import redefines things. BEGIN { \${^WARNING_BITS} = \$caller->[9] if defined \$caller->[9] }; #line $caller->[2] "$caller->[1]" -(*{"$into\\::\$_"} = \$final{\$_}, push \@{\$IMPORTED->{\$into}} => \$_) for keys %final; +(*{"$into\\::\$_"} = \$final{\$_}, push \@{\$Importer::IMPORTED{\$into}} => \$_) for keys %final; 1; EOT } @@ -645,8 +609,7 @@ other variables. =head1 *** EXPERIMENTAL *** -This module is still experimental. Anything can change at any time. Testing is -currently VERY insufficient. +This module is still experimental. Anything can change at any time. =head1 SYNOPSYS @@ -828,6 +791,8 @@ List of symbols to export. Sigil is optional for subs. Symbols listed here are exported by default. If possible you should put symbols in C<@EXPORT_OK> instead. + our @EXPORT = qw/foo bar &baz $BAT/; + =head2 @EXPORT_OK This is used exactly the way L<Exporter> uses it. @@ -835,6 +800,8 @@ This is used exactly the way L<Exporter> uses it. List of symbols that can be imported. Sigil is optional for subs. Symbols listed here are not exported by default. This is preferred over C<@EXPORT>. + our @EXPORT_OK = qw/foo bar &baz $BAT/; + =head2 %EXPORT_TAGS This module supports tags exactly the way L<Exporter> does. @@ -852,6 +819,8 @@ to import one of these, Importer will hit your C<< $from->export_fail(@items) >> callback to try to resolve the issue. See L<Exporter.pm> for documentation of this feature. + our @EXPORT_FAIL = qw/maybe_bad/; + =head2 %EXPORT_ANON This is new to this module, L<Exporter> does not support it. @@ -925,13 +894,6 @@ name, or a caller level. =back -=head1 GIVING YOUR PACKAGE AN OLD-SCHOOL IMPORT METHOD - - package My::Exporter; - use Importer Importer => ('exporter_import' => {-as => 'import'}); - - ... - =head1 USING WITH OTHER EXPORTER IMPLEMENTATIONS If you want your module to work with Importer, but you use something other than @@ -966,8 +928,10 @@ to support Importer by putting this sub in your package: return $ref; } -All exports must be listed in either C<@EXPORT> or C<@EXPORT_OK> to be allowed. -C<%EXPORT_TAGS>, C<@EXPORT_FAIL>, and C<\&GENERATE> are optional. +All exports must be listed in either C<@EXPORT> or C<@EXPORT_OK>, or be keys in +C<%EXPORT_GEN> or C<%EXPORT_ANON> to be allowed. 'export_tags', 'export_fail', +'export_anon', 'export_gen', and 'generate' are optional. You cannot combine +'generate' and 'export_gen'. B<Note:> If your GENERATE sub needs the C<$class>, C<$into>, or C<$caller> then your C<IMPORTER_MENU()> method will need to build an anonymous sub that closes diff --git a/lib/Importer/Exporter.pm b/lib/Importer/Exporter.pm deleted file mode 100644 index 1a1df7a..0000000 --- a/lib/Importer/Exporter.pm +++ /dev/null @@ -1,110 +0,0 @@ -package Importer::Exporter; -use strict; -use warnings; - -use Importer Importer => ( - exporter_import => { -as => 'import' }, - exporter_import => { -as => 'export' }, -); - -our @EXPORT_OK = qw/import/; - -sub export_fail { shift; @_ } - -sub export_to_level { - my $from = shift; - my ($level, $ignore, @args) = @_; - Importer->import_into($from, $level + 1, @args); -} - -sub require_version { - my ($self, $wanted) = @_; - my $pkg = ref $self || $self; - return ${pkg}->VERSION($wanted); -} - -my $push_tags = sub { - my $from = shift; - my ($var, @tags) = @_; - - no strict 'refs'; - my $export = \%{"$from\::$var"}; - my $export_tags = \%{"$from\::EXPORT_TAGS"}; - use strict 'refs'; - - my @nontag = (); - for my $tag (@tags ? @tags : keys %$export_tags) { - my $tag_list = $export_tags->{$tag}; - $tag_list ? push @$export => @$tag_list : push @nontag => $tag; - }; - - return unless @nontag && $^W; - - require Carp; - Carp::carp(join(", ", @nontag) . " are not tags of $from"); -}; - -sub export_tags { - my $from = shift; - $from->$push_tags('EXPORT', @_); -} - -sub export_ok_tags { - my $from = shift; - $from->$push_tags('EXPORT_OK', @_); -} - -1; - -__END__ - -=pod - -=encoding UTF-8 - -=head1 NAME - -Importer::Exporter - DO NOT USE THIS - -=head1 DESCRIPTION - -This is what L<Exporter> would look like if it used L<Importer> to get the job -done. Works with C<use base 'Importer::Exporter';> as well as -C<use Importer::Exporter qw/import/>. - -=head1 *** EXPERIMENTAL *** - -This module is still experimental. Anything can change at any time. Testing is -currently VERY insufficient. - -=head1 SOURCE - -The source code repository for symbol can be found at -F<http://github.com/exodist/Importer>. - -=head1 MAINTAINERS - -=over 4 - -=item Chad Granum E<lt>exodist@cpan.orgE<gt> - -=back - -=head1 AUTHORS - -=over 4 - -=item Chad Granum E<lt>exodist@cpan.orgE<gt> - -=back - -=head1 COPYRIGHT - -Copyright 2015 Chad Granum E<lt>exodist7@gmail.comE<gt>. - -This program is free software; you can redistribute it and/or -modify it under the same terms as Perl itself. - -See F<http://dev.perl.org/licenses/> - -=cut diff --git a/lib/Importer/Exporter/Heavy.pm b/lib/Importer/Exporter/Heavy.pm deleted file mode 100644 index 1977ccf..0000000 --- a/lib/Importer/Exporter/Heavy.pm +++ /dev/null @@ -1,68 +0,0 @@ -package Importer::Exporter::Heavy; -use warnings; - -use Importer::Exporter(); - -*{"heavy_$_"} = Importer::Exporter->can($_) for qw{ - export_fail - export - export_to_level - require_version - export_tags - export_ok_tags -}; - - -1; - -__END__ - -=pod - -=encoding UTF-8 - -=head1 NAME - -Importer::Exporter::Heavy - DO NOT USE THIS - -=head1 DESCRIPTION - -This is what L<Exporter::Heavy> would look like if it used L<Importer> to get -the job done. - -=head1 *** EXPERIMENTAL *** - -This module is still experimental. Anything can change at any time. Testing is -currently VERY insufficient. - -=head1 SOURCE - -The source code repository for symbol can be found at -F<http://github.com/exodist/Importer>. - -=head1 MAINTAINERS - -=over 4 - -=item Chad Granum E<lt>exodist@cpan.orgE<gt> - -=back - -=head1 AUTHORS - -=over 4 - -=item Chad Granum E<lt>exodist@cpan.orgE<gt> - -=back - -=head1 COPYRIGHT - -Copyright 2015 Chad Granum E<lt>exodist7@gmail.comE<gt>. - -This program is free software; you can redistribute it and/or -modify it under the same terms as Perl itself. - -See F<http://dev.perl.org/licenses/> - -=cut @@ -922,26 +922,4 @@ subtest _optimal_import => sub { ok(!FDestD->can('foo'), "Removed 'foo'"); }; -subtest exporter_import => sub { - BEGIN { - $INC{'Fake/Exporter.pm'} = 1; - package Fake::Exporter; - use Importer Importer => 'exporter_import' => { -as => 'import' }; - our @EXPORT = qw/foo $ZAP/; - sub foo { 'foo' } - our $ZAP = 1; - } - - { - package Fake::XXX::A; - use Fake::Exporter qw/foo/; - - package Fake::XXX::B; - use Fake::Exporter qw/foo $ZAP/; - } - - can_ok('Fake::XXX::A', 'foo'); # Optimal case - can_ok('Fake::XXX::B', 'foo'); # Non-optimal -}; - done_testing; |