blib/lib/perl5i/2/SCALAR.pm

filter: Statementstmt 88.4% 123 / 139 Branchbran 86.2% 50 / 58 Conditioncond 41.9% 13 / 31 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 87.8% 29 / 33 Podpod 0.0% 0 / 25 totaltotal 72.8% 220 / 302 SCAR 39.7
CC
42
Cov
82%
group_digits
14.9
_get_grouping
12.0
center
6.0
CRAP 53.0
« blib/lib/perl5i/2/equal.pm ↑ Summary blib/lib/perl5i/2/DateTime.pm »
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::2::SCALAR;
3 109 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 109 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 109 require Carp::Fix::1_25;
8 109 ▶use perl5i::2::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub title_case {
11 4 ▶ my ($string) = @_;
Subroutine title_case: called CC=1 SCAR=0.0
Pod undocumented
12 4 $string =~ s/\b(\w)/\U$1/g;
13 4 return $string;
14 }
15  
16  
17 sub center {
18 27 ▶ my ($string, $size, $char) = @_;
Subroutine center: called CC=6 SCAR=17.9
Pod undocumented
19 27 ▶ Carp::Fix::1_25::carp("Use of uninitialized value for size in center()") if !defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 1 26 27
20 27 ▶ $size //= 0;
Condition100%2/2
$size //= 0
l!l
261
MC/DC100%1/1
$size //= 0
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
21 27 ▶ $char //= ' ';
Condition100%2/2
$char //= " "
l!l
522
MC/DC100%1/1
$char //= " "
$char
Truth table100%2/2
A
A= $char
Aresult
11
00
22  
23 27 ▶ if (length $char > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $char > 1) 1 26 27
24 1 my $bad = $char;
25 1 $char = substr $char, 0, 1;
26 1 Carp::Fix::1_25::carp("'$bad' is longer than one character, using '$char' instead");
27 }
28  
29 27 my $len = length $string;
30  
31 27 ▶ return $string if $size <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $size <= $len 6 21 27
32  
33 21 my $padlen = $size - $len;
34  
35 # pad right with half the remaining characters
36 21 my $rpad = int( $padlen / 2 );
37  
38 # bias the left padding to one more space, if $size - $len is odd
39 21 my $lpad = $padlen - $rpad;
40  
41 21 return $char x $lpad . $string . $char x $rpad;
42 }
43  
44  
45 sub ltrim {
46 33 ▶ my ($string,$trim_charset) = @_;
Subroutine ltrim: called CC=2 SCAR=6.9
Pod undocumented
47 33 ▶ $trim_charset = '\s' unless defined $trim_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trim_charset 24 9 33
48 33 my $re = qr/^[$trim_charset]*/;
49 33 $string =~ s/$re//;
50 33 return $string;
51 }
52  
53  
54 sub rtrim {
55 33 ▶ my ($string,$trim_charset) = @_;
Subroutine rtrim: called CC=2 SCAR=6.9
Pod undocumented
56 33 ▶ $trim_charset = '\s' unless defined $trim_charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trim_charset 24 9 33
57 33 my $re = qr/[$trim_charset]*$/;
58 33 $string =~ s/$re//;
59 33 return $string;
60 }
61  
62  
63 sub trim {
64 17 ▶ my $charset = $_[1];
Subroutine trim: called CC=1 SCAR=0.0
Pod undocumented
65  
66 17 return rtrim(ltrim($_[0], $charset), $charset);
67 }
68  
69  
70 sub wrap {
71 30 ▶ my ($string, %args) = @_;
Subroutine wrap: called CC=4 SCAR=13.9
Pod undocumented
72  
73 30 ▶ my $width = $args{width} // 76;
Condition100%2/2
$args{"width"} // 76
l!l
246
MC/DC100%1/1
$args{"width"} // 76
$args{"width"}
Truth table100%2/2
A
A= $args{"width"}
Aresult
11
00
74 30 ▶ my $separator = $args{separator} // "\n";
Condition50%1/2
$args{"separator"} // "\n"
l!l
300
MC/DC0%0/1
$args{"separator"} // "\n"
$args{"separator"}
Truth table50%1/2
A
A= $args{"separator"}
Aresult
11
00
75  
76 30 ▶ return $string if $width <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 12 18 30
77  
78 18 require Text::Wrap;
79 18 local $Text::Wrap::separator = $separator;
80 18 local $Text::Wrap::columns = $width;
81  
82 18 return Text::Wrap::wrap('', '', $string);
83  
84 }
85  
86  
87 # Always reverse the scalar, not a single element list.
88 sub reverse {
89 2 ▶ return scalar CORE::reverse $_[0];
Subroutine reverse: called CC=1 SCAR=0.0
Pod undocumented
90 }
91  
92  
93 # Compatiblity wrappers. Remove in 3.0.
94 sub untaint {
95 1 ▶ return $_[0]->mo->untaint;
Subroutine untaint: called CC=1 SCAR=0.0
Pod undocumented
96 }
97  
98 sub taint {
99 1 ▶ return $_[0]->mo->taint;
Subroutine taint: called CC=1 SCAR=0.0
Pod undocumented
100 }
101  
102 sub is_tainted {
103 3 ▶ return $_[0]->mo->is_tainted;
Subroutine is_tainted: called CC=1 SCAR=0.0
Pod undocumented
104 }
105  
106  
107 sub require {
108 36 ▶ my $error = do {
Subroutine require: called CC=2 SCAR=6.9
Pod undocumented
109 # Don't let them leak out or get reset
110 36 local($!,$@);
111 36 ▶ return $_[0] if eval { require $_[0]->module2path };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require $_[0]->module2path } } 34 2 36
112 2 $@;
113 };
114  
115 2 my($pack, $file, $line) = caller;
116 2 $error =~ s{ at .*? line .*?\.\n$}{ at $file line $line.\n};
117 2 die $error;
118 }
119  
120  
121 sub alias {
122 3 ▶ Carp::Fix::1_25::croak(<<ERROR) if !ref $_[0];
Subroutine alias: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 2 3
123 Due to limitations in autoboxing, scalars cannot be aliased.
124 Sorry. Use a scalar reference instead.
125 ERROR
126  
127 2 goto &perl5i::2::UNIVERSAL::alias;
128 }
129  
130  
131 109 require POSIX;
132 109 *ceil = \&POSIX::ceil;
133 109 *floor = \&POSIX::floor;
134 109 *round_up = \&ceil;
135 109 *round_down = \&floor;
136 sub round {
137 7 ▶ return 0 if $_[0] == 0;
Subroutine round: called CC=5 SCAR=16.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == 0 1 6 7
138  
139 6 ▶ if( $_[0]->is_positive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->is_positive) { } 3 3 6
140 3 ▶ abs($_[0] - int($_[0])) < 0.5 ? round_down($_[0])
Branch: 1 branch
BranchTrueFalseTotal
abs $_[0] - int($_[0]) < 0.5 ? : 1 2 3
141 : round_up($_[0])
142 }
143 else {
144 3 ▶ abs($_[0] - int($_[0])) < 0.5 ? round_up($_[0])
Branch: 1 branch
BranchTrueFalseTotal
abs $_[0] - int($_[0]) < 0.5 ? : 1 2 3
145 : round_down($_[0])
146 }
147 }
148  
149 109 require Scalar::Util;
150 109 *is_number = \&Scalar::Util::looks_like_number;
151 23 ▶sub is_positive { $_[0]->is_number && $_[0] > 0 }
Subroutine is_positive: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_number and $_[0] > 0 21 2 23
152 7 ▶sub is_negative { $_[0]->is_number && $_[0] < 0 }
Subroutine is_negative: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_number and $_[0] < 0 5 2 7
153 sub is_integer {
154 33 ▶ return 0 if !$_[0]->is_number;
Subroutine is_integer: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->is_number 4 29 33
155 29 return $_[0] =~ m{ ^[+-]? \d+ $}x;
156 }
157 109 *is_int = \&is_integer;
158 5 ▶sub is_even { $_[0]->is_integer && !($_[0] % 2) }
Subroutine is_even: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_integer and not $_[0] % 2 4 1 5
159 5 ▶sub is_odd { $_[0]->is_integer && ($_[0] % 2) }
Subroutine is_odd: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_integer and $_[0] % 2 4 1 5
160 sub is_decimal {
161 9 ▶ return 0 if !$_[0]->is_number;
Subroutine is_decimal: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->is_number 2 7 9
162  
163 # Fast and reliable way to spot most decimals
164 7 ▶ return 1 if ((int($_[0]) - $_[0]) != 0);
Branch: 1 branch
BranchTrueFalseTotal
if int($_[0]) - $_[0] != 0 2 5 7
165  
166 # Final gate for tricky things like 1.0, 1. and .0
167 5 return $_[0] =~ m{^ [+-]? (?: \d+\.\d* | \.\d+ ) $}x;
168 }
169  
170 sub group_digits {
171 15 ▶ my($self, $opts) = @_;
Subroutine group_digits: called CC=9 SCAR=27.0
Pod undocumented
172  
173 15 state $defaults = {
174 thousands_sep => ",",
175 grouping => 3,
176 decimal_point => ".",
177 };
178  
179 15 my $is_money = $opts->{currency};
180 my $sep = $opts->{separator} // (_get_from_locale("thousands_sep", $is_money)
181 15 ▶ || $defaults->{thousands_sep});
Condition16%1/6
$opts->{"separator"} // (_get_from_locale("thousands_sep", $is_money) || $defaults->{"thousands_sep"})
l!l&&r!l&&!r
1500
_get_from_locale("thousands_sep", $is_money) or $defaults->{"thousands_sep"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts->{"separator"} // (_get_from_locale("thousands_sep", $is_money) || $defaults->{"thousands_sep"})
$opts->{"separator"} _get_from_locale("thousands_sep", $is_money) $defaults->{"thousands_sep"}
Truth table25%1/4
A // B or C
A= $opts->{"separator"}B= _get_from_locale("thousands_sep", $is_money)C= $defaults->{"thousands_sep"}
ABCresult
1XX1
01X1
0011
0000
182 my $grouping = $opts->{grouping} // (_get_grouping($is_money)
183 15 ▶ || $defaults->{grouping});
Condition16%1/6
$opts->{"grouping"} // (_get_grouping($is_money) || $defaults->{"grouping"})
l!l&&r!l&&!r
1500
_get_grouping($is_money) or $defaults->{"grouping"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts->{"grouping"} // (_get_grouping($is_money) || $defaults->{"grouping"})
$opts->{"grouping"} _get_grouping($is_money) $defaults->{"grouping"}
Truth table25%1/4
A // B or C
A= $opts->{"grouping"}B= _get_grouping($is_money)C= $defaults->{"grouping"}
ABCresult
1XX1
01X1
0011
0000
184 my $decimal_point = $opts->{decimal_point} // (_get_from_locale("decimal_point", $is_money)
185 15 ▶ || $defaults->{decimal_point});
Condition16%1/6
$opts->{"decimal_point"} // (_get_from_locale("decimal_point", $is_money) || $defaults->{"decimal_point"})
l!l&&r!l&&!r
1500
_get_from_locale("decimal_point", $is_money) or $defaults->{"decimal_point"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts->{"decimal_point"} // (_get_from_locale("decimal_point", $is_money) || $defaults->{"decimal_point"})
$opts->{"decimal_point"} _get_from_locale("decimal_point", $is_money) $defaults->{"decimal_point"}
Truth table25%1/4
A // B or C
A= $opts->{"decimal_point"}B= _get_from_locale("decimal_point", $is_money)C= $defaults->{"decimal_point"}
ABCresult
1XX1
01X1
0011
0000
186 15 ▶ return $self if $grouping == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $grouping == 0 1 14 15
187  
188 14 my($integer, $decimal) = split m{\.}, $self, 2;
189  
190 14 $integer = CORE::reverse $integer;
191 14 $integer =~ s/(\d{$grouping})(?=\d)(?!\d*\.)/$1$sep/g;
192 14 $integer = CORE::reverse $integer;
193  
194 14 my $number = $integer;
195 14 ▶ $number .= $decimal_point . $decimal if defined $decimal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $decimal 6 8 14
196  
197 14 return $number;
198 }
199  
200 sub commify {
201 15 ▶ my($self, $args) = @_;
Subroutine commify: called CC=1 SCAR=0.0
Pod undocumented
202  
203 15 state $defaults = {
204 separator => ",",
205 grouping => 3,
206 decimal_point => ".",
207 };
208  
209 15 my $opts = $defaults->merge($args);
210  
211 15 return $self->group_digits( $opts );
212 }
213  
214  
215 sub _get_lconv {
216 0 ▶ return eval {
Subroutine _get_lconv: not called CC=2 SCAR=17.9
Condition0%0/2
eval { || {}
l!l
00
MC/DC0%0/1
eval { do { require POSIX; POSIX::localeconv() } } || {}
eval { do { require POSIX; POSIX::localeconv() } }
Truth table0%0/2
A
A= eval { do { require POSIX; POSIX::localeconv() } }
Aresult
11
00
217 0 require POSIX;
218 0 POSIX::localeconv();
219 } || {};
220 }
221  
222 sub _get_grouping {
223 0 ▶ my $is_money = shift;
Subroutine _get_grouping: not called CC=3 SCAR=24.8
224 0 ▶ my $key = $is_money ? "mon_grouping" : "grouping";
Branch: 1 branch
BranchTrueFalseTotal
$is_money ? : 0 0 0
225  
226 0 my $lconv = _get_lconv;
227  
228 0 ▶ if( $lconv->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lconv->{$key}) { } 0 0 0
229 0 return (unpack("C*", $lconv->{$key}))[0];
230 }
231 else {
232 0 return;
233 }
234 }
235  
236 sub _get_from_locale {
237 0 ▶ my($key, $is_money) = @_;
Subroutine _get_from_locale: not called CC=2 SCAR=17.9
238 0 ▶ $key = "mon_$key" if $is_money;
Branch: 1 branch
BranchTrueFalseTotal
if $is_money 0 0 0
239  
240 0 my $lconv = _get_lconv;
241 0 return $lconv->{$key};
242 }
243  
244  
245 sub path2module {
246 7 ▶ my $path = shift;
Subroutine path2module: called CC=4 SCAR=13.9
Pod undocumented
247  
248 7 my($vol, $dirs, $file) = File::Spec->splitpath($path);
249 7 my @dirs = grep length, File::Spec->splitdir($dirs);
250  
251 7 ▶ Carp::Fix::1_25::croak("'$path' does not look like a Perl module path")
Branch: 1 branch
BranchTrueFalseTotal
if not $file =~ /\.pm$/ or "File::Spec"->file_name_is_absolute($path) 3 4 7
Condition100%3/3
not $file =~ /\.pm$/ or "File::Spec"->file_name_is_absolute($path)
l!l&&r!l&&!r
214
MC/DC100%2/2
not $file =~ /\.pm$/ or "File::Spec"->file_name_is_absolute($path)
not $file =~ /\.pm$/ "File::Spec"->file_name_is_absolute($path)
Truth table100%3/3
not(A) or B
A= not $file =~ /\.pm$/B= "File::Spec"->file_name_is_absolute($path)
ABresult
1X1
011
000
252 if $file !~ m{\.pm$} or File::Spec->file_name_is_absolute($path);
253  
254 4 $file =~ s{\.pm$}{};
255  
256 4 my $module = join "::", @dirs, $file;
257 4 ▶ Carp::Fix::1_25::croak("'$module' is not a valid module name") unless $module->is_module_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $module->is_module_name 0 4 4
258  
259 4 return $module;
260 }
261  
262  
263 sub is_module_name {
264 59 ▶ my $name = shift;
Subroutine is_module_name: called CC=3 SCAR=11.0
Pod undocumented
265  
266 59 ▶ return 0 unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 59 59
267  
268 59 ▶ return 0 unless $name =~ qr{\A
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ qr/\A [[:alpha:]_] # Must start with an alpha or _ [[:word:]]* # Then any number of alpha numerics or _ (?: :: [[:word:]]+ )* # Then optional ::word's \z /x 10 49 59
269 [[:alpha:]_] # Must start with an alpha or _
270 [[:word:]]* # Then any number of alpha numerics or _
271 (?: :: [[:word:]]+ )* # Then optional ::word's
272 \z
273 }x;
274  
275 49 return 1;
276 }
277  
278  
279 sub module2path {
280 43 ▶ my $module = shift;
Subroutine module2path: called CC=2 SCAR=6.9
Pod undocumented
281  
282 43 ▶ Carp::Fix::1_25::croak("'$module' is not a valid module name") unless $module->is_module_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $module->is_module_name 4 39 43
283  
284 39 my @parts = split /::/, $module;
285 39 $parts[-1] .= ".pm";
286  
287 39 return join "/", @parts;
288 }
289  
290  
291 # On the first call, load Path::Tiny and replace our path() with
292 # Path::Tiny::path() so we don't try to load it again.
293 # Shaves about 1/6 off the call time.
294 sub path {
295 0 ▶ require Path::Tiny;
Subroutine path: not called CC=1 SCAR=1.2
Pod undocumented
296 109 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
297 0 *path = \&Path::Tiny::path;
298 0 goto &Path::Tiny::path;
299 }
300  
301 109 1;
« blib/lib/perl5i/2/equal.pm ↑ Summary blib/lib/perl5i/2/DateTime.pm »