1 package Sub::Exporter::Progressive;
6 our $VERSION = '0.001007';
9 use List::Util 'first';
12 my ($self, @args) = @_;
14 my $inner_target = caller(0);
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/^:?\w+$/ } @args) {
27 die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed'
28 unless eval { require Sub::Exporter };
30 Sub::Exporter::build_exporter($export_data->{original});
35 goto \&Exporter::import;
40 my $too_complicated = <<'DEATH';
41 You are using Sub::Exporter::Progressive, but the features your program uses from
42 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
43 just use vanilla Sub::Exporter
46 sub sub_export_options {
47 my ($inner_target, $setup, $options) = @_;
53 if ($setup eq '-setup') {
54 my %options = %$options;
57 for my $opt (keys %options) {
58 if ($opt eq 'exports') {
60 croak $too_complicated if ref $options{exports} ne 'ARRAY';
61 @exports = @{$options{exports}};
62 croak $too_complicated if first { ref } @exports;
64 } elsif ($opt eq 'groups') {
65 %tags = %{$options{groups}};
66 for my $tagset (values %tags) {
67 croak $too_complicated if first { /^-(?!all\b)/ || ref } @{$tagset};
69 @defaults = @{$tags{default} || [] };
71 croak $too_complicated;
74 @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags;
75 $tags{all} ||= [ @exports ];
76 my @errors = grep { my $default = $_; !grep { $default eq $_ } @exports } @defaults;
77 die join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors;
82 defaults => \@defaults,
94 Sub::Exporter::Progressive - Only use Sub::Exporter if you need it
98 package Syntax::Keyword::Gather;
100 use Sub::Exporter::Progressive -setup => {
101 exports => [qw( break gather gathered take )],
103 defaults => [qw( break gather gathered take )],
109 # uses Exporter for speed
110 use Syntax::Keyword::Gather;
114 # uses Sub::Exporter for features
115 use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
119 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
120 great responsibility, er- as well as some runtime penalties. This module
121 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
122 if all they are doing is picking exports, but use C<Sub::Exporter> if your
123 users try to use C<Sub::Exporter>'s more advanced features features, like
124 renaming exports, if they try to use them.
126 Note that this module will export C<@EXPORT>, C<@EXPORT_OK> and
127 C<%EXPORT_TAGS> package variables for C<Exporter> to work. Additionally, if
128 your package uses advanced C<Sub::Exporter> features like currying, this module
129 will only ever use C<Sub::Exporter>, so you might as well use it directly.
133 frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com>
137 ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org>
139 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
141 leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org>
145 Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and
146 L</CONTRIBUTORS> as listed above.
150 This library is free software and may be distributed under the same terms