blib/lib/Fauxtobox.pm

filter: Statementstmt 80.9% 115 / 142 Branchbran 53.2% 33 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.3% 43 / 51 Podpod n/a totaltotal 74.9% 191 / 255 CC 29 SCAR 11.0
CC
29
Cov
73%
__ANON__
12.0
__ANON__
12.0
__ANON__
6.0
CRAP 3.0
↑ Summary
1 package Fauxtobox;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Munge qw(eval_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @ISA = qw(Exporter);
11  
12 2 our $VERSION = '0.02';
13  
14 2 ▶sub import {
Subroutine import: called CC=2 SCAR=7.4
15 2 my $class = shift;
16 2 ▶ @_ = map /^[a-z0-9]+\z/ ? '$_' . $_ : $_, @_;
Branch: 1 branch
BranchTrueFalseTotal
/^[a-z0-9]+\z/ ? : 0 0 0
17 2 unshift @_, $class;
18 2 goto &{ $class->can('SUPER::import') };
19 }
20  
21 54 ▶sub _filetest {
Subroutine _filetest: called CC=1 SCAR=0.0
22 54 my ($name) = @_;
23 54 "test_$name" => eval_string("sub { -$name \$_[0] }")
24 }
25  
26 16 ▶sub _xlist {
Subroutine _xlist: called CC=1 SCAR=0.0
27 16 my ($n, $name) = @_;
28 16 my $xs = join '', map "\$x$_, ", 1 .. $n;
29 16 $name => eval_string "sub { my (\$arg, $xs) = \@_; $name(${xs}ref(\$arg) eq 'ARRAY' ? \@\$arg : \$_[0]) }"
30 }
31  
32 122 ▶sub _fixed_opt {
Subroutine _fixed_opt: called CC=3 SCAR=11.1
33 122 my ($n, $m, $name) = @_;
34 $name => do {
35 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 122 *{"CORE::$name"}{CODE}
38 122 ▶ } || eval_string do {
Branch: 1 branch
BranchTrueFalseTotal
do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} no strict 'refs'; *{"CORE::$name";}{"CODE"} } or eval_string(do { my $args = join(", ", map(("\$_[$_]"), 0 .. $n - 1)); my $base = "$name $args"; my $code = $base; foreach my $c (0 .. $m - 1) { my $i = $n + $c; $base .= ", \$_[$i]"; $code = "\@_ > $i ? $base : $code"; } ; "sub { $code }" }) 120 2 122
39 2 my $args = join ', ', map "\$_[$_]", 0 .. $n - 1;
40 2 my $base = "$name $args";
41 2 my $code = $base;
42 2 for my $c (0 .. $m - 1) {
43 0 my $i = $n + $c;
44 0 $base .= ", \$_[$i]";
45 0 $code = "\@_ > $i ? $base : $code";
46 }
47 "sub { $code }"
48 2 }
49 }
50  
51 116 ▶sub _fixed {
Subroutine _fixed: called CC=1 SCAR=0.0
52 116 my ($n, $name) = @_;
53 116 _fixed_opt $n, 0, $name
54 }
55  
56 94 ▶sub _scalar {
Subroutine _scalar: called CC=1 SCAR=0.0
57 94 my ($name) = @_;
58 94 _fixed 1, $name
59 }
60  
61 6 ▶sub _hxa {
Subroutine _hxa: called CC=3 SCAR=11.0
62 6 my ($name) = @_;
63 6 ▶ my $body =
Branch: 1 branch
BranchTrueFalseTotal
$^V ge v5.12.0 ? : 6 0 6
64 $^V ge v5.12.0 ?
65 "ref(\$_[0]) eq 'ARRAY' ? $name \@{\$_[0]} : $name %{\$_[0]}" :
66 "$name %{\$_[0]}"
67 ;
68 6 ▶ $body = "[$body]" unless $name eq 'each';
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "each" 4 2 6
69 6 $name => eval_string("sub { $body }")
70 }
71  
72 my %functions = (
73 1 ▶ apply => sub { my $x = shift; my $f = shift; $f->($x, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 0 ▶ list => sub { ref($_[0]) eq 'HASH' ? %{$_[0]} : @{$_[0]} },
Subroutine __ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 2 2
75 1 ▶ qr => sub { @_ > 1 ? qr/(?$_[1])$_[0]/ : qr/$_[0]/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
76  
77 1 ▶ m => sub { $_[0] =~ /$_[1]/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 ▶ m_g => sub { $_[0] =~ /$_[1]/g },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 0 ▶ m_gc => sub { $_[0] =~ /$_[1]/gc },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80  
81 1 ▶ s => sub { ref($_[2]) ? $_[0] =~ s/$_[1]/$_[2]()/e : $_[0] =~ s/$_[1]/$_[2]/ },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] ? : 1 0 1
82 1 ▶ s_g => sub { ref($_[2]) ? $_[0] =~ s/$_[1]/$_[2]()/ge : $_[0] =~ s/$_[1]/$_[2]/g },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] ? : 1 0 1
83 $^V ge v5.14.0 ? (
84 s_r => eval_string('sub { ref($_[2]) ? $_[0] =~ s/$_[1]/$_[2]()/re : $_[0] =~ s/$_[1]/$_[2]/r }'),
85 s_gr => eval_string('sub { ref($_[2]) ? $_[0] =~ s/$_[1]/$_[2]()/gre : $_[0] =~ s/$_[1]/$_[2]/gr }'),
86 ) : (
87 0 ▶ s_r => sub { my $s = $_[0]; if (ref $_[2]) { $s =~ s/$_[1]/$_[2]()/e } else { $s =~ s/$_[1]/$_[2]/ } $s },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2]) { } 0 0 0
88 0 ▶ s_gr => sub { my $s = $_[0]; if (ref $_[2]) { $s =~ s/$_[1]/$_[2]()/ge } else { $s =~ s/$_[1]/$_[2]/g } $s },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2]) { } 0 0 0
89 ),
90  
91 (map _filetest($_), qw(
92 r w x o
93 R W X O
94 e z s
95 f d l p S b c t
96 u g k
97 T B
98 M A C
99 )),
100  
101 _scalar('abs'),
102 _scalar('alarm'),
103 _fixed(2, 'atan2'),
104 0 ▶ bless => defined &CORE::bless ? \&CORE::bless : sub { bless $_[0], @_ > 1 ? $_[1] : scalar caller },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
105 _scalar('chdir'),
106 _xlist(1, 'chmod'),
107 _xlist(0, 'chomp'),
108 _xlist(0, 'chop'),
109 _xlist(2, 'chown'),
110 _scalar('chr'),
111 _scalar('chroot'),
112 _scalar('cos'),
113 _fixed(2, 'crypt'),
114 2 ▶ defined => sub { defined $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 ▶ delete => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
116 ref($_[0]) eq 'ARRAY' ?
117 delete $_[0][$_[1]] :
118 1 ▶ delete $_[0]{$_[1]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 1 1
119 },
120 _scalar('die'),
121 _hxa('each'),
122 1 ▶ eval => sub { eval $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 0 ▶ exec => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
124 0 my $prog = shift;
125 0 ▶ @_ ? exec { $prog } @_ :
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
ref $prog eq 'ARRAY' ? : 0 0 0
126 ref($prog) eq 'ARRAY' ? exec @$prog :
127 exec $prog
128 },
129 2 ▶ exists => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
130 ref($_[0]) eq 'ARRAY' ?
131 exists $_[0][$_[1]] :
132 2 ▶ exists $_[0]{$_[1]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 2 2
133 },
134 _scalar('exit'),
135 _scalar('exp'),
136 $^V ge v5.16.0 ? (fc => \&CORE::fc) : (),
137 _scalar('getpgrp'),
138 _scalar('getpwnam'),
139 _scalar('getgrnam'),
140 _scalar('gethostbyname'),
141 _scalar('getnetbyname'),
142 _scalar('getprotobyname'),
143 _scalar('getpwuid'),
144 _scalar('getgrgid'),
145 _fixed(2, 'getservbyname'),
146 _fixed(2, 'gethostbyaddr'),
147 _fixed(2, 'getnetbyaddr'),
148 _scalar('getprotobynumber'),
149 _fixed(2, 'getservbyport'),
150 2 ▶ glob => sub { [ glob $_[0] ] },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
151 _scalar('gmtime'),
152 1 ▶ grep => sub { my ($arg, $f) = @_; [ grep $f->($_), @$arg ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 _scalar('hex'),
154 _fixed_opt(2, 1, 'index'),
155 _scalar('int'),
156 1 ▶ join => sub { join $_[1], @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 _hxa('keys'),
158 _xlist(1, 'kill'),
159 _scalar('lc'),
160 _scalar('lcfirst'),
161 _scalar('length'),
162 _fixed(2, 'link'),
163 _scalar('localtime'),
164 _scalar('log'),
165 _scalar('lstat'),
166 1 ▶ map => sub { my ($arg, $f) = @_; [ map $f->($_), @$arg ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
167 _scalar('mkdir'),
168 _scalar('oct'),
169 _scalar('ord'),
170 _xlist(1, 'pack'),
171 1 ▶ pop => sub { pop @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
172 3 ▶ pos => sub :lvalue { @_ > 1 ? pos($_[0]) = $_[1] : pos($_[0]) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 2 3
173 _scalar('prototype'),
174 1 ▶ push => sub { my $arg = shift; push @$arg, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 _scalar('quotemeta'),
176 _scalar('rand'),
177 _scalar('readlink'),
178 _scalar('ref'),
179 _fixed(2, 'rename'),
180 _scalar('require'),
181 2 ▶ reverse => sub { ref($_[0]) eq 'ARRAY' ? [ reverse @{$_[0]} ] : scalar reverse $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
182 _fixed_opt(2, 1, 'rindex'),
183 _scalar('rmdir'),
184 1 ▶ shift => sub { shift @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
185 _scalar('sin'),
186 _scalar('sleep'),
187 2 ▶ sort => sub { [ @_ > 1 ? sort { $_[1]($a, $b) } @{$_[0]} : sort @{$_[0]} ] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 1 2
188 4 ▶ splice => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
189 4 my $arg = shift;
190 4 ▶ return splice @$arg unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 3 4
191 3 my $offset = shift;
192 3 ▶ return splice @$arg, $offset unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
193 2 my $length = shift;
194 2 splice @$arg, $offset, $length, @_
195 },
196 4 ▶ split => sub { [ @_ > 2 ? split $_[1], $_[0], $_[2] : @_ > 1 ? split $_[1], $_[0] : split ' ', $_[0] ] },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
@_ > 2 ? : 2 2 4
@_ > 1 ? : 1 1 2
197 _xlist(1, 'sprintf'),
198 _scalar('sqrt'),
199 _scalar('srand'),
200 _scalar('stat'),
201 _fixed_opt(2, 2, 'substr'),
202 _fixed(2, 'symlink'),
203 0 ▶ syscall => sub { my $arg = shift; syscall $arg, @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 ▶ system => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
205 0 my $prog = shift;
206 0 ▶ @_ ? system { $prog } @_ :
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
ref $prog eq 'ARRAY' ? : 0 0 0
207 ref($prog) eq 'ARRAY' ? system @$prog :
208 system $prog
209 },
210 _fixed(2, 'truncate'),
211 _scalar('uc'),
212 _scalar('ucfirst'),
213 _scalar('umask'),
214 _scalar('unlink'),
215 2 ▶ unpack => sub { unpack $_[1], $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
216 1 ▶ unshift => sub { my $arg = shift; unshift @$arg, @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
217 _xlist(2, 'utime'),
218 _hxa('values'),
219 2 ▶ vec => sub :lvalue { @_ > 3 ? vec($_[0], $_[1], $_[2]) = $_[3] : vec($_[0], $_[1], $_[2]) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 ? : 1 1 2
220 _fixed(2, 'waitpid'),
221 0 ▶ waitpid => sub { waitpid $_[0], @_ > 1 ? $_[1] : 0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
222 2 ▶ _scalar('warn'),
Branch: 3 branches
BranchTrueFalseTotal
$^V ge v5.14.0 ? : 2 0 2
defined &CORE::bless ? : 2 0 2
$^V ge v5.16.0 ? : 2 0 2
223 );
224  
225 2 our @EXPORT = map '$_' . $_, keys %functions;
226  
227 2 for my $k (keys %functions) {
228 266 my $v = $functions{$k};
229 2 ▶ my $svref = do { no strict 'refs'; \${"_$k"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
230 97 ▶ $$svref = sub { blessed($_[0]) and return shift->$k(@_); goto &$v };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and return (shift())->$k(@_) 1 96 97
231 266 ▶ Internals::SvREADONLY($$svref, 1) if defined &Internals::SvREADONLY;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Internals::SvREADONLY 266 0 266
232 }
233  
234 'ok'
235  
236 2 __END__
↑ Summary