Handy/Util.pm

filter: Statementstmt 67.5% 73 / 108 Branchbran 54.3% 25 / 46 Conditioncond 38.5% 27 / 70 MC/DCmcdc 21.0% 8 / 38 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 2 totaltotal 51.7% 146 / 282 SCAR 57.2
CC
54
Cov
56%
expand_xtags_
306.0
_xtag_group
40.2
__ANON__
6.0
CRAP 305.7
↑ Summary Handy.pm »
1 package Exporter::Handy::Util;
2  
3 # ABSTRACT: Routines useful when exporting symbols thru Exporter and friends
4 1 our $VERSION = '1.000002';
5  
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Automatically fall back to pure perl for older versions of List::Util (circa Perl version < 5.20)
11 1 ▶use List::Util::MaybeXS qw( pairs unpairs uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Exporter::Extensible -exporter_setup => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 export(qw(
15 =xtags
16 =expand_xtags
17 ));
18  
19  
20 # Generators for exported functions
21 sub _generate_xtags {
22 2 ▶ my ($exporter, $symbol, $opts) = @_;
Subroutine _generate_xtags: called CC=1 SCAR=0.0
23 6 ▶ sub {; xtags_($opts // {}, @_ ) } # curried
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$opts // {}
l!l
33
MC/DC100%1/1
$opts // {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
24 2 }
25  
26 sub _generate_expand_xtags {
27 0 ▶ my ($exporter, $symbol, $opts) = @_;
Subroutine _generate_expand_xtags: not called CC=1 SCAR=6.9
28 0 ▶ sub {; expand_xtags_(@_, $opts // { }) } # curried
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
29 0 }
30  
31  
32  
33  
34 sub xtags_ { # useful for building export tags
35 # say STDERR 'xtag ARGS: ' . np(@_);
36  
37 6 ▶ my %opt; %opt = ( %opt, %{; shift } ) while _is_plain_hashref($_[0]); # merge options given by any leading hash-refs
Subroutine xtags_: called CC=5 SCAR=16.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
_is_plain_hashref($_[0]) and do { %opt = (%opt, %{shift();}) } 6 6 12
38 6 my @res;
39  
40  
41 6 for (pairs @_) {
42 6 my ($k ,$v) = @$_;
43  
44 6 ▶ if ( ref($v) =~ /^HASH$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($v) =~ /^HASH$/) { } 4 2 6
45 4 push @res, _xtag_group( \%opt, $k => $v );
46 } else {
47 2 push @res, _xtag_group( \%opt, '' => { $k => $v } );
48 }
49 }
50 6 ▶ wantarray ? @res : \@res; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
51 }
52  
53 sub _xtag_group {
54 # say STDERR '_xtag_group ARGS: ' . np(@_);
55  
56 # options may be given by one or more leading hash-refs (that we merge)
57 8 ▶ my %opt; %opt = ( %opt, %{; shift } ) while _is_plain_hashref($_[0]);
Subroutine _xtag_group: called CC=29 SCAR=36.9
Branch: 1 branch
BranchTrueFalseTotal
_is_plain_hashref($_[0]) and do { %opt = (%opt, %{shift();}) } 8 8 16
58  
59 8 ▶ my $group = ( @_ && !ref( $_[0] ) ) ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && !ref($_[0]) ? : 8 0 8
Condition33%1/3
@_ and not ref $_[0]
!ll&&!rl&&r
008
MC/DC0%0/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
60 8 my %items = %{; shift };
61 8 ▶ %opt = ( %opt, %{; delete $items{'%'} // {} } );
Condition50%1/2
delete $items{"%"} // {}
l!l
08
MC/DC0%0/1
delete $items{"%"} // {}
delete $items{"%"}
Truth table50%1/2
A
A= delete $items{"%"}
Aresult
11
00
62  
63 8 ▶ $group = $group // delete $opt{group} // delete $opt{name} // '';
Condition37%3/8
(($group // delete $opt{"group"}) // delete $opt{"name"}) // ""
l!l
80
($group // delete $opt{"group"}) // delete $opt{"name"}
l!l&&r!l&&!r
800
$group // delete $opt{"group"}
l!l&&r!l&&!r
800
MC/DC0%0/3
(($group // delete $opt{"group"}) // delete $opt{"name"}) // ""
$group delete $opt{"group"} delete $opt{"name"}
Truth table25%1/4
A // B // C
A= $groupB= delete $opt{"group"}C= delete $opt{"name"}
ABCresult
1XX1
01X1
0011
0000
64 8 $group =~ s/^([:])//;
65  
66 8 my %subopt= %opt;
67 8 ▶ my $sig = delete $opt{sig} // $1 // ''; # like a sigil... It's typically either ':' or empty string.
Condition100%5/5
(delete $opt{"sig"} // $1) // ""
l!l
53
delete $opt{"sig"} // $1
l!l&&r!l&&!r
413
MC/DC100%2/2
(delete $opt{"sig"} // $1) // ""
delete $opt{"sig"} $1
Truth table100%3/3
A // B
A= delete $opt{"sig"}B= $1
ABresult
1X1
011
000
68 8 ▶ my $sep = delete $opt{sep} // '_';
Condition50%1/2
delete $opt{"sep"} // "_"
l!l
08
MC/DC0%0/1
delete $opt{"sep"} // "_"
delete $opt{"sep"}
Truth table50%1/2
A
A= delete $opt{"sep"}
Aresult
11
00
69 8 ▶ my $nogroup = delete $opt{nogroup} // 0;
Condition50%1/2
delete $opt{"nogroup"} // 0
l!l
08
MC/DC0%0/1
delete $opt{"nogroup"} // 0
delete $opt{"nogroup"}
Truth table50%1/2
A
A= delete $opt{"nogroup"}
Aresult
11
00
70  
71 8 ▶ my @pfx = _flat( delete $opt{pfx} // ( $group ? "${group}${sep}" : "" ));
Branch: 1 branch
BranchTrueFalseTotal
$group ? : 6 2 8
Condition66%2/3
delete $opt{"pfx"} // ($group ? "$group$sep" : "")
l!l&&r!l&&!r
062
MC/DC50%1/2
delete $opt{"pfx"} // ($group ? "$group$sep" : "")
delete $opt{"pfx"} ($group ? "$group$sep" : "")
Truth table66%2/3
A // B
A= delete $opt{"pfx"}B= ($group ? "$group$sep" : "")
ABresult
1X1
011
000
72  
73 8 my %tags;
74 8 for my $pfx (@pfx) {
75 8 ▶ $pfx = $sig . $pfx if $sig && ($pfx !~ /^\Q$sig\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $sig and not $pfx =~ /^\Q$sig\E/ 4 4 8
Condition66%2/3
$sig and not $pfx =~ /^\Q$sig\E/
!ll&&!rl&&r
404
MC/DC50%1/2
$sig and not $pfx =~ /^\Q$sig\E/
$sig not $pfx =~ /^\Q$sig\E/
Truth table66%2/3
A and not(B)
A= $sigB= not $pfx =~ /^\Q$sig\E/
ABresult
0X0
100
111
76  
77 8 for my $k (sort keys %items) {
78 8 my $v = $items{$k};
79 8 $k =~ s/^\Q$sig\E//;
80 8 my $key = "${pfx}${k}";
81 8 ▶ my %subtags = ( ref($v) =~ /^HASH$/ ) ? ( _xtag_group(\%subopt, $key => $v) ) : ( $key => $v);
Branch: 1 branch
BranchTrueFalseTotal
ref($v) =~ /^HASH$/ ? : 2 6 8
82 8 %tags = (%tags, %subtags);
83 }
84 }
85 # umbrella entry (that encompasses all subtags)
86 8 ▶ if (!$nogroup && defined $group && $group) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $nogroup and defined $group and $group) 6 2 8
Condition50%3/6
not $nogroup and defined $group and $group
!ll&&!rl&&r
026
not $nogroup and defined $group
!ll&&!rl&&r
008
MC/DC33%1/3
not $nogroup and defined $group and $group
not $nogroup defined $group $group
Truth table50%2/4
not(A) and B and C
A= not $nogroupB= defined $groupC= $group
ABCresult
0XX0
10X0
1100
1111
87 6 my $g = $group;
88 6 ▶ $g = $sig . $g if $sig && ($g !~ /^\Q$sig\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $sig and not $g =~ /^\Q$sig\E/ 3 3 6
Condition66%2/3
$sig and not $g =~ /^\Q$sig\E/
!ll&&!rl&&r
303
MC/DC50%1/2
$sig and not $g =~ /^\Q$sig\E/
$sig not $g =~ /^\Q$sig\E/
Truth table66%2/3
A and not(B)
A= $sigB= not $g =~ /^\Q$sig\E/
ABresult
0X0
100
111
89 $tags{$g} = [
90 6 map {;
91 8 my $item = $_;
92 8 ▶ $item = ':' . $_ if defined $_ && $_ && !m/^[:]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and $_ and not /^[:]/ 4 4 8
Condition50%3/6
defined $_ and $_ and not /^[:]/
!ll&&!rl&&r
044
defined $_ and $_
!ll&&!rl&&r
008
MC/DC33%1/3
defined $_ and $_ and not /^[:]/
defined $_ $_ not /^[:]/
Truth table50%2/4
A and B and not(C)
A= defined $_B= $_C= not /^[:]/
ABCresult
0XX0
10X0
1100
1111
93 8 ▶ $item // ()
Condition33%1/3
$item // ()
l!l&&r!l&&!r
800
MC/DC0%0/2
$item // ()
$item ()
Truth table33%1/3
A // B
A= $itemB= ()
ABresult
1X1
011
000
94 } ( sort keys %tags ) ]
95 }
96  
97 8 my @tags = _kv_sort(%tags); # sort on keys
98 8 ▶ wantarray ? @tags : \@tags; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 0 8
99 }
100  
101  
102  
103 sub expand_xtags_ {
104 0 ▶ local $_;
Subroutine expand_xtags_: not called CC=17 SCAR=57.2
Pod undocumented
105 0 ▶ my %tags; %tags = ( %tags, %{; shift } ) while _is_plain_hashref($_[0]); # tags at start
Branch: 1 branch
BranchTrueFalseTotal
_is_plain_hashref($_[0]) and do { %tags = (%tags, %{shift();}) } 0 0 0
106 0 ▶ my %opt; %opt = ( %opt, %{; pop } ) while _is_plain_hashref($_[-1]); # options at the end.
Branch: 1 branch
BranchTrueFalseTotal
_is_plain_hashref($_[-1]) and do { %opt = (%opt, %{pop();}) } 0 0 0
107  
108 # Handle special requests given via options
109 0 ▶ my @keys = _flat( delete $opt{key} // (), delete $opt{keys} // () );
Condition0%0/6
delete $opt{"key"} // ()
l!l&&r!l&&!r
000
delete $opt{"keys"} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opt{"key"} // ()
delete $opt{"key"} ()
MC/DC0%0/2
delete $opt{"keys"} // ()
delete $opt{"keys"} ()
Truth table0%0/3
A // B
A= delete $opt{"key"}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= delete $opt{"keys"}B= ()
ABresult
1X1
011
000
110 0 for (@keys) {
111 0 ▶ if (_is_plain_scalarref($_) and ($$_ =~ /[*]|ALL/i ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_plain_scalarref($_) and $$_ =~ /[*]|ALL/i) 0 0 0
Condition0%0/3
_is_plain_scalarref($_) and $$_ =~ /[*]|ALL/i
!ll&&!rl&&r
000
MC/DC0%0/2
_is_plain_scalarref($_) and $$_ =~ /[*]|ALL/i
_is_plain_scalarref($_) $$_ =~ /[*]|ALL/i
Truth table0%0/3
A and B
A= _is_plain_scalarref($_)B= $$_ =~ /[*]|ALL/i
ABresult
0X0
100
111
112 # A scalar ref indicates special handling!
113 # If it deferences to '*' (or 'ALL'), it means "ALL KEYS".
114 0 push @_, values %tags;
115 next
116 0 }
117 0 push @_, $tags{$_};
118 }
119 0 @_ = uniq(@_);
120  
121 0 my %seen;
122 my @res;
123  
124 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
125 0 $_ = shift;
126 0 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
127 0 ▶ ref($_) eq 'ARRAY' and do { unshift @_, @$_; next };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" and do { unshift @_, @$_; next } 0 0 0
128  
129 0 ▶ next if exists $seen{$_} && ( $seen{$_} // 0 );
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$_} and $seen{$_} // 0 0 0 0
Condition0%0/5
exists $seen{$_} and $seen{$_} // 0
!ll&&!rl&&r
000
$seen{$_} // 0
l!l
00
MC/DC0%0/2
exists $seen{$_} and $seen{$_} // 0
exists $seen{$_} $seen{$_}
Truth table0%0/3
A and B
A= exists $seen{$_}B= $seen{$_}
ABresult
0X0
100
111
130 0 $seen{$_} = 1;
131  
132 0 ▶ m/^([:](.*))$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^([:](.*))$/ and do { unshift @_, delete $tags{$1} // (), delete $tags{$2} // (); next } 0 0 0
133 0 ▶ unshift @_, delete $tags{$1} // (), delete $tags{$2} // ();
Condition0%0/6
delete $tags{$1} // ()
l!l&&r!l&&!r
000
delete $tags{$2} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $tags{$1} // ()
delete $tags{$1} ()
MC/DC0%0/2
delete $tags{$2} // ()
delete $tags{$2} ()
Truth table0%0/3
A // B
A= delete $tags{$1}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= delete $tags{$2}B= ()
ABresult
1X1
011
000
134 0 next;
135 };
136 0 push @res, $_;
137 }
138 @res
139 0 }
140  
141  
142 # PRIVATE routines
143 # ref
144 8 ▶sub _is_plain_arrayref { ref( $_[0] ) eq 'ARRAY' }
Subroutine _is_plain_arrayref: called CC=1 SCAR=0.0
145 28 ▶sub _is_plain_hashref { ref( $_[0] ) eq 'HASH' }
Subroutine _is_plain_hashref: called CC=1 SCAR=0.0
146 0 ▶sub _is_plain_scalarref { ref( $_[0] ) eq 'SCALAR' }
Subroutine _is_plain_scalarref: not called CC=1 SCAR=6.9
147 0 ▶sub _is_plain_scalar { !ref( $_[0] ) }
Subroutine _is_plain_scalar: not called CC=1 SCAR=6.9
148  
149 # List
150 sub _flat { # shamelessly copied from: [List::_flat](https://metacpan.org/pod/List::_flat)
151 8 ▶ my @results;
Subroutine _flat: called CC=4 SCAR=15.0
152  
153 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 8 16
154 8 ▶ if ( _is_plain_arrayref( my $element = shift @_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_plain_arrayref(my $element = shift @_)) { } 0 8 8
155 0 unshift @_, @{$element};
156 }
157 else {
158 8 push @results, $element;
159 }
160 }
161 8 ▶ return wantarray ? @results : \@results; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 0 8
162 }
163  
164 sub _kv_sort {
165 8 ▶ unpairs sort { $a->[0] cmp $b->[0] } pairs(@_)
Subroutine _kv_sort: called CC=1 SCAR=0.0
166 }
167  
168 1 1;
169  
170 __END__
↑ Summary Handy.pm »