Commit | Line | Data |
6d823d74 |
1 | package Sub::Exporter::Progressive; |
2 | |
6d823d74 |
3 | use strict; |
4 | use warnings; |
5 | |
be002636 |
6 | our $VERSION = '0.001010'; |
18f84e12 |
7 | |
5776cb98 |
8 | use Carp (); |
9 | use List::Util (); |
6d823d74 |
10 | |
11 | sub import { |
12 | my ($self, @args) = @_; |
13 | |
ea84f762 |
14 | my $inner_target = caller; |
2b3cd6e0 |
15 | my $export_data = sub_export_options($inner_target, @args); |
113a844b |
16 | |
17 | my $full_exporter; |
ea84f762 |
18 | no strict 'refs'; |
113a844b |
19 | @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}}; |
20 | @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}}; |
47825557 |
21 | %{"${inner_target}::EXPORT_TAGS"} = %{$export_data->{tags}}; |
113a844b |
22 | *{"${inner_target}::import"} = sub { |
23 | use strict; |
24 | my ($self, @args) = @_; |
25 | |
5776cb98 |
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' |
113a844b |
28 | unless eval { require Sub::Exporter }; |
ea84f762 |
29 | $full_exporter ||= Sub::Exporter::build_exporter($export_data->{original}); |
113a844b |
30 | |
31 | goto $full_exporter; |
5776cb98 |
32 | } elsif (defined(my $num = List::Util::first { !ref and m/^\d/ } @args)) { |
d9568f3a |
33 | die "cannot export symbols with a leading digit: '$num'"; |
113a844b |
34 | } else { |
35 | require Exporter; |
b94ac53e |
36 | s/ \A - /:/xm for @args; |
37 | @_ = ($self, @args); |
113a844b |
38 | goto \&Exporter::import; |
39 | } |
40 | }; |
ea84f762 |
41 | return; |
6d823d74 |
42 | } |
43 | |
2b3cd6e0 |
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 |
48 | DEATH |
49 | |
6d823d74 |
50 | sub sub_export_options { |
bb7b321b |
51 | my ($inner_target, $setup, $options) = @_; |
6d823d74 |
52 | |
6d823d74 |
53 | my @exports; |
54 | my @defaults; |
47825557 |
55 | my %tags; |
6d823d74 |
56 | |
57 | if ($setup eq '-setup') { |
58 | my %options = %$options; |
59 | |
60 | OPTIONS: |
61 | for my $opt (keys %options) { |
62 | if ($opt eq 'exports') { |
63 | |
5776cb98 |
64 | Carp::croak $too_complicated if ref $options{exports} ne 'ARRAY'; |
6d823d74 |
65 | @exports = @{$options{exports}}; |
5776cb98 |
66 | Carp::croak $too_complicated if List::Util::first { ref } @exports; |
6d823d74 |
67 | |
68 | } elsif ($opt eq 'groups') { |
47825557 |
69 | %tags = %{$options{groups}}; |
70 | for my $tagset (values %tags) { |
5776cb98 |
71 | Carp::croak $too_complicated if List::Util::first { / \A - (?! all \b ) /x || ref } @{$tagset}; |
47825557 |
72 | } |
73 | @defaults = @{$tags{default} || [] }; |
6d823d74 |
74 | } else { |
5776cb98 |
75 | Carp::croak $too_complicated; |
6d823d74 |
76 | } |
77 | } |
707baf12 |
78 | @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags; |
79 | $tags{all} ||= [ @exports ]; |
ea84f762 |
80 | my %exports = map { $_ => 1 } @exports; |
81 | my @errors = grep { not $exports{$_} } @defaults; |
5776cb98 |
82 | Carp::croak join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors; |
6d823d74 |
83 | } |
84 | |
2b3cd6e0 |
85 | return { |
6d823d74 |
86 | exports => \@exports, |
87 | defaults => \@defaults, |
88 | original => $options, |
47825557 |
89 | tags => \%tags, |
2b3cd6e0 |
90 | }; |
6d823d74 |
91 | } |
92 | |
93 | 1; |
94 | |
62a442f8 |
95 | =encoding utf8 |
96 | |
5cf6a81d |
97 | =head1 NAME |
98 | |
99 | Sub::Exporter::Progressive - Only use Sub::Exporter if you need it |
100 | |
6d823d74 |
101 | =head1 SYNOPSIS |
102 | |
103 | package Syntax::Keyword::Gather; |
104 | |
105 | use Sub::Exporter::Progressive -setup => { |
106 | exports => [qw( break gather gathered take )], |
107 | groups => { |
56621abe |
108 | default => [qw( break gather gathered take )], |
6d823d74 |
109 | }, |
110 | }; |
111 | |
112 | # elsewhere |
113 | |
114 | # uses Exporter for speed |
115 | use Syntax::Keyword::Gather; |
116 | |
117 | # somewhere else |
118 | |
119 | # uses Sub::Exporter for features |
120 | use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' }; |
121 | |
122 | =head1 DESCRIPTION |
123 | |
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 |
0dc049eb |
128 | users try to use C<Sub::Exporter>'s more advanced features, like |
6d823d74 |
129 | renaming exports, if they try to use them. |
130 | |
47825557 |
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. |
5cf6a81d |
135 | |
136 | =head1 AUTHOR |
137 | |
138 | frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com> |
139 | |
140 | =head1 CONTRIBUTORS |
141 | |
142 | ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org> |
143 | |
144 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
145 | |
4afbf82c |
146 | leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org> |
147 | |
5cf6a81d |
148 | =head1 COPYRIGHT |
149 | |
150 | Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and |
151 | L</CONTRIBUTORS> as listed above. |
152 | |
153 | =head1 LICENSE |
154 | |
155 | This library is free software and may be distributed under the same terms |
156 | as perl itself. |
157 | |
158 | =cut |