lib/Hash/MostUtils.pm

filter: Statementstmt 98.9% 96 / 97 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 26 / 26 Podpod 100% 10 / 10 totaltotal 97.9% 140 / 143 SCAR 20.8
CC
8
Cov
97%
__ANON__
5.0
__ANON__
2.1
__ANON__
2.0
CRAP 8.0
« blib/lib/Hash/MostUtils/leach.pm ↑ Summary blib/lib/Hash/MostUtils/leach/v5_10.pm »
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 package Hash::MostUtils;
4 10 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Hash::MostUtils::leach qw(n_each leach);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our $VERSION = 1.05;
10  
11 10 our @EXPORT_OK = qw(
12 lvalues
13 lkeys
14 leach
15 hash_slice_of
16 hash_slice_by
17 hashmap
18 hashgrep
19 hashapply
20 n_each
21 n_map
22 n_grep
23 n_apply
24 reindex
25 rekey
26 revalue
27 );
28  
29 # decrementing $| flips it between 0 and 1
30 3 ▶sub lkeys { local $|; return grep { $|-- == 0 } @_ }
Subroutine lkeys: called CC=1 SCAR=0.0
Pod documented
31 3 ▶sub lvalues { local $|; return grep { $|-- == 1 } @_ }
Subroutine lvalues: called CC=1 SCAR=0.0
Pod documented
32  
33 # I would put leach() here, but it was imported above
34  
35 4 ▶*hashmap = sub(&@) { unshift @_, 2; goto &n_map };
Subroutine __ANON__: called CC=1 SCAR=0.0
36 3 ▶*hashgrep = sub(&@) { unshift @_, 2; goto &n_grep };
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶*hashapply = sub (&@) { unshift @_, 2; goto &n_apply };
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 # I would put n_each() here, but it was imported above
40  
41 sub n_map ($&@) {
42 # Usually I don't mutate @_. Here I deliberately modify @_ for the upcoming non-obvious goto-&NAME.
43 13 ▶ my $n = shift;
Subroutine n_map: called CC=1 SCAR=0.0
Pod documented
44 13 ▶ my $collector = sub { return $_[0]->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 13 unshift @_, $collector;
46  
47 # Using a "safe goto" allows n_map() to remove itself from the callstack, which allows _n_collect()
48 # to see the correct caller.
49 #
50 # 'perldoc -f goto' for why this is a safe goto.
51 13 goto &{_n_collect($n)};
52 }
53  
54 sub n_grep ($&@) {
55 7 ▶ my $n = shift;
Subroutine n_grep: called CC=1 SCAR=0.0
Pod documented
56  
57 # the comments in n_map() apply here as well.
58  
59 my $collector = sub {
60 32 ▶ my ($code, $vals, $aliases) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
61 32 ▶ return $code->() ? @$vals : ();
Branch: 1 branch
BranchTrueFalseTotal
&$code() ? : 25 7 32
62 7 };
63 7 unshift @_, $collector;
64  
65 7 goto &{_n_collect($n)};
66 }
67  
68 sub n_apply {
69 3 ▶ my $n = shift;
Subroutine n_apply: called CC=1 SCAR=0.0
Pod documented
70 my $collector = sub {
71 9 ▶ my ($code, $vals, $aliases) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
72 9 $code->();
73 9 return map { $$_ } @$aliases;
74 3 };
75 3 unshift @_, $collector;
76  
77 3 goto &{_n_collect($n)};
78 }
79  
80 sub _n_collect($) {
81 23 ▶ my ($n) = @_;
Subroutine _n_collect: called CC=1 SCAR=0.0
82 return sub(&@) {
83 23 ▶ my $collector = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
84 23 my $code = shift;
85 23 ▶ if (@_ % $n != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % $n != 0) 0 23 23
86 0 confess("your input is insane: can't evenly slice " . @_ . " elements into $n-sized chunks\n");
87 }
88  
89 # these'll reserve some namespace back in the callpackage
90 23 my @n = ('a' .. 'z');
91  
92 # stash old values back in callpackage *and* in main. If called from main::, this comes down to:
93 # local ${'main::a'}, ${'main::b'}, ${'main::c'}
94 # when $n is 3.
95 23 my $caller = caller;
96 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 23 foreach ((@n[ 0 .. $n-1 ])) {
98 71 local ${"$caller\::$_"};
99 71 local ${"::$_"};
100 }
101  
102 23 my @out;
103 23 ▶ while (my @chunk = splice @_, 0, $n) { # build up each set...
Branch: 1 branch
BranchTrueFalseTotal
if (my(@chunk) = splice(@_, 0, $n)) 95 23 118
104 95 my @aliases;
105 95 foreach (0 .. $#chunk) {
106 # ...assign values from @_ back to localized variables in $caller *and* in 'main::'.
107 # Aliasing in main:: allows you to refer to variables $c and onwards as $::c.
108 # Aliasing in $caller allows you to refer to variables $c and onwards as $whatever::package::c.
109 237 ${"::$n[$_]"} = ${"$caller\::$n[$_]"} = $chunk[$_];
110  
111 # Keep a reference to $::a (etc.) and pass them in to the $collector; this allows $code to mutate
112 # $::a (etc) and signal the changed values back to $collector.
113 237 push @aliases, \${"::$n[$_]"};
114 }
115 95 push @out, $collector->($code, \@chunk, \@aliases); # ...and apply $code.
116 }
117  
118 23 return @out;
119 23 };
120 }
121  
122 sub hash_slice_of {
123 4 ▶ my ($ref, @keys) = @_;
Subroutine hash_slice_of: called CC=1 SCAR=0.0
Pod documented
124 4 return map { ($_ => $ref->{$_}) } @keys;
125 }
126  
127 sub hash_slice_by {
128 2 ▶ my ($obj, @methods) = @_;
Subroutine hash_slice_by: called CC=1 SCAR=0.0
Pod documented
129 2 return map { ($_ => scalar($obj->$_)) } @methods;
130 }
131  
132 sub rekey (&@) {
133 3 ▶ my %map = shift()->();
Subroutine rekey: called CC=1 SCAR=0.0
Pod documented
134 3 ▶ return n_map 2, sub { $map{$a} || $a => $b }, @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$map{$a} or $a 8 8 16
135 }
136  
137 sub reindex (&@) {
138 1 ▶ my %map = shift()->();
Subroutine reindex: called CC=1 SCAR=0.0
Pod documented
139 1 @_[values %map] = delete @_[keys %map];
140 1 return @_;
141 }
142  
143 sub revalue (&@) {
144 1 ▶ my %map = shift()->();
Subroutine revalue: called CC=1 SCAR=0.1
Pod documented
145 1 ▶ return n_map 2, sub { $a => $map{$b} || $b }, @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$map{$b} or $b 0 2 2
146 }
147  
148 10 1;
149  
150 __END__
« blib/lib/Hash/MostUtils/leach.pm ↑ Summary blib/lib/Hash/MostUtils/leach/v5_10.pm »