| 1 | package Sub::Exporter::Progressive; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 6 | our $VERSION = '0.001011'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 6 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use List::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | sub import { | ||||||||||||||||||||
| 12 | 7 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 7 | my $inner_target = caller; | |||||||||||||||||||
| 15 | 7 | my $export_data = sub_export_options($inner_target, @args); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 7 | my $full_exporter; | |||||||||||||||||||
| 18 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 7 | @{"${inner_target}::EXPORT_OK"} = @{$export_data->{exports}}; | |||||||||||||||||||
| 20 | 7 | @{"${inner_target}::EXPORT"} = @{$export_data->{defaults}}; | |||||||||||||||||||
| 21 | 7 | %{"${inner_target}::EXPORT_TAGS"} = %{$export_data->{tags}}; | |||||||||||||||||||
| 22 | 7 | *{"${inner_target}::import"} = sub { | |||||||||||||||||||
| 23 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 18 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 18 | ▶ | if (List::Util::first { ref || !m/ \A [:-]? \w+ \z /xm } @args) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 3 branches
| |||||||||||||||||||||
| 27 | Carp::croak 'your usage of Sub::Exporter::Progressive requires Sub::Exporter to be installed' | ||||||||||||||||||||
| 28 | 1 | ▶ | unless eval { require Sub::Exporter }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 1 | ▶ | $full_exporter ||= Sub::Exporter::build_exporter($export_data->{original}); | ||||||||||||||||||
|
Condition33%1/3
$full_exporter ||= Sub::Exporter::build_exporter($export_data->{"original"})
MC/DC0%0/2
$full_exporter ||= Sub::Exporter::build_exporter($export_data->{"original"})
$full_exporter
Sub::Exporter::build_exporter($export_data->{"original"})
Truth table33%1/3
A ||= B
A= $full_exporterB= Sub::Exporter::build_exporter($export_data->{"original"})
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | goto $full_exporter; | |||||||||||||||||||
| 32 | 19 | ▶ | } elsif (defined(my $num = List::Util::first { !ref and m/^\d/ } @args)) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 1 | die "cannot export symbols with a leading digit: '$num'"; | |||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||
| 35 | 16 | require Exporter; | |||||||||||||||||||
| 36 | 16 | s/ \A - /:/xm for @args; | |||||||||||||||||||
| 37 | 16 | @_ = ($self, @args); | |||||||||||||||||||
| 38 | 16 | goto \&Exporter::import; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | 7 | }; | |||||||||||||||||||
| 41 | 7 | return; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 6 | 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 | |||||||||||||||||||||
| 50 | sub sub_export_options { | ||||||||||||||||||||
| 51 | 7 | ▶ | my ($inner_target, $setup, $options) = @_; | ||||||||||||||||||
|
Subroutine
sub_export_options: called CC=14 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 7 | my @exports; | |||||||||||||||||||
| 54 | my @defaults; | ||||||||||||||||||||
| 55 | 7 | my %tags; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 7 | ▶ | if ($setup eq '-setup') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 7 | my %options = %$options; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | OPTIONS: | ||||||||||||||||||||
| 61 | 7 | for my $opt (keys %options) { | |||||||||||||||||||
| 62 | 13 | ▶ | if ($opt eq 'exports') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 7 | ▶ | Carp::croak $too_complicated if ref $options{exports} ne 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 7 | @exports = @{$options{exports}}; | |||||||||||||||||||
| 66 | 7 | ▶ | Carp::croak $too_complicated if List::Util::first { ref } @exports; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | } elsif ($opt eq 'groups') { | ||||||||||||||||||||
| 69 | 6 | %tags = %{$options{groups}}; | |||||||||||||||||||
| 70 | 6 | for my $tagset (values %tags) { | |||||||||||||||||||
| 71 | 11 | ▶ | Carp::croak $too_complicated if List::Util::first { / \A - (?! all \b ) /x || ref } @{$tagset}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
Branch: 2 branches
| |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 6 | ▶ | @defaults = @{$tags{default} || [] }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||
| 75 | 0 | Carp::croak $too_complicated; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 7 | ▶ | @{$_} = map { / \A [:-] all \z /x ? @exports : $_ } @{$_} for \@defaults, values %tags; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 7 | ▶ | $tags{all} ||= [ @exports ]; | ||||||||||||||||||
|
Condition50%1/2
$tags{"all"} ||= [@exports]
MC/DC0%0/1
$tags{"all"} ||= [@exports]
$tags{"all"}
Truth table50%1/2
A
A= $tags{"all"}
| |||||||||||||||||||||
| 80 | 7 | my %exports = map { $_ => 1 } @exports; | |||||||||||||||||||
| 81 | 7 | my @errors = grep { not $exports{$_} } @defaults; | |||||||||||||||||||
| 82 | 7 | ▶ | Carp::croak join(', ', @errors) . " is not exported by the $inner_target module\n" if @errors; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | return { | ||||||||||||||||||||
| 86 | 7 | exports => \@exports, | |||||||||||||||||||
| 87 | defaults => \@defaults, | ||||||||||||||||||||
| 88 | original => $options, | ||||||||||||||||||||
| 89 | tags => \%tags, | ||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 6 | 1; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =encoding utf8 | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head1 NAME | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | Sub::Exporter::Progressive - Only use Sub::Exporter if you need it | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 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 | default => [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, like | ||||||||||||||||||||
| 129 | renaming exports, if they try to use them. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 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. | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 146 | leont - Leon Timmermans (cpan:LEONT) <leont@cpan.org> | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 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 | ||||||||||||||||||||