1 package Sub::Exporter::Progressive;
6 our $VERSION = '0.001007';
9 use List::Util 'first';
12 my ($self, @args) = @_;
14 my $inner_target = caller;
15 my $export_data = sub_export_options($inner_target, @args);
19 @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}};
20 @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}};
21 %{"${inner_target}::EXPORT_TAGS"} = %{$export_data->{tags}};
22 *{"${inner_target}::import"} = sub {
24 my ($self, @args) = @_;
26 if (first { ref || !m/ \A [:-]? \w+ \z /xm } @args) {
27 croak 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed'
28 unless eval { require Sub::Exporter };
29 $full_exporter ||= Sub::Exporter::build_exporter($export_data->{original});
34 s/ \A - /:/xm for @args;
36 goto \&Exporter::import;
42 my $too_complicated = <<'DEATH';
43 You are using Sub::Exporter::Progressive, but the features your program uses from
44 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
45 just use vanilla Sub::Exporter
48 sub sub_export_options {
49 my ($inner_target, $setup, $options) = @_;
55 if ($setup eq '-setup') {
56 my %options = %$options;
59 for my $opt (keys %options) {
60 if ($opt eq 'exports') {
62 croak $too_complicated if ref $options{exports} ne 'ARRAY';
63 @exports = @{$options{exports}};
64 croak $too_complicated if first { ref } @exports;
66 } elsif ($opt eq 'groups') {
67 %tags = %{$options{groups}};
68 for my $tagset (values %tags) {
69 croak $too_complicated if first { / \A - (?! all \b ) /x || ref } @{$tagset};
71 @defaults = @{$tags{default} || [] };
73 croak $too_complicated;
76 @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags;
77 $tags{all} ||= [ @exports ];
78 my %exports = map { $_ => 1 } @exports;
79 my @errors = grep { not $exports{$_} } @defaults;
80 croak join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors;
85 defaults => \@defaults,
97 Sub::Exporter::Progressive - Only use Sub::Exporter if you need it
101 package Syntax::Keyword::Gather;
103 use Sub::Exporter::Progressive -setup => {
104 exports => [qw( break gather gathered take )],
106 defaults => [qw( break gather gathered take )],
112 # uses Exporter for speed
113 use Syntax::Keyword::Gather;
117 # uses Sub::Exporter for features
118 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
122 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
123 great responsibility, er- as well as some runtime penalties. This module
124 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
125 if all they are doing is picking exports, but use C<Sub::Exporter> if your
126 users try to use C<Sub::Exporter>'s more advanced features features, like
127 renaming exports, if they try to use them.
129 Note that this module will export C<@EXPORT>, C<@EXPORT_OK> and
130 C<%EXPORT_TAGS> package variables for C<Exporter> to work. Additionally, if
131 your package uses advanced C<Sub::Exporter> features like currying, this module
132 will only ever use C<Sub::Exporter>, so you might as well use it directly.
136 frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com>
140 ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
142 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
144 leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org>
148 Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and
149 L</CONTRIBUTORS> as listed above.
153 This library is free software and may be distributed under the same terms