1 package Sub::Exporter::Progressive;
6 our $VERSION = '0.001006';
8 use List::Util 'first';
11 my ($self, @args) = @_;
13 my $inner_target = caller(0);
14 my ($TOO_COMPLICATED, $export_data) = sub_export_options($inner_target, @args);
16 die <<'DEATH' if $TOO_COMPLICATED;
17 You are using Sub::Exporter::Progressive, but the features your program uses from
18 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
19 just use vanilla Sub::Exporter
24 @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}};
25 @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}};
26 %{"${inner_target}::EXPORT_TAGS"} = %{$export_data->{tags}};
27 *{"${inner_target}::import"} = sub {
29 my ($self, @args) = @_;
31 if (first { ref || !m/^:?\w+$/ } @args) {
32 die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed'
33 unless eval { require Sub::Exporter };
35 Sub::Exporter::build_exporter($export_data->{original});
40 goto \&Exporter::import;
45 sub sub_export_options {
46 my ($inner_target, $setup, $options) = @_;
48 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') {
70 %tags = %{$options{groups}};
71 for my $tagset (values %tags) {
72 $TOO_COMPLICATED = 1 if first { /^-(?!all\b)/ || ref } @{$tagset};
74 @defaults = @{$tags{default} || [] };
80 @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags;
81 $tags{all} ||= [ @exports ];
82 my @errors = grep { my $default = $_; !grep { $default eq $_ } @exports } @defaults;
83 die join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors;
86 return $TOO_COMPLICATED, {
88 defaults => \@defaults,
100 Sub::Exporter::Progressive - Only use Sub::Exporter if you need it
104 package Syntax::Keyword::Gather;
106 use Sub::Exporter::Progressive -setup => {
107 exports => [qw( break gather gathered take )],
109 defaults => [qw( break gather gathered take )],
115 # uses Exporter for speed
116 use Syntax::Keyword::Gather;
120 # uses Sub::Exporter for features
121 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
125 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
126 great responsibility, er- as well as some runtime penalties. This module
127 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
128 if all they are doing is picking exports, but use C<Sub::Exporter> if your
129 users try to use C<Sub::Exporter>'s more advanced features features, like
130 renaming exports, if they try to use them.
132 Note that this module will export C<@EXPORT>, C<@EXPORT_OK> and
133 C<%EXPORT_TAGS> package variables for C<Exporter> to work. Additionally, if
134 your package uses advanced C<Sub::Exporter> features like currying, this module
135 will only ever use C<Sub::Exporter>, so you might as well use it directly.
139 frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com>
143 ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
145 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
147 leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org>
151 Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and
152 L</CONTRIBUTORS> as listed above.
156 This library is free software and may be distributed under the same terms