Hopen/Util/Data.pm

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