Util/Underscore.pm

filter: Statementstmt 99.0% 105 / 106 Branchbran 87.5% 14 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 33 / 33 Podpod n/a totaltotal 95.6% 153 / 160 CC 10 SCAR 5.3
CC
10
Cov
96%
prototype
4.2
__ANON__
3.0
callstack
3.0
CRAP 1.7
« Util/Underscore/Objects.pm ↑ Summary Util/Underscore/CallStackFrame.pm »
1 package Util::Underscore;
2  
3 #ABSTRACT: Common helper functions without having to import them
4  
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use version 0.77; our $VERSION = qv('v1.4.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Const::Fast 0.011 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Data::Dump 1.10 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use List::MoreUtils 0.07 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use List::Util 1.35 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Scalar::Util 1.36 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Try::Tiny 0.03 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use IPC::Run 0.92 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 BEGIN {
23 # check if a competing "_" exists
24 13 ▶ if (keys %{_::}) {
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (keys %main::_::) 1 12 13
25 1 Carp::confess qq(The package "_" has already been defined);
26 }
27 }
28  
29 BEGIN {
30 # Load the dummy "_.pm" module.
31 # This will set up various booby traps so that "_" isn't used directly.
32 # In order to prevent the traps from triggering when *we* go there, we have
33 # to declare our peaceful intentions:
34 12 ▶ local our $_WE_COME_IN_PEACE = 'pinky swear';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 require _;
36 }
37  
38 12 our $_ASSIGN_ALIASES;
39  
40 BEGIN {
41 $_ASSIGN_ALIASES = sub {
42 108 ▶ my ($pkg, %aliases) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
43 12 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
44 108 ▶ while (my ($this, $that) = each %aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %aliases) 432 108 540
45 432 my $target = "_::${this}";
46 432 my $source = "${pkg}::${that}";
47 432 *{$target} = *{$source}{CODE}
48 432 ▶ // Carp::croak "Unknown subroutine $source in _ASSIGN_ALIASES";
Condition33%1/3
*{$source;}{"CODE"} // Carp::croak("Unknown subroutine $source in _ASSIGN_ALIASES")
l!l&&r!l&&!r
43200
MC/DC0%0/2
*{$source;}{"CODE"} // Carp::croak("Unknown subroutine $source in _ASSIGN_ALIASES")
*{$source;}{"CODE"} Carp::croak("Unknown subroutine $source in _ASSIGN_ALIASES")
Truth table33%1/3
A // B
A= *{$source;}{"CODE"}B= Carp::croak("Unknown subroutine $source in _ASSIGN_ALIASES")
ABresult
1X1
011
000
49 }
50 12 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
51 }
52  
53  
54 # From now, every function is in the "_" package
55 ## no critic (ProhibitMultiplePackages)
56 package # Hide from PAUSE
57 _;
58  
59 ## no critic (RequireArgUnpacking, RequireFinalReturn, ProhibitSubroutinePrototypes)
60 # Why this "no critic"? In an util module, efficiency is crucial because we
61 # have no idea about the context where these function are being used. Therefore,
62 # no arg unpacking, and no explicit return. Most functions are so trivial anyway
63 # that this isn't much of a legibility concern.
64 # Subroutine prototypes are used to offer a convenient and natural interface.
65 # I fully understand why they shouldn't be used in ordinary code, but this
66 # module puts them to mostly good use.
67  
68 # Predeclare a few things so that we can use them in the sub definitions below.
69 sub blessed(_);
70 sub ref_type(_);
71  
72 # load the actual function collections
73 12 ▶use Util::Underscore::Scalars ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 12 ▶use Util::Underscore::Numbers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75 12 ▶use Util::Underscore::References ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76 12 ▶use Util::Underscore::Objects ();
Subroutine BEGIN: called CC=1 SCAR=0.0
77 12 ▶use Util::Underscore::ListUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79  
80 BEGIN {
81 12 ▶ $_ASSIGN_ALIASES->(
Subroutine BEGIN: called CC=1 SCAR=0.0
82 'Carp',
83 carp => 'carp',
84 cluck => 'cluck',
85 croak => 'croak',
86 confess => 'confess',
87 );
88 }
89  
90 12 $_ASSIGN_ALIASES->(
91 'Try::Tiny',
92 try => 'try',
93 catch => 'catch',
94 finally => 'finally',
95 );
96  
97 sub carpf($@) {
98 1 ▶ my $pattern = shift;
Subroutine carpf: called CC=1 SCAR=0.0
99 1 @_ = sprintf $pattern, @_;
100 1 goto &carp;
101 }
102  
103 sub cluckf($@) {
104 1 ▶ my $pattern = shift;
Subroutine cluckf: called CC=1 SCAR=0.0
105 1 @_ = sprintf $pattern, @_;
106 1 goto &cluck;
107 }
108  
109 sub croakf($@) {
110 1 ▶ my $pattern = shift;
Subroutine croakf: called CC=1 SCAR=0.0
111 1 @_ = sprintf $pattern, @_;
112 1 goto &croak;
113 }
114  
115 sub confessf($@) {
116 1 ▶ my $pattern = shift;
Subroutine confessf: called CC=1 SCAR=0.0
117 1 @_ = sprintf $pattern, @_;
118 1 goto &confess;
119 }
120  
121  
122 12 $_ASSIGN_ALIASES->('Scalar::Util', is_open => 'openhandle');
123  
124 sub _::prototype ($;$) {
125 10 ▶ if (@_ == 2) {
Subroutine prototype: called CC=4 SCAR=14.5
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 4 6 10
126 4 ▶ goto &Scalar::Util::set_prototype if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 4 0 4
127 }
128 6 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 6 0 6
129 6 my ($coderef) = @_;
130 6 return prototype $coderef; # Calls CORE::prototype
131 }
132 else {
133 ## no critic (RequireInterpolationOfMetachars)
134 0 Carp::confess '_::prototype($;$) takes exactly one or two arguments';
135 }
136 }
137  
138 # This sub uses CamelCase because it's a factory function
139 sub Dir(@) { ## no critic (NamingConventions::Capitalization)
140 1 ▶ require Path::Class;
Subroutine Dir: called CC=1 SCAR=0.0
141 1 Path::Class::Dir->new(@_);
142 }
143  
144 # This sub uses CamelCase because it's a factory function
145 sub File(@) { ## no critic (NamingConventions::Capitalization)
146 1 ▶ require Path::Class;
Subroutine File: called CC=1 SCAR=0.0
147 1 Path::Class::File->new(@_);
148 }
149  
150  
151 12 $_ASSIGN_ALIASES->(
152 'Data::Dump',
153 pp => 'pp',
154 dd => 'dd',
155 );
156  
157  
158 ## no critic (ProhibitBuiltinHomonyms)
159 sub caller(;$) {
160 3 ▶ require Util::Underscore::CallStackFrame;
Subroutine caller: called CC=2 SCAR=6.9
161 3 ▶ Util::Underscore::CallStackFrame->of(@_ ? shift() + 1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 2 3
162 }
163  
164 sub callstack(;$) {
165 2 ▶ my $level = @_ ? shift() + 1 : 1;
Subroutine callstack: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
166 2 my @callers;
167 2 ▶ while (my $caller = Util::Underscore::CallStackFrame->of($level + @callers))
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = "Util::Underscore::CallStackFrame"->of($level + @callers)) 19 2 21
168 {
169 19 push @callers, $caller;
170 }
171 2 return @callers;
172 }
173  
174  
175 12 $_ASSIGN_ALIASES->(
176 'IPC::Run',
177 process_run => 'run',
178 process_start => 'start',
179 );
180  
181  
182  
183 12 1;
184  
185 __END__
« Util/Underscore/Objects.pm ↑ Summary Util/Underscore/CallStackFrame.pm »