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 die <<'DEATH' if $TOO_COMPLICATED;
19 You are using Sub::Exporter::Progressive, but the features your program uses from
20 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
21 just use vanilla Sub::Exporter
26 @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}};
27 @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}};
28 *{"${inner_target}::import"} = sub {
30 my ($self, @args) = @_;
32 if (first { ref || !m/^\w+$/ } @args) {
33 die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed'
34 unless eval { require Sub::Exporter };
36 Sub::Exporter::build_exporter($export_data->{original});
41 goto \&Exporter::import;
46 sub sub_export_options {
47 my ($setup, $options) = @_;
49 my $TOO_COMPLICATED = 0;
54 if ($setup eq '-setup') {
55 my %options = %$options;
58 for my $opt (keys %options) {
59 if ($opt eq 'exports') {
61 $TOO_COMPLICATED = 1, last OPTIONS
62 if ref $options{exports} ne 'ARRAY';
64 @exports = @{$options{exports}};
66 $TOO_COMPLICATED = 1, last OPTIONS
67 if first { ref } @exports;
69 } elsif ($opt eq 'groups') {
71 $TOO_COMPLICATED = 1, last OPTIONS
72 if first { $_ ne 'default' } keys %{$options{groups}};
74 @defaults = @{$options{groups}{default} || [] };
80 @defaults = @exports if $defaults[0] eq '-all';
83 return $TOO_COMPLICATED, {
85 defaults => \@defaults,
94 package Syntax::Keyword::Gather;
96 use Sub::Exporter::Progressive -setup => {
97 exports => [qw( break gather gathered take )],
99 defaults => [qw( break gather gathered take )],
105 # uses Exporter for speed
106 use Syntax::Keyword::Gather;
110 # uses Sub::Exporter for features
111 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
115 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
116 great responsibility, er- as well as some runtime penalties. This module
117 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
118 if all they are doing is picking exports, but use C<Sub::Exporter> if your
119 users try to use C<Sub::Exporter>'s more advanced features features, like
120 renaming exports, if they try to use them.
122 Note that this module will export C<@EXPORT> and C<@EXPORT_OK> package
123 variables for C<Exporter> to work. Additionally, if your package uses advanced
124 C<Sub::Exporter> features like currying, this module will only ever use
125 C<Sub::Exporter>, so you might as well use it directly.