1 package Sub::Exporter::Progressive;
3 # ABSTRACT: Only use Sub::Exporter if you need it
10 use List::Util 'first';
13 my ($self, @args) = @_;
15 my $inner_target = caller(0);
16 my ($TOO_COMPLICATED, $export_data) = sub_export_options(@args);
18 if ($TOO_COMPLICATED) {
20 You are using Sub::Exporter::Progressive, but the features your program uses from
21 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
22 just use vanilla Sub::Exporter
24 require Sub::Exporter;
25 goto \&Sub::Exporter::import;
30 @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}};
31 @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}};
32 *{"${inner_target}::import"} = sub {
34 my ($self, @args) = @_;
36 if (first { ref || !m/^\w+$/ } @args) {
37 require Sub::Exporter;
39 Sub::Exporter::build_exporter($export_data->{original});
44 goto \&Exporter::import;
50 sub sub_export_options {
51 my ($setup, $options) = @_;
53 my $TOO_COMPLICATED = 0;
58 if ($setup eq '-setup') {
59 my %options = %$options;
62 for my $opt (keys %options) {
63 if ($opt eq 'exports') {
65 $TOO_COMPLICATED = 1, last OPTIONS
66 if ref $options{exports} ne 'ARRAY';
68 @exports = @{$options{exports}};
70 $TOO_COMPLICATED = 1, last OPTIONS
71 if first { ref } @exports;
73 } elsif ($opt eq 'groups') {
75 $TOO_COMPLICATED = 1, last OPTIONS
76 if first { $_ ne 'default' } keys %{$options{groups}};
78 @defaults = @{$options{groups}{default} || [] };
84 @defaults = @exports if $defaults[0] eq '-all';
87 return $TOO_COMPLICATED, {
89 defaults => \@defaults,
98 package Syntax::Keyword::Gather;
100 use Sub::Exporter::Progressive -setup => {
101 exports => [qw( break gather gathered take )],
103 defaults => [qw( break gather gathered take )],
109 # uses Exporter for speed
110 use Syntax::Keyword::Gather;
114 # uses Sub::Exporter for features
115 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
119 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
120 great responsibility, er- as well as some runtime penalties. This module
121 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
122 if all they are doing is picking exports, but use C<Sub::Exporter> if your
123 users try to use C<Sub::Exporter>'s more advanced features features, like
124 renaming exports, if they try to use them.
126 Note that this module will export C<@EXPORT> and C<@EXPORT_OK> package
127 variables for C<Exporter> to work. Additionally, if your package uses advanced
128 C<Sub::Exporter> features like currying, this module will only ever use
129 C<Sub::Exporter>, so you might as well use it directly.