blib/lib/Util/H2O/More.pm

filter: Statementstmt 92.9% 144 / 155 Branchbran 90.0% 18 / 20 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.3% 28 / 31 Podpod 88.2% 15 / 17 totaltotal 89.6% 207 / 231 SCAR 30.6
CC
21
Cov
91%
o2d
6.0
ddd
6.0
dddie
6.0
CRAP 21.3
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Util::H2O::More;
5 10 ▶use parent q/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 our $VERSION = q{0.3.2};
8  
9 10 our @EXPORT_OK = (qw/baptise opt2h2o h2o o2h d2o o2d o2h2o ini2h2o ini2o h2o2ini o2ini Getopt2h2o ddd dddie tr4h2o yaml2o/);
10  
11 10 ▶use Util::H2O ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # quick hack to export h2o, uses proper
16 # Util::H2O::h2o called with full namespace
17 sub h2o {
18 64 ▶ return Util::H2O::h2o @_;
Subroutine h2o: called CC=1 SCAR=0.0
Pod documented
19 }
20  
21 # maintains basically a count to create non-colliding
22 # unique $pkg names (basically what Util::H2O::h2o does
23 # if $pkg is not specified using -class
24 # monatomically increasing uuid
25 sub _uuid {
26 9 ▶ state $uuid = 0;
Subroutine _uuid: called CC=1 SCAR=0.0
27 9 return ++$uuid;
28 }
29  
30 # non-recursive option
31 sub baptise ($$@) {
32 9 ▶ my ( $ref, $pkg, @default_accessors );
Subroutine baptise: called CC=3 SCAR=11.0
Pod documented
33 9 my $pos0 = shift;
34  
35 # check pos0 for '-recurse'
36 9 ▶ if ( $pos0 eq q{-recurse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos0 eq '-recurse') { } 7 2 9
37 7 ( $ref, $pkg, @default_accessors ) = @_;
38 }
39 else {
40 2 $ref = $pos0;
41 2 ( $pkg, @default_accessors ) = @_;
42 }
43  
44 9 my $self;
45 9 my $real_pkg = sprintf qq{%s::_%s}, $pkg, _uuid;
46  
47 # uses -isa to inherit from $pkg; -class to bless with a package name
48 # derived from $pkg
49 9 ▶ if ( $pos0 eq q{-recurse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos0 eq '-recurse') { } 7 2 9
50 7 $self = h2o -recurse, -isa => $pkg, -class => $real_pkg, $ref, @default_accessors;
51 }
52 else {
53 2 $self = h2o -isa => $pkg, -class => $real_pkg, $ref, @default_accessors;
54 }
55  
56 9 return $self;
57 }
58  
59 # make keys legal for use as accessor, provides original keys via "__og_keys" accessor
60 sub tr4h2o($) {
61 1 ▶ my $hash_ref = shift;
Subroutine tr4h2o: called CC=2 SCAR=6.9
Pod documented
62 1 my $new_hashref = {};
63  
64 # List::Util::pairmap was not happy being require'd for some reason
65 # so iterate and replace keys explicitly; store original key in resulting
66 # hashref via __og_keys
67 1 foreach my $og_k ( keys %$hash_ref ) {
68 2 my $k = $og_k;
69 2 $k =~ tr/a-zA-Z0-9/_/c;
70 2 $new_hashref->{$k} = $hash_ref->{$og_k};
71  
72 # save old key via __og_keys
73 2 $new_hashref->{__og_keys}->{$k} = $og_k;
74 }
75 1 return $new_hashref;
76 }
77  
78 # preconditioner for use with Getopt::Long flags; returns just the flag name given
79 # a list of option descriptors, e.g., qw/option1=s option2=i option3/;
80  
81 # Getopt to keys
82 sub opt2h2o(@) {
83 2 ▶ my @getopt_def = @_;
Subroutine opt2h2o: called CC=1 SCAR=0.0
Pod documented
84 2 my @flags_only = map { m/([^=|\s]+)/g; $1 } @getopt_def;
85 2 return @flags_only;
86 }
87  
88 # wrapper around opt2h2o (yeah!)
89 sub Getopt2h2o(@) {
90 1 ▶ my ( $ARGV_ref, $defaults, @opts ) = @_;
Subroutine Getopt2h2o: called CC=2 SCAR=7.0
Pod documented
91 1 ▶ $defaults //= {};
Condition50%1/2
$defaults //= {}
l!l
10
MC/DC0%0/1
$defaults //= {}
$defaults
Truth table50%1/2
A
A= $defaults
Aresult
11
00
92 1 my $o = h2o $defaults, opt2h2o(@opts);
93 1 require Getopt::Long;
94 1 Getopt::Long::GetOptionsFromArray( $ARGV_ref, $o, @opts ); # Note, @ARGV is passed by reference
95 1 return $o;
96 }
97  
98 # general form of method used to give accessors to Config::Tiny in Util::H2O's
99 # POD documentation
100 sub o2h2o($) {
101 6 ▶ my $ref = shift;
Subroutine o2h2o: called CC=1 SCAR=0.0
Pod documented
102 6 return h2o -recurse, { %{$ref} };
103 }
104  
105 # more specific helper app that uses Config::Tiny->read and o2h2o to get a config
106 # object back from an .ini; requries Config::Tiny
107 sub ini2h2o($) {
108 4 ▶ my $filename = shift;
Subroutine ini2h2o: called CC=1 SCAR=0.0
Pod documented
109 4 require Config::Tiny;
110 4 return o2h2o( Config::Tiny->read($filename) );
111 }
112  
113 # back compat
114 sub ini2o($) {
115 2 ▶ return ini2h2o(shift);
Subroutine ini2o: called CC=1 SCAR=0.0
Pod undocumented
116 }
117  
118 # write out the INI file
119 sub h2o2ini($$) {
120 2 ▶ my ( $config, $filename ) = @_;
Subroutine h2o2ini: called CC=1 SCAR=0.0
Pod documented
121 2 require Config::Tiny;
122 2 return Config::Tiny->new( Util::H2O::o2h $config)->write($filename);
123 }
124  
125 # back compat
126 sub o2ini($$) {
127 1 ▶ return h2o2ini( shift, shift );
Subroutine o2ini: called CC=1 SCAR=0.0
Pod undocumented
128 }
129  
130 # return a dereferences hash (non-recursive); reverse of `h2o'
131 sub o2h($) {
132 18 ▶ $Util::H2O::_PACKAGE_REGEX = qr/::_[0-9A-Fa-f]+\z/; # makes internal package name more generic for baptise created references
Subroutine o2h: called CC=2 SCAR=7.4
Pod documented
133 18 my $ref = Util::H2O::o2h @_;
134 18 ▶ if ( ref $ref ne q{HASH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref ne "HASH") 0 18 18
135 0 die qq{Could not fully remove top-level reference. Probably an issue with \$Util::H2O_PACKAGE_REGEX\n};
136 }
137 18 return $ref;
138 }
139  
140 sub d2o($); # forward declaration to get rid of "too early" warning
141 sub a2o($);
142  
143 sub d2o($) {
144 171 ▶ my $thing = shift;
Subroutine d2o: called CC=5 SCAR=16.1
Pod documented
145 171 my $isa = ref $thing;
146 171 ▶ if ( $isa eq q{ARRAY} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isa eq 'ARRAY') { } 24 147 171
elsif ($isa eq 'HASH') { } 37 110 147
147 24 a2o $thing;
148 24 foreach my $element (@$thing) {
149 76 d2o $element;
150 }
151 }
152 elsif ( $isa eq q{HASH} ) {
153 37 foreach my $keys ( keys %$thing ) {
154 74 d2o( $thing->{$keys} );
155 }
156  
157 # package level wrapper, so this can be monkey patched
158 # if so desired, per documentation
159 37 h2o $thing;
160 }
161 171 return $thing;
162 }
163  
164 # blesses ARRAY ref as a container and gives it some virtual methods
165 # useful in the context of containing HASH refs that get objectified
166 # by h2o
167 sub a2o($) {
168 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 24 ▶ my $array_ref = shift;
Subroutine a2o: called CC=1 SCAR=0.0
Pod documented
171  
172 # uses lexical scop of the 'if' to a bless $array_ref (an ARRAY ref)
173 # and assigns to it some virtual methods for making dealing with
174 # the "lists of C<HASH> references easier, as a container
175  
176 24 my $a2o_pkg = sprintf( qq{%s::__a2o_%d::vmethods}, __PACKAGE__, int rand 100_000_000 ); # internal a2o
177  
178 24 bless $array_ref, $a2o_pkg;
179  
180 ## add vmethod to wrap around array_refs
181  
182 # return item at index INDEX
183 24 ▶ my $GET = sub { my ( $self, $i ) = @_; return $self->[$i]; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
184 24 *{"${a2o_pkg}::get"} = $GET;
185  
186 # return rereferenced ARRAY
187 24 ▶ my $ALL = sub { my $self = shift; return @$self; };
Subroutine __ANON__: called CC=1 SCAR=0.0
188 24 *{"${a2o_pkg}::all"} = $ALL;
189  
190 # returns value returned by the 'scalar' keyword
191 24 ▶ my $SCALAR = sub { my $self = shift; return scalar @$self; };
Subroutine __ANON__: called CC=1 SCAR=0.0
192 24 *{"${a2o_pkg}::scalar"} = $SCALAR;
193  
194 # 'push' will apply "d2o" to all elements pushed
195 24 ▶ my $PUSH = sub { my ( $self, @i ) = @_; d2o \@i; push @$self, @i; return \@i };
Subroutine __ANON__: called CC=1 SCAR=0.0
196 24 *{"${a2o_pkg}::push"} = $PUSH;
197  
198 # 'pop' intentionally does NOT apply "o2d" to anyarray_ref pop'd
199 24 ▶ my $POP = sub { my $self = shift; return pop @$self };
Subroutine __ANON__: called CC=1 SCAR=0.0
200 24 *{"${a2o_pkg}::pop"} = $POP;
201  
202 # 'unshift' will apply "d2o" to all elements unshifted
203 24 ▶ my $UNSHIFT = sub { my ( $self, @i ) = @_; d2o \@i; unshift @$self, @i; return \@i };
Subroutine __ANON__: called CC=1 SCAR=0.0
204 24 *{"${a2o_pkg}::unshift"} = $UNSHIFT;
205  
206 # 'shift' intentionally does NOT apply "o2d" to anyarray_ref shift'd
207 24 ▶ my $SHIFT = sub { my $self = shift; return shift @$self };
Subroutine __ANON__: called CC=1 SCAR=0.0
208 24 *{"${a2o_pkg}::shift"} = $SHIFT;
209  
210 24 return $array_ref;
211 }
212  
213 # includes internal dereferencing so to be compatible
214 # with the behavior of Util::H2O::o2h
215 sub o2d($); # forward declaration to get rid of "too early" warning
216  
217 sub o2d($) {
218 31 ▶ my $thing = shift;
Subroutine o2d: called CC=6 SCAR=17.9
Pod documented
219 31 ▶ return $thing if not $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless $thing 1 30 31
220 30 my $isa = ref $thing;
221 30 ▶ if ( $isa =~ m/^Util::H2O::More::__a2o/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isa =~ /^Util::H2O::More::__a2o/) { } 4 26 30
elsif ($isa =~ /^Util::H2O::_/) { } 3 23 26
222 4 my @_thing = @$thing;
223 4 $thing = \@_thing;
224 4 foreach my $element (@$thing) {
225 22 $element = o2d $element;
226 }
227 }
228 elsif ( $isa =~ m/^Util::H2O::_/ ) {
229 3 foreach my $key ( keys %$thing ) {
230 6 $thing->{$key} = o2d $thing->{$key};
231 }
232 3 $thing = Util::H2O::o2h $thing;
233 }
234 30 return Util::H2O::o2h $thing;
235 }
236  
237 # handy, poor man's debug wrappers
238  
239 sub ddd(@) {
240 0 ▶ require Data::Dumper;
Subroutine ddd: not called CC=2 SCAR=17.9
Pod documented
241 0 foreach my $ref (@_) {
242 0 print STDERR Data::Dumper::Dumper($ref);
243 }
244 }
245  
246 sub dddie(@) {
247 0 ▶ require Data::Dumper;
Subroutine dddie: not called CC=2 SCAR=17.9
Pod documented
248 0 foreach my $ref (@_) {
249 0 print STDERR Data::Dumper::Dumper($ref);
250 }
251 0 die qq{died due to use of dddie};
252 }
253  
254 # YAML configuration support - may return more than 1 reference
255 sub yaml2o($) {
256 3 ▶ require YAML;
Subroutine yaml2o: called CC=5 SCAR=16.6
Pod documented
257 3 my $file_or_yaml = shift; # may be a file or a string
258 3 my @yaml = (); # yaml can have multiple objects serialized, via ---
259  
260 # determine if YAML or file name
261 3 my @lines = split /\n/, $file_or_yaml;
262  
263 # if a file, use YAML::LoadFile
264 3 ▶ if ( @lines == 1 and -e $file_or_yaml ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@lines == 1 and -e $file_or_yaml) { } 0 3 3
elsif ($lines[0] eq '---') { } 2 1 3
Condition33%1/3
@lines == 1 and -e $file_or_yaml
!ll&&!rl&&r
300
MC/DC0%0/2
@lines == 1 and -e $file_or_yaml
@lines == 1 -e $file_or_yaml
Truth table33%1/3
A and B
A= @lines == 1B= -e $file_or_yaml
ABresult
0X0
100
111
265 0 @yaml = YAML::LoadFile($file_or_yaml);
266 }
267  
268 # if not a file, assume YAML string and use YAML::Load
269 elsif ($lines[0] eq q{---}) {
270 2 @yaml = YAML::Load($file_or_yaml);
271 }
272  
273 # die because not supported content $file_or_yaml - it is neither
274 else {
275 1 die qq{Provided parameter looks like neither a file name nor a valid YAML snippet.\n};
276 }
277  
278 # iterate over 1 or more serialized objects that were deserialized
279 # from the YAML, applie C<d2o> to it due to the potential presence
280 # of ARRAY references
281 2 my @obs = ();
282 2 foreach my $y (@yaml) {
283 4 push @obs, d2o $y;
284 }
285  
286 2 return @obs;
287 }
288  
289 # NOTE: no o2yaml, but can add one if somebody needs it ... please file an issue on the tracker (GH these days)
290  
291 10 1;
292  
293 __END__
↑ Summary