Hopen/Util/Data.pm

filter: Statementstmt 49.2% 31 / 63 Branchbran 19.0% 8 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 7 / 11 Podpod 100% 5 / 5 totaltotal 38.9% 51 / 131 CC 25 SCAR 34.3
CC
25
Cov
35%
dedent
90.0
_describe
20.0
identical
20.0
CRAP 31.0
« Hopen/Arrrgs.pm ↑ Summary Hopen/T/Gnu/C.pm »
1 # Build::Hopen::Util::Data - general-purpose data-manipulation functions
2 package Build::Hopen::Util::Data;
3 10 ▶use Build::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Build::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '0.000008'; # TRIAL
7  
8 10 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
10 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 @EXPORT = qw();
12 10 @EXPORT_OK = qw(boolify clone dedent forward_opts identical);
13 10 %EXPORT_TAGS = (
14 default => [@EXPORT],
15 all => [@EXPORT, @EXPORT_OK]
16 );
17 }
18  
19 10 ▶use Scalar::Util qw( refaddr blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Docs {{{1
22  
23 =head1 NAME
24  
25 Build::Hopen::Util::Data - general-purpose data-manipulation functions
26  
27 =head1 FUNCTIONS
28  
29 Nothing is exported by default --- specify C<:all> if you want it all.
30  
31 =cut
32  
33 # }}}1
34  
35 =head2 boolify
36  
37 Convert a scalar to a Boolean as Perl does, except:
38  
39 =over
40  
41 =item * Falsy
42  
43 C</^(false|off|no)$/i>
44  
45 =item * Truthy
46  
47 C<"0">
48  
49 =back
50  
51 So C<false>, C<off>, C<no>, empty string, C<undef>, and numeric C<0> are falsy,
52 and all other values (including string C<'0'>) are truthy.
53  
54 =cut
55  
56 0 ▶sub boolify {
Subroutine boolify: not called CC=3 SCAR=24.8
Pod documented
57 0 ▶ return false if $_[0] =~ /^(false|off|no)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^(false|off|no)$/iu 0 0 0
58 0 ▶ return true if $_[0] =~ /^0$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0$/u 0 0 0
59 0 return !!$_[0];
60 } #boolify()
61  
62 =head2 clone
63  
64 Clones a scalar or a reference. Thin wrapper around L<Storable/dclone>.
65  
66 =cut
67  
68 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=8.1
Pod documented
69 1 my $val = shift;
70 1 ▶ return $val unless ref($val);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 1 0 1
71 0 return Storable::dclone($val);
72 } #clone()
73  
74 =head2 dedent
75  
76 Yet Another routine for dedenting multiline strings. Removes the leading
77 horizontal whitespace on the first nonblank line from all lines. If the first
78 argument is a reference, also trims for use in multiline C<q()>/C<qq()>.
79 Usage:
80  
81 dedent " some\n multiline string";
82 dedent [], q(
83 very indented
84 ); # [] (or any ref) means do the extra trimming.
85  
86 The extra trimming includes:
87  
88 =over
89  
90 =item *
91  
92 Removing the initial C<\n>, if any; and
93  
94 =item *
95  
96 Removing trailing horizontal whitespace between the last C<\n> and the
97 end of the string.
98  
99 =back
100  
101 =cut
102  
103 0 ▶sub dedent {
Subroutine dedent: not called CC=9 SCAR=45.0
Pod documented
104 0 ▶ my $extra_trim = (@_ && ref $_[0]) ? (shift, true) : false;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
105 0 ▶ my $val = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
106 0 my $initial_NL;
107  
108 0 ▶ if($val =~ /\A\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\A\n/u) 0 0 0
109 0 $initial_NL = true;
110 0 $val =~ s/^\A\n//;
111 }
112  
113 0 ▶ if($val =~ m/^(?<ws>\h+)\S/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(?<ws>\h+)\S/mu) 0 0 0
114 0 $val =~ s/^$+{ws}//gm;
115 }
116  
117 0 ▶ $val =~ s/^\h+\z//m if $extra_trim;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_trim 0 0 0
118  
119 0 ▶ return (($initial_NL && !$extra_trim) ? "\n" : '') . $val;
Branch: 1 branch
BranchTrueFalseTotal
$initial_NL && !$extra_trim ? : 0 0 0
Condition0%0/3
$initial_NL and not $extra_trim
!ll&&!rl&&r
000
MC/DC0%0/2
$initial_NL and not $extra_trim
$initial_NL not $extra_trim
Truth table0%0/3
A and not(B)
A= $initial_NLB= not $extra_trim
ABresult
0X0
100
111
120 } #dedent()
121  
122 =head2 forward_opts
123  
124 Returns a list of key-value pairs extracted from a given hashref. Usage:
125  
126 my %forwarded_opts = forward_opts(\%original_opts, [option hashref,]
127 'name'[, 'name2'...]);
128  
129 If the option hashref is given, the following can be provided:
130  
131 =over
132  
133 =item lc
134  
135 If truthy, lower-case the key names in the output
136  
137 =item '-'
138  
139 If present, add C<-> to the beginning of each name in the output.
140 This is useful with L<Build::Hopen::Arrrgs>.
141  
142 =back
143  
144 =cut
145  
146 63 ▶sub forward_opts {
Subroutine forward_opts: called CC=8 SCAR=21.6
Pod documented
147 63 ▶ my $hrIn = shift or croak 'Need an input option set';
Branch: 1 branch
BranchTrueFalseTotal
my $hrIn = shift() or croak("Need an input option set") 63 0 63
148 63 ▶ croak 'Need a hashref' unless ref $hrIn eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hrIn eq "HASH" 0 63 63
149 63 my $hrOpts = {};
150 63 ▶ $hrOpts = shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 63 0 63
151  
152 63 my %result;
153 63 foreach my $name (@_) {
154 125 ▶ next unless exists $hrIn->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hrIn->{$name} 72 53 125
155  
156 53 ▶ my $newname = $hrOpts->{lc} ? lc($name) : $name;
Branch: 1 branch
BranchTrueFalseTotal
$hrOpts->{'lc'} ? : 0 53 53
157 53 ▶ $newname = "-$newname" if $hrOpts->{'-'};
Branch: 1 branch
BranchTrueFalseTotal
if $hrOpts->{"-"} 53 0 53
158 53 $result{$newname} = $hrIn->{$name}
159 }
160  
161 63 return %result;
162 } #forward_opts()
163  
164 =head2 identical
165  
166 Return truthy if the given parameters are identical objects.
167 Taken from L<Test::Identity> by Paul Evans, which is licensed under the same
168 terms as Perl itself.
169  
170 =cut
171  
172 sub _describe
173 0 ▶{
Subroutine _describe: not called CC=4 SCAR=30.0
174 0 my ( $ref ) = @_;
175  
176 0 ▶ if( !defined $ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $ref) { } 0 0 0
elsif (not builtin::refaddr($ref)) { } 0 0 0
elsif (builtin::blessed($ref)) { } 0 0 0
177 0 return "undef";
178 }
179 elsif( !refaddr $ref ) {
180 0 return "a non-reference";
181 }
182 elsif( blessed $ref ) {
183 0 return "a reference to a " . ref( $ref );
184 }
185 else {
186 0 return "an anonymous " . ref( $ref ) . " ref";
187 }
188 } #_describe()
189  
190 sub identical($$)
191 0 ▶{
Subroutine identical: not called CC=4 SCAR=30.0
Pod documented
192 0 my ( $got, $expected ) = @_;
193  
194 0 my $got_desc = _describe $got;
195 0 my $exp_desc = _describe $expected;
196  
197 # TODO: Consider if undef/undef ought to do this...
198 0 ▶ if( $got_desc ne $exp_desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_desc ne $exp_desc) 0 0 0
199 0 return false;
200 }
201  
202 0 ▶ if( !defined $got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got) 0 0 0
203 # Two undefs
204 0 return true;
205 }
206  
207 0 my $got_addr = refaddr $got;
208 0 my $exp_addr = refaddr $expected;
209  
210 0 ▶ if( $got_addr != $exp_addr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_addr != $exp_addr) 0 0 0
211 0 return false;
212 }
213  
214 0 return true;
215 } #identical()
216  
217 10 1;
218 __END__
« Hopen/Arrrgs.pm ↑ Summary Hopen/T/Gnu/C.pm »