blib/lib/Exporter/ConditionalSubs.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 77.2% 17 / 22 Conditioncond 60.0% 6 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 86.9% 113 / 130 CC 17 SCAR 23.7
CC
17
Cov
88%
import
18.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.7
↑ Summary
1 package Exporter::ConditionalSubs;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 require Exporter;
7 3 our @ISA = qw( Exporter );
8  
9 #------------------------------------------------------------
10 #
11 # This section lifted directly from Debug::Show v0.0
12 #
13 # https://metacpan.org/pod/Debug::Show
14 #
15 # Thanks to Zefram
16 #
17 3 ▶use B::CallChecker qw( cv_set_call_checker );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 BEGIN {
19 # B::Generate provides a broken version of B::COP->warnings, which
20 # makes B::Deparse barf [rt.cpan.org #70396], and of B::SVOP->sv,
21 # which makes B::Concise emit rubbish [rt.cpan.org #70398].
22 # This works around it by restoring the non-broken versions,
23 # provided that B::Generate hasn't already been loaded. If it
24 # was loaded by someone else, better hope they worked around it
25 # the same way.
26 3 ▶ require B;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 my $cop_warnings = \&B::COP::warnings;
28 3 my $svop_sv = \&B::SVOP::sv;
29 3 require B::Generate;
30 3 ▶ no warnings "redefine";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 *B::COP::warnings = $cop_warnings;
32 3 *B::SVOP::sv = $svop_sv;
33 3 B::Generate->VERSION(1.33);
34 }
35 #------------------------------------------------------------
36  
37 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 #pod =head1 NAME
40 #pod
41 #pod Exporter::ConditionalSubs - Conditionally export subroutines
42 #pod
43 #pod =head1 VERSION
44 #pod
45 #pod Version 1.01
46 #pod
47 #pod =cut
48  
49 3 our $VERSION = '1.01';
50  
51 #pod =head1 SYNOPSIS
52 #pod
53 #pod Allows subroutines to be conditionally exported. If the condition
54 #pod is satisfied, the subroutine will be exported as usual. But if not,
55 #pod the subroutine will be replaced with a stub that gets optimized away
56 #pod by the compiler. When stubbed out, not even the arguments to the
57 #pod subroutine will get evaluated.
58 #pod
59 #pod This allows for e.g. assertion-like behavior, where subroutine calls
60 #pod can be left in the code but effectively ignored under certain conditions.
61 #pod
62 #pod First create a module that C<ISA> L<Exporter::ConditionalSubs>:
63 #pod
64 #pod package My::Assertions;
65 #pod
66 #pod require Exporter::ConditionalSubs;
67 #pod our @ISA = qw( Exporter::ConditionalSubs );
68 #pod
69 #pod our @EXPORT = ();
70 #pod our @EXPORT_OK = qw( _assert_non_empty );
71 #pod
72 #pod sub _assert_non_empty
73 #pod {
74 #pod carp "Found empty value" unless length(shift // '') > 0;
75 #pod }
76 #pod
77 #pod Then, specify an C<-if> or C<-unless> condition when C<use>ing that module:
78 #pod
79 #pod package My::App;
80 #pod
81 #pod use My::Assertions qw( _assert_non_empty ), -if => $ENV{DEBUG};
82 #pod
83 #pod use My::MoreAssertions -unless => $ENV{RUNTIME} eq 'prod';
84 #pod
85 #pod # Coderefs work too:
86 #pod use My::OtherAssertions -if => sub { ... some logic ... };
87 #pod
88 #pod _assert_non_empty($foo); # this subroutine call might be a no-op
89 #pod
90 #pod
91 #pod This is a subclass of L<Exporter> and works just like it, with the
92 #pod addition of support for the C<-if> and C<-unless> import arguments.
93 #pod
94 #pod =head1 SUBROUTINES
95 #pod
96 #pod =head2 import
97 #pod
98 #pod Works like the L<Exporter> C<import()> function, except that it checks
99 #pod for an optional C<-if> or C<-unless> import arg, followed by either
100 #pod a boolean, or a coderef that returns true/false.
101 #pod
102 #pod If the condition evaluates to true for C<-if>, or false for C<-unless>,
103 #pod then any subs are exported as-is. Otherwise, any subs in C<@EXPORT_OK>
104 #pod are replaced with stubs that get optimized away by the compiler.
105 #pod
106 #pod You can specify either C<-if> or C<-unless>, but not both. Croaks if
107 #pod both are specified, or if you specify the same option more than once.
108 #pod
109 #pod =cut
110  
111 sub import
112 {
113 11 ▶ my ($package, @args) = @_;
Subroutine import: called CC=17 SCAR=28.9
114  
115 # By default we are going to export subs as-is and not optimize them away:
116 11 my $should_export_subs = 1;
117  
118 11 my @export_args = ($package);
119  
120 # Copy args until we come across "-if" or "-unless":
121 #
122 11 ▶ while (@args && ($args[0] || '') !~ /^-(?:if|unless)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and not +($args[0] || "") =~ /^-(?:if|unless)$/) 18 11 29
Condition80%4/5
@args and not +($args[0] || "") =~ /^-(?:if|unless)$/
!ll&&!rl&&r
3818
$args[0] || ''
l!l
260
MC/DC0%0/2
@args and not +($args[0] || "") =~ /^-(?:if|unless)$/
@args $args[0]
Truth table66%2/3
A and not(B)
A= @argsB= $args[0]
ABresult
0X0
110
101
123 18 push @export_args, shift @args;
124 }
125  
126 # If any args remain, it must be because we found "-if" or "-unless".
127 # We expect the next arg to be the condition boolean or coderef:
128 #
129 11 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 3 11
130 8 my $thing = shift @args; # i.e. "-if" or "-unless"
131 8 ▶ unless (@args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 8 8
132 0 croak "$package->import failed: " .
133 qq{"$thing" must be followed by boolean or coderef};
134 }
135 8 my $condition = shift @args;
136  
137 # Go ahead and evaluate the condition if it's a coderef:
138 8 ▶ $condition = $condition->() if ref($condition) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $condition eq "CODE" 4 4 8
139  
140 # We might decide against importing subs, depending on the condition:
141 8 ▶ $should_export_subs = $thing eq '-if' ? $condition : !$condition;
Branch: 1 branch
BranchTrueFalseTotal
$thing eq '-if' ? : 4 4 8
142  
143 # Copy any remaining args:
144 #
145 8 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 8 8
146 0 ▶ if ($args[0] && $args[0] =~ /^-(?:if|unless)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and $args[0] =~ /^-(?:if|unless)$/) 0 0 0
Condition0%0/3
$args[0] and $args[0] =~ /^-(?:if|unless)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$args[0] and $args[0] =~ /^-(?:if|unless)$/
$args[0] $args[0] =~ /^-(?:if|unless)$/
Truth table0%0/3
A and B
A= $args[0]B= $args[0] =~ /^-(?:if|unless)$/
ABresult
0X0
100
111
147 0 croak "$package->import failed: " .
148 qq{Cannot use "$args[0]" after "$thing"};
149 }
150 0 push @export_args, shift @args;
151 }
152 }
153  
154 # If the "if" condition is false, or the "unless" condition is true,
155 # replace any exportable subs with something that will get optimized away:
156 #
157 11 my %their_original_coderefs;
158 11 ▶ unless ($should_export_subs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($should_export_subs) 4 7 11
159  
160 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
161 4 my $stash = *{ $package . "::" };
162  
163 4 ▶ my @their_export_oks = @{ ($stash->{EXPORT_OK} || []) };
Branch: 1 branch
BranchTrueFalseTotal
$stash->{"EXPORT_OK"} or [] 0 4 4
164 4 for my $export_name (@their_export_oks) {
165  
166 8 my $globref = $stash->{$export_name};
167 8 ▶ if ($globref && *$globref{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($globref and *$globref{"CODE"}) 4 4 8
Condition100%2/2
$globref and *$globref{"CODE"}
!ll
44
MC/DC100%1/1
$globref and *$globref{"CODE"}
$globref
Truth table100%2/2
A
A= $globref
Aresult
00
11
168  
169 4 my $symbol = $package . "::" . $export_name;
170  
171 # Save a copy of the original code:
172 4 $their_original_coderefs{$symbol} = \&$symbol;
173  
174 # Replace the sub being imported with a void prototype sub
175 # that gets optimized away:
176 #
177 {
178 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 3 ▶ no warnings 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 4 *$symbol = sub () {0};
182  
183 #---------------------------------------------------------
184 #
185 # This section lifted almost as-is from Debug::Show v0.0
186 #
187 # https://metacpan.org/pod/Debug::Show
188 #
189 # Thanks to Zefram
190 #
191 cv_set_call_checker(\&$symbol, sub ($$$) {
192 8 ▶ my($entersubop, undef, undef) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
193 # B::Generate doesn't offer a way to explicitly free ops.
194 # We ought to be able to implicitly free $entersubop via
195 # constant folding, by something like
196 #
197 # return B::LOGOP->new("and", 0,
198 # B::SVOP->new("const", 0, !1),
199 # $entersubop);
200 #
201 # but empirically that causes memory corruption and it's
202 # not clear why. For the time being, leak $entersubop.
203 8 return B::SVOP->new("const", 0, !1);
204 4 }, \!1);
205 #
206 #---------------------------------------------------------
207 }
208 }
209 }
210 }
211  
212 # Let Export handle everything else as usual:
213 11 $package->export_to_level(1, @export_args);
214  
215 # Restore coderefs in the original package with their saved version:
216 #
217 11 ▶ while (my ($symbol, $coderef) = each %their_original_coderefs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %their_original_coderefs) 4 11 15
218 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 3 ▶ no warnings 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 4 *$symbol = $coderef;
222 }
223 }
224  
225 #pod =head1 SEE ALSO
226 #pod
227 #pod L<Exporter>
228 #pod
229 #pod L<B::CallChecker>
230 #pod
231 #pod L<B::Generate>
232 #pod
233 #pod =head1 AUTHOR
234 #pod
235 #pod Larry Leszczynski, C<< <larryl at cpan.org> >>
236 #pod
237 #pod =head1 BUGS
238 #pod
239 #pod Please report any bugs or feature requests at:
240 #pod L<https://github.com/GrantStreetGroup/Exporter-ConditionalSubs>
241 #pod
242 #pod =head1 SUPPORT
243 #pod
244 #pod You can find documentation for this module with the perldoc command.
245 #pod
246 #pod perldoc Exporter::ConditionalSubs
247 #pod
248 #pod You can also look for information at:
249 #pod
250 #pod =over 4
251 #pod
252 #pod =item * GitHub
253 #pod
254 #pod L<https://github.com/GrantStreetGroup/Exporter-ConditionalSubs>
255 #pod
256 #pod =item * MetaCPAN
257 #pod
258 #pod L<https://metacpan.org/pod/Exporter::ConditionalSubs>
259 #pod
260 #pod =item * AnnoCPAN, Annotated CPAN documentation
261 #pod
262 #pod L<http://annocpan.org/dist/Exporter-ConditionalSubs>
263 #pod
264 #pod =item * CPAN Ratings
265 #pod
266 #pod L<http://cpanratings.perl.org/d/Exporter-ConditionalSubs>
267 #pod
268 #pod =back
269 #pod
270 #pod =head1 AUTHOR
271 #pod
272 #pod Larry Leszczynski, C<< <larryl at cpan.org> >>
273 #pod
274 #pod =head1 ACKNOWLEDGEMENTS
275 #pod
276 #pod Thanks to Grant Street Group L<http://www.grantstreet.com> for funding
277 #pod development of this code.
278 #pod
279 #pod Thanks to Tom Christiansen (C<< <tchrist@perl.com> >>) for help with the
280 #pod symbol table hackery.
281 #pod
282 #pod Thanks to Zefram (C<< <zefram@fysh.org> >>) for the pointer to his
283 #pod L<Debug::Show> hackery.
284 #pod
285 #pod =head1 LICENSE AND COPYRIGHT
286 #pod
287 #pod Copyright 2015 Grant Street Group
288 #pod
289 #pod This program is free software; you can redistribute it and/or modify it
290 #pod under the terms of the the Artistic License (2.0). You may obtain a
291 #pod copy of the full license at:
292 #pod
293 #pod L<http://www.perlfoundation.org/artistic_license_2_0>
294 #pod
295 #pod Any use, modification, and distribution of the Standard or Modified
296 #pod Versions is governed by this Artistic License. By using, modifying or
297 #pod distributing the Package, you accept this license. Do not use, modify,
298 #pod or distribute the Package, if you do not accept this license.
299 #pod
300 #pod If your Modified Version has been derived from a Modified Version made
301 #pod by someone other than you, you are nevertheless required to ensure that
302 #pod your Modified Version complies with the requirements of this license.
303 #pod
304 #pod This license does not grant you the right to use any trademark, service
305 #pod mark, tradename, or logo of the Copyright Holder.
306 #pod
307 #pod This license includes the non-exclusive, worldwide, free-of-charge
308 #pod patent license to make, have made, use, offer to sell, sell, import and
309 #pod otherwise transfer the Package with respect to any patent claims
310 #pod licensable by the Copyright Holder that are necessarily infringed by the
311 #pod Package. If you institute patent litigation (including a cross-claim or
312 #pod counterclaim) against any party alleging that the Package constitutes
313 #pod direct or contributory patent infringement, then this Artistic License
314 #pod to you shall terminate on the date that such litigation is filed.
315 #pod
316 #pod Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
317 #pod AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
318 #pod THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
319 #pod PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
320 #pod YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
321 #pod CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
322 #pod CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
323 #pod EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
324 #pod
325 #pod =cut
326  
327 3 1;
328  
329 __END__
↑ Summary