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 die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed'
38 unless eval { require Sub::Exporter };
40 Sub::Exporter::build_exporter($export_data->{original});
45 goto \&Exporter::import;
51 sub sub_export_options {
52 my ($setup, $options) = @_;
54 my $TOO_COMPLICATED = 0;
59 if ($setup eq '-setup') {
60 my %options = %$options;
63 for my $opt (keys %options) {
64 if ($opt eq 'exports') {
66 $TOO_COMPLICATED = 1, last OPTIONS
67 if ref $options{exports} ne 'ARRAY';
69 @exports = @{$options{exports}};
71 $TOO_COMPLICATED = 1, last OPTIONS
72 if first { ref } @exports;
74 } elsif ($opt eq 'groups') {
76 $TOO_COMPLICATED = 1, last OPTIONS
77 if first { $_ ne 'default' } keys %{$options{groups}};
79 @defaults = @{$options{groups}{default} || [] };
85 @defaults = @exports if $defaults[0] eq '-all';
88 return $TOO_COMPLICATED, {
90 defaults => \@defaults,
99 package Syntax::Keyword::Gather;
101 use Sub::Exporter::Progressive -setup => {
102 exports => [qw( break gather gathered take )],
104 defaults => [qw( break gather gathered take )],
110 # uses Exporter for speed
111 use Syntax::Keyword::Gather;
115 # uses Sub::Exporter for features
116 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
120 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
121 great responsibility, er- as well as some runtime penalties. This module
122 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
123 if all they are doing is picking exports, but use C<Sub::Exporter> if your
124 users try to use C<Sub::Exporter>'s more advanced features features, like
125 renaming exports, if they try to use them.
127 Note that this module will export C<@EXPORT> and C<@EXPORT_OK> package
128 variables for C<Exporter> to work. Additionally, if your package uses advanced
129 C<Sub::Exporter> features like currying, this module will only ever use
130 C<Sub::Exporter>, so you might as well use it directly.