Commit | Line | Data |
6d823d74 |
1 | package Sub::Exporter::Progressive; |
2 | |
6d823d74 |
3 | use strict; |
4 | use warnings; |
5 | |
fa713f7a |
6 | our $VERSION = '0.001007'; |
18f84e12 |
7 | |
2b3cd6e0 |
8 | use Carp 'croak'; |
6d823d74 |
9 | use List::Util 'first'; |
10 | |
11 | sub import { |
12 | my ($self, @args) = @_; |
13 | |
14 | my $inner_target = caller(0); |
2b3cd6e0 |
15 | my $export_data = sub_export_options($inner_target, @args); |
113a844b |
16 | |
17 | my $full_exporter; |
18 | no strict; |
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 | |
47825557 |
26 | if (first { ref || !m/^:?\w+$/ } @args) { |
113a844b |
27 | die 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed' |
28 | unless eval { require Sub::Exporter }; |
29 | $full_exporter ||= |
30 | Sub::Exporter::build_exporter($export_data->{original}); |
31 | |
32 | goto $full_exporter; |
33 | } else { |
34 | require Exporter; |
35 | goto \&Exporter::import; |
36 | } |
37 | }; |
6d823d74 |
38 | } |
39 | |
2b3cd6e0 |
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 |
44 | DEATH |
45 | |
6d823d74 |
46 | sub sub_export_options { |
bb7b321b |
47 | my ($inner_target, $setup, $options) = @_; |
6d823d74 |
48 | |
6d823d74 |
49 | my @exports; |
50 | my @defaults; |
47825557 |
51 | my %tags; |
6d823d74 |
52 | |
53 | if ($setup eq '-setup') { |
54 | my %options = %$options; |
55 | |
56 | OPTIONS: |
57 | for my $opt (keys %options) { |
58 | if ($opt eq 'exports') { |
59 | |
2b3cd6e0 |
60 | croak $too_complicated if ref $options{exports} ne 'ARRAY'; |
6d823d74 |
61 | @exports = @{$options{exports}}; |
2b3cd6e0 |
62 | croak $too_complicated if first { ref } @exports; |
6d823d74 |
63 | |
64 | } elsif ($opt eq 'groups') { |
47825557 |
65 | %tags = %{$options{groups}}; |
66 | for my $tagset (values %tags) { |
2b3cd6e0 |
67 | croak $too_complicated if first { /^-(?!all\b)/ || ref } @{$tagset}; |
47825557 |
68 | } |
69 | @defaults = @{$tags{default} || [] }; |
6d823d74 |
70 | } else { |
2b3cd6e0 |
71 | croak $too_complicated; |
6d823d74 |
72 | } |
73 | } |
707baf12 |
74 | @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags; |
75 | $tags{all} ||= [ @exports ]; |
bb7b321b |
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; |
6d823d74 |
78 | } |
79 | |
2b3cd6e0 |
80 | return { |
6d823d74 |
81 | exports => \@exports, |
82 | defaults => \@defaults, |
83 | original => $options, |
47825557 |
84 | tags => \%tags, |
2b3cd6e0 |
85 | }; |
6d823d74 |
86 | } |
87 | |
88 | 1; |
89 | |
62a442f8 |
90 | =encoding utf8 |
91 | |
5cf6a81d |
92 | =head1 NAME |
93 | |
94 | Sub::Exporter::Progressive - Only use Sub::Exporter if you need it |
95 | |
6d823d74 |
96 | =head1 SYNOPSIS |
97 | |
98 | package Syntax::Keyword::Gather; |
99 | |
100 | use Sub::Exporter::Progressive -setup => { |
101 | exports => [qw( break gather gathered take )], |
102 | groups => { |
103 | defaults => [qw( break gather gathered take )], |
104 | }, |
105 | }; |
106 | |
107 | # elsewhere |
108 | |
109 | # uses Exporter for speed |
110 | use Syntax::Keyword::Gather; |
111 | |
112 | # somewhere else |
113 | |
114 | # uses Sub::Exporter for features |
115 | use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' }; |
116 | |
117 | =head1 DESCRIPTION |
118 | |
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. |
125 | |
47825557 |
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. |
5cf6a81d |
130 | |
131 | =head1 AUTHOR |
132 | |
133 | frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com> |
134 | |
135 | =head1 CONTRIBUTORS |
136 | |
137 | ilmari - Dagfinn Ilmari Mannsåker (cpan:ILMARI) <ilmari@ilmari.org> |
138 | |
139 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
140 | |
4afbf82c |
141 | leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org> |
142 | |
5cf6a81d |
143 | =head1 COPYRIGHT |
144 | |
145 | Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and |
146 | L</CONTRIBUTORS> as listed above. |
147 | |
148 | =head1 LICENSE |
149 | |
150 | This library is free software and may be distributed under the same terms |
151 | as perl itself. |
152 | |
153 | =cut |