Commit | Line | Data |
6d823d74 |
1 | package Sub::Exporter::Progressive; |
2 | |
6d823d74 |
3 | use strict; |
4 | use warnings; |
5 | |
439b5e83 |
6 | our $VERSION = '0.001005'; |
18f84e12 |
7 | |
6d823d74 |
8 | use List::Util 'first'; |
9 | |
10 | sub import { |
11 | my ($self, @args) = @_; |
12 | |
13 | my $inner_target = caller(0); |
bb7b321b |
14 | my ($TOO_COMPLICATED, $export_data) = sub_export_options($inner_target, @args); |
6d823d74 |
15 | |
113a844b |
16 | die <<'DEATH' if $TOO_COMPLICATED; |
6d823d74 |
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 |
113a844b |
20 | DEATH |
21 | |
22 | my $full_exporter; |
23 | no strict; |
24 | @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}}; |
25 | @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}}; |
47825557 |
26 | %{"${inner_target}::EXPORT_TAGS"} = %{$export_data->{tags}}; |
113a844b |
27 | *{"${inner_target}::import"} = sub { |
28 | use strict; |
29 | my ($self, @args) = @_; |
30 | |
47825557 |
31 | if (first { ref || !m/^:?\w+$/ } @args) { |
113a844b |
32 | die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed' |
33 | unless eval { require Sub::Exporter }; |
34 | $full_exporter ||= |
35 | Sub::Exporter::build_exporter($export_data->{original}); |
36 | |
37 | goto $full_exporter; |
38 | } else { |
39 | require Exporter; |
40 | goto \&Exporter::import; |
41 | } |
42 | }; |
6d823d74 |
43 | } |
44 | |
45 | sub sub_export_options { |
bb7b321b |
46 | my ($inner_target, $setup, $options) = @_; |
6d823d74 |
47 | |
48 | my $TOO_COMPLICATED = 0; |
49 | |
50 | my @exports; |
51 | my @defaults; |
47825557 |
52 | my %tags; |
6d823d74 |
53 | |
54 | if ($setup eq '-setup') { |
55 | my %options = %$options; |
56 | |
57 | OPTIONS: |
58 | for my $opt (keys %options) { |
59 | if ($opt eq 'exports') { |
60 | |
61 | $TOO_COMPLICATED = 1, last OPTIONS |
62 | if ref $options{exports} ne 'ARRAY'; |
63 | |
64 | @exports = @{$options{exports}}; |
65 | |
66 | $TOO_COMPLICATED = 1, last OPTIONS |
67 | if first { ref } @exports; |
68 | |
69 | } elsif ($opt eq 'groups') { |
47825557 |
70 | %tags = %{$options{groups}}; |
71 | for my $tagset (values %tags) { |
72 | $TOO_COMPLICATED = 1 if first { /^-(?!all\b)/ || ref } @{$tagset}; |
73 | } |
74 | @defaults = @{$tags{default} || [] }; |
6d823d74 |
75 | } else { |
76 | $TOO_COMPLICATED = 1; |
77 | last OPTIONS |
78 | } |
79 | } |
47825557 |
80 | @{$_} = map { $_ eq '-all' ? @exports : $_ } @{$_} for \@defaults, values %tags; |
bb7b321b |
81 | my @errors = grep { my $default = $_; !grep { $default eq $_ } @exports } @defaults; |
82 | die join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors; |
6d823d74 |
83 | } |
84 | |
85 | return $TOO_COMPLICATED, { |
86 | exports => \@exports, |
87 | defaults => \@defaults, |
88 | original => $options, |
47825557 |
89 | tags => \%tags, |
6d823d74 |
90 | } |
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 => { |
108 | defaults => [qw( break gather gathered take )], |
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 |
128 | users try to use C<Sub::Exporter>'s more advanced features features, like |
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 |