f4bed0674a195323578de99197b6438e6f95f18a
[p5sagit/Sub-Exporter-Progressive.git] / lib / Sub / Exporter / Progressive.pm
1 package Sub::Exporter::Progressive;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.001007';
7
8 use Carp 'croak';
9 use List::Util 'first';
10
11 sub import {
12    my ($self, @args) = @_;
13
14    my $inner_target = caller(0);
15    my $export_data = sub_export_options($inner_target, @args);
16
17    my $full_exporter;
18    no strict;
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 {
23       use strict;
24       my ($self, @args) = @_;
25
26       if (first { ref || !m/ \A [:-]? \w+ \z /xm } @args) {
27          croak '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          s/ \A - /:/xm for @args;
36          @_ = ($self, @args);
37          goto \&Exporter::import;
38       }
39    };
40 }
41
42 my $too_complicated = <<'DEATH';
43 You are using Sub::Exporter::Progressive, but the features your program uses from
44 Sub::Exporter cannot be implemented without Sub::Exporter, so you might as well
45 just use vanilla Sub::Exporter
46 DEATH
47
48 sub sub_export_options {
49    my ($inner_target, $setup, $options) = @_;
50
51    my @exports;
52    my @defaults;
53    my %tags;
54
55    if ($setup eq '-setup') {
56       my %options = %$options;
57
58       OPTIONS:
59       for my $opt (keys %options) {
60          if ($opt eq 'exports') {
61
62             croak $too_complicated if ref $options{exports} ne 'ARRAY';
63             @exports = @{$options{exports}};
64             croak $too_complicated if first { ref } @exports;
65
66          } elsif ($opt eq 'groups') {
67             %tags = %{$options{groups}};
68             for my $tagset (values %tags) {
69                croak $too_complicated if first { /^-(?!all\b)/ || ref } @{$tagset};
70             }
71             @defaults = @{$tags{default} || [] };
72          } else {
73             croak $too_complicated;
74          }
75       }
76       @{$_} = map { / \A  [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags;
77       $tags{all} ||= [ @exports ];
78       my @errors = grep { my $default = $_; !grep { $default eq $_ } @exports } @defaults;
79       die join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors;
80    }
81
82    return {
83       exports => \@exports,
84       defaults => \@defaults,
85       original => $options,
86       tags => \%tags,
87    };
88 }
89
90 1;
91
92 =encoding utf8
93
94 =head1 NAME
95
96 Sub::Exporter::Progressive - Only use Sub::Exporter if you need it
97
98 =head1 SYNOPSIS
99
100  package Syntax::Keyword::Gather;
101
102  use Sub::Exporter::Progressive -setup => {
103    exports => [qw( break gather gathered take )],
104    groups => {
105      defaults => [qw( break gather gathered take )],
106    },
107  };
108
109  # elsewhere
110
111  # uses Exporter for speed
112  use Syntax::Keyword::Gather;
113
114  # somewhere else
115
116  # uses Sub::Exporter for features
117  use Syntax::Keyword::Gather 'gather', take => { -as => 'grab' };
118
119 =head1 DESCRIPTION
120
121 L<Sub::Exporter> is an incredibly powerful module, but with that power comes
122 great responsibility, er- as well as some runtime penalties.  This module
123 is a C<Sub::Exporter> wrapper that will let your users just use L<Exporter>
124 if all they are doing is picking exports, but use C<Sub::Exporter> if your
125 users try to use C<Sub::Exporter>'s more advanced features features, like
126 renaming exports, if they try to use them.
127
128 Note that this module will export C<@EXPORT>, C<@EXPORT_OK> and
129 C<%EXPORT_TAGS> package variables for C<Exporter> to work.  Additionally, if
130 your package uses advanced C<Sub::Exporter> features like currying, this module
131 will only ever use C<Sub::Exporter>, so you might as well use it directly.
132
133 =head1 AUTHOR
134
135 frew - Arthur Axel Schmidt (cpan:FREW) <frioux+cpan@gmail.com>
136
137 =head1 CONTRIBUTORS
138
139 ilmari - Dagfinn Ilmari MannsÃ¥ker (cpan:ILMARI) <ilmari@ilmari.org>
140
141 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
142
143 leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org>
144
145 =head1 COPYRIGHT
146
147 Copyright (c) 2012 the Sub::Exporter::Progressive L</AUTHOR> and
148 L</CONTRIBUTORS> as listed above.
149
150 =head1 LICENSE
151
152 This library is free software and may be distributed under the same terms
153 as perl itself.
154
155 =cut