Data/Unixish/sprintf.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 66.6% 12 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 92.5% 100 / 108 CC 12 SCAR 14.6
CC
12
Cov
91%
_sprintf_item
8.0
_sprintf_begin
7.2
sprintf
2.0
CRAP 4.3
« Data/Unixish/count.pm ↑ Summary Data/Unixish/linum.pm »
1 package Data::Unixish::sprintf;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use Log::Any '$log';
9  
10 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 2 our $DATE = '2025-02-24'; # DATE
16 2 our $DIST = 'Data-Unixish'; # DIST
17 2 our $VERSION = '1.574'; # VERSION
18  
19 2 our %SPEC;
20  
21 $SPEC{sprintf} = {
22 2 v => 1.1,
23 summary => 'Apply sprintf() on input',
24 description => <<'MARKDOWN',
25  
26 Array will also be processed (all the elements are fed to sprintf(), the result
27 is a single string), unless `skip_array` is set to true.
28  
29 Non-numbers can be skipped if you use `skip_non_number`.
30  
31 Undef, hashes, and other non-scalars are ignored.
32  
33 MARKDOWN
34 args => {
35 %common_args,
36 format => {
37 schema=>['str*'],
38 cmdline_aliases => { f=>{} },
39 req => 1,
40 pos => 0,
41 },
42 skip_non_number => {
43 schema=>[bool => default=>0],
44 },
45 skip_array => {
46 schema=>[bool => default=>0],
47 },
48 },
49 tags => [qw/formatting itemfunc text/],
50 };
51 2 ▶sub sprintf {
Subroutine sprintf: called CC=2 SCAR=6.9
Pod documented
52 2 my %args = @_;
53 2 my ($in, $out) = ($args{in}, $args{out});
54 2 my $format = $args{format};
55  
56 2 my $orig_locale = _sprintf_begin();
57  
58 2 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 8 2 10
59 8 push @$out, _sprintf_item($item, \%args);
60 }
61  
62 2 _sprintf_end(\%args, $orig_locale);
63  
64 2 [200, "OK"];
65 }
66  
67 4 ▶sub _sprintf_begin {
Subroutine _sprintf_begin: called CC=4 SCAR=19.7
68 4 my $orig_locale = setlocale(LC_ALL);
69 4 ▶ if ($ENV{LC_NUMERIC}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ENV{'LC_NUMERIC'}) { } 4 0 4
elsif ($ENV{'LC_ALL'}) { } 0 0 0
elsif ($ENV{'LANG'}) { } 0 0 0
70 4 setlocale(LC_NUMERIC, $ENV{LC_NUMERIC});
71 } elsif ($ENV{LC_ALL}) {
72 0 setlocale(LC_ALL, $ENV{LC_ALL});
73 } elsif ($ENV{LANG}) {
74 0 setlocale(LC_ALL, $ENV{LANG});
75 }
76 4 return $orig_locale;
77 }
78  
79 4 ▶sub _sprintf_end {
Subroutine _sprintf_end: called CC=1 SCAR=0.0
80 4 my ($args, $orig_locale) = @_;
81 4 setlocale(LC_ALL, $orig_locale);
82 }
83  
84 16 ▶sub _sprintf_item {
Subroutine _sprintf_item: called CC=8 SCAR=20.8
85 16 my ($item, $args) = @_;
86  
87 {
88 16 ▶ last unless defined($item);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 4 12 16
89  
90 12 my $r = ref($item);
91 12 ▶ if ($r eq 'ARRAY' && !$args->{skip_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "ARRAY" and not $args->{"skip_array"}) 2 10 12
Condition100%3/3
$r eq "ARRAY" and not $args->{"skip_array"}
!ll&&!rl&&r
822
MC/DC100%2/2
$r eq "ARRAY" and not $args->{"skip_array"}
$r eq "ARRAY" not $args->{"skip_array"}
Truth table100%3/3
A and not(B)
A= $r eq "ARRAY"B= not $args->{"skip_array"}
ABresult
0X0
100
111
92 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 $item = CORE::sprintf($args->{format}, @$item);
94 2 last;
95 }
96 10 ▶ last if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 2 8 10
97 8 ▶ last if $item eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "" 0 8 8
98 8 ▶ last if !looks_like_number($item) && $args->{skip_non_number};
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($item) and $args->{"skip_non_number"} 2 6 8
Condition100%3/3
not &looks_like_number($item) and $args->{"skip_non_number"}
!ll&&!rl&&r
422
MC/DC100%2/2
not &looks_like_number($item) and $args->{"skip_non_number"}
not &looks_like_number($item) $args->{"skip_non_number"}
Truth table100%3/3
not(A) and B
A= not &looks_like_number($item)B= $args->{"skip_non_number"}
ABresult
0X0
100
111
99 {
100 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 6 $item = CORE::sprintf($args->{format}, $item);
102 }
103 }
104 16 return $item;
105 }
106  
107 2 1;
108 # ABSTRACT: Apply sprintf() on input
109  
110 __END__
« Data/Unixish/count.pm ↑ Summary Data/Unixish/linum.pm »