Handy.pm

filter: Statementstmt 48.3% 30 / 62 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 9 / 15 Podpod 100% 6 / 6 totaltotal 44.1% 45 / 102 CC 10 SCAR 38.4
CC
10
Cov
38%
features
110.0
strictures
2.0
sane
2.0
CRAP 46.5
« Handy/Util.pm ↑ Summary
1 package Exporter::Handy;
2  
3 # ABSTRACT: An EXPERIMENTAL subclass of <Exporter::Extensible>, which helps create easy-to-extend modules that export symbols
4 2 our $VERSION = '1.000003';
5  
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Exporter::Extensible -exporter_setup => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # PRAGMATA
14 # Remember: Pragmas effect the current compilation context.
15 # No need to keep track of where we are importing into...
16 # They require their ->import() method to be called directly, no matter how deep the call stack happens to be.
17 # Just call ->import() directly, like below, and it will do the right thing.
18 2 ▶sub strict : Export(-) { strict->import }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine strict: not called CC=1 SCAR=0.2
Pod documented
19 2 ▶sub warnings : Export(-) { warnings->import }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine warnings: not called CC=1 SCAR=0.2
Pod documented
20 2 ▶sub utf8 : Export(-) { utf8->import }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine utf8: not called CC=1 SCAR=0.2
Pod documented
21  
22 sub strictures : Export(-) {
23 0 ▶ strict->import;
Subroutine strictures: not called CC=1 SCAR=6.9
Pod documented
24 0 warnings->import
25 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub sane : Export(-) {
28 0 ▶ utf8->import;
Subroutine sane: not called CC=1 SCAR=6.9
Pod documented
29 0 strict->import;
30 0 warnings->import;
31 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # use Exporter::Handy qw(-sane -features), exporter_setup => 1;
34 sub features {
35 0 ▶ my ($exporter, $arg)= @_;
Subroutine features: not called CC=10 SCAR=47.0
Pod documented
36  
37 # default features to turn on/off
38 0 my %feats = (
39 'current_sub' => 1, # Perl v5.16+ (2012) : enable __SUB__ token that returns a ref to the current subroutine (or undef).
40 'evalbytes' => 1, # Perl v5.16+ (2012) : like string eval, but it treats its argument as a byte string.
41 'fc' => 1, # Perl v5.16+ (2012) : enable the fc function (Unicode casefolding).
42 'lexical_subs' => 1, # Perl v5.18+ (2012) : enable declaration of subroutines via my sub foo, state sub foo and our sub foo syntax.
43 'say' => 1, # Perl v5.10+ (2007) : enable the Raku-inspired "say" function.
44 'state' => 1, # Perl v5.10+ (2007) : enable state variables.
45 'unicode_eval' => 1, # Perl v5.16+ (2012) : changes the behavior of plain string eval to work more consistently, especially in the Unicode world.
46 'unicode_strings' => 1, # Perl v5.12+ (2010) : use Unicode rules in all string operations (unless either use locale or use bytes are also within the scope).
47 );
48  
49 0 my @args = eval { @$arg }; # if $arg is an ARRAY-ref, than it denotes a list of features
50 0 my %args = eval { %$arg }; # if $arg is a HASH-ref, then it denotes individual overrides (1: on, 0:off)
51  
52 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
53 0 ▶ if ($args[0] eq '+') { # request to keep defaults.
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq '+') { } 0 0 0
54 0 shift @args;
55 0 %args = map { $_ => 1 } @args;
56 } else { # replace defaults
57 0 %feats = map { $_ => 1 } @args;
58 }
59 }
60  
61 # handle individual overrides
62 0 %feats = (%feats, %args);
63  
64 0 ▶ return unless %feats;
Branch: 1 branch
BranchTrueFalseTotal
unless %feats 0 0 0
65  
66 # determine features to be turned ON or OFF
67 0 my (@on, @off);
68 0 for (keys %feats) {
69 0 ▶ next if m/^-/; # ignore inline args à la <Exporter::Extensible>, if any: -prefix, -as, ...
Branch: 1 branch
BranchTrueFalseTotal
if /^-/ 0 0 0
70  
71 0 ▶ if (defined $feats{$_} && $feats{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $feats{$_} and $feats{$_}) { } 0 0 0
Condition0%0/3
defined $feats{$_} and $feats{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $feats{$_} and $feats{$_}
defined $feats{$_} $feats{$_}
Truth table0%0/3
A and B
A= defined $feats{$_}B= $feats{$_}
ABresult
0X0
100
111
72 0 push @on, $_;
73 } else {
74 0 push @off, $_;
75 }
76 }
77  
78 # Do the actual work
79 0 require feature;
80 0 ▶ feature->import(@on) if @on;
Branch: 1 branch
BranchTrueFalseTotal
if @on 0 0 0
81 0 ▶ feature->unimport(@off) if @off;
Branch: 1 branch
BranchTrueFalseTotal
if @off 0 0 0
82 }
83  
84 # Note that attribute syntax, i.e. :Export(-?), was not working before version 0.11 of <Exporter::Extensible>.
85 # So we take the more verbose approach, as below:
86 2 __PACKAGE__->exporter_register_option('features', \&features, '?');
87  
88 2 1;
89  
90 __END__
« Handy/Util.pm ↑ Summary