1 package Sub::Exporter::Progressive;
6 our $VERSION = '0.001010';
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 (List::Util::first { ref || !m/ \A [:-]? \w+ \z /xm } @args) {
27 Carp::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});
32 } elsif (defined(my $num = List::Util::first { !ref and m/^\d/ } @args)) {
33 die "cannot export symbols with a leading digit: '$num'";
36 s/ \A - /:/xm for @args;
38 goto \&Exporter::import;
44 my $too_complicated = <<'DEATH';
45 You are using Sub::Exporter::Progressive, but the features your program uses from
46 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
47 just use vanilla Sub::Exporter
50 sub sub_export_options {
51 my ($inner_target, $setup, $options) = @_;
57 if ($setup eq '-setup') {
58 my %options = %$options;
61 for my $opt (keys %options) {
62 if ($opt eq 'exports') {
64 Carp::croak $too_complicated if ref $options{exports} ne 'ARRAY';
65 @exports = @{$options{exports}};
66 Carp::croak $too_complicated if List::Util::first { ref } @exports;
68 } elsif ($opt eq 'groups') {
69 %tags = %{$options{groups}};
70 for my $tagset (values %tags) {
71 Carp::croak $too_complicated if List::Util::first { / \A - (?! all \b ) /x || ref } @{$tagset};
73 @defaults = @{$tags{default} || [] };
75 Carp::croak $too_complicated;
78 @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags;
79 $tags{all} ||= [ @exports ];
80 my %exports = map { $_ => 1 } @exports;
81 my @errors = grep { not $exports{$_} } @defaults;
82 Carp::croak join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors;
87 defaults => \@defaults,
99 Sub::Exporter::Progressive - Only use Sub::Exporter if you need it
103 package Syntax::Keyword::Gather;
105 use Sub::Exporter::Progressive -setup => {
106 exports => [qw( break gather gathered take )],
108 default => [qw( break gather gathered take )],
114 # uses Exporter for speed
115 use Syntax::Keyword::Gather;
119 # uses Sub::Exporter for features
120 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
124 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
125 great responsibility, er- as well as some runtime penalties. This module
126 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
127 if all they are doing is picking exports, but use C<Sub::Exporter> if your
128 users try to use C<Sub::Exporter>'s more advanced features, like
129 renaming exports, if they try to use them.
131 Note that this module will export C<@EXPORT>, C<@EXPORT_OK> and
132 C<%EXPORT_TAGS> package variables for C<Exporter> to work. Additionally, if
133 your package uses advanced C<Sub::Exporter> features like currying, this module
134 will only ever use C<Sub::Exporter>, so you might as well use it directly.
138 frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com>
142 ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
144 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
146 leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org>
150 Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and
151 L</CONTRIBUTORS> as listed above.
155 This library is free software and may be distributed under the same terms