blib/lib/Hash/StoredIterator.pm

filter: Statementstmt 97.8% 90 / 92 Branchbran 83.3% 15 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 100% 6 / 6 totaltotal 95.3% 124 / 130 SCAR 23.0
CC
10
Cov
95%
__ANON__
4.0
eich
4.0
hmap
3.0
CRAP 10.0
↑ Summary Build »
1 package Hash::StoredIterator;
2  
3 10 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our @EXPORT_OK = qw{
12 hmap
13 eich
14 eech
15 iterator
16 hash_get_iterator
17 hash_set_iterator
18 hash_init_iterator
19 hkeys
20 hvalues
21 };
22  
23 10 our $VERSION = '0.007';
24  
25 10 require XSLoader;
26 10 XSLoader::load( 'Hash::StoredIterator', $VERSION );
27  
28 sub eich(\%\$) {
29 33 ▶ carp "eich is deprecated, you should use iterator() instead";
Subroutine eich: called CC=4 SCAR=13.9
Pod documented
30 33 my ( $hash, $i_ref ) = @_;
31  
32 33 my $old_it = hash_get_iterator($hash);
33  
34 33 my ( $key, $val );
35  
36 33 my $success = eval {
37 33 ▶ if ( !defined $$i_ref )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$i_ref) { } 9 24 33
38 {
39 9 hash_init_iterator($hash);
40 }
41 else {
42 24 hash_set_iterator( $hash, $$i_ref );
43 }
44  
45 33 ( $key, $val ) = each(%$hash);
46  
47 33 $$i_ref = hash_get_iterator($hash);
48  
49 33 1;
50 };
51  
52 33 hash_set_iterator( $hash, $old_it );
53 33 ▶ die $@ unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 33 33
54  
55 33 ▶ unless ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 8 25 33
56 8 $$i_ref = undef;
57 8 return;
58 }
59  
60 25 return ( $key, $val );
61 }
62  
63 sub iterator(\%) {
64 10 ▶ my ($hash) = @_;
Subroutine iterator: called CC=1 SCAR=0.0
Pod documented
65 10 my $i = undef;
66  
67 return sub {
68 42 ▶ my $old_it = hash_get_iterator($hash);
Subroutine __ANON__: called CC=4 SCAR=13.9
69  
70 42 my ( $key, $val );
71  
72 42 my $success = eval {
73 42 ▶ if ( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i) { } 11 31 42
74 11 hash_init_iterator( $hash );
75 }
76 else {
77 31 hash_set_iterator( $hash, $i );
78 }
79  
80 42 ( $key, $val ) = each( %$hash );
81  
82 42 $i = hash_get_iterator($hash);
83  
84 42 1;
85 };
86  
87 42 hash_set_iterator( $hash, $old_it );
88 42 ▶ die $@ unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 42 42
89  
90 42 ▶ unless ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 9 33 42
91 9 $i = undef;
92 9 return;
93 }
94  
95 33 return ( $key, $val );
96 10 };
97 }
98  
99 sub hmap(&\%) {
100 9 ▶ my ( $code, $hash ) = @_;
Subroutine hmap: called CC=3 SCAR=11.0
Pod documented
101  
102 9 my $old_it = hash_get_iterator($hash);
103 9 hash_init_iterator($hash);
104  
105 9 my $success = eval {
106 9 my $iter = iterator %$hash;
107  
108 9 ▶ while ( my ( $k, $v ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 29 8 37
109 29 local $_ = $k;
110 # Can't use caller(), subref might be from a different package than
111 # eech is called from.
112 29 my $callback_package = B::svref_2object($code)->GV->STASH->NAME;
113 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 29 local ${"$callback_package\::a"} = $k;
115 29 local ${"$callback_package\::b"} = $v;
116 29 $code->( $k, $v );
117 }
118  
119 8 1;
120 };
121  
122 9 hash_set_iterator( $hash, $old_it );
123 9 ▶ die $@ unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 1 8 9
124 8 return;
125 }
126  
127 sub eech(&\%) {
128 0 ▶ carp "eech is deprecated, use hmap instead";
Subroutine eech: not called CC=1 SCAR=6.9
Pod documented
129 0 goto &hmap;
130 }
131  
132 sub hkeys(\%) {
133 19 ▶ my ($hash) = @_;
Subroutine hkeys: called CC=2 SCAR=7.0
Pod documented
134 19 ▶ croak "ARGH!" unless $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash 0 19 19
135  
136 19 my $old_it = hash_get_iterator($hash);
137 19 hash_init_iterator($hash);
138  
139 19 my @out = keys %$hash;
140  
141 19 hash_set_iterator( $hash, $old_it );
142  
143 19 return @out;
144 }
145  
146 sub hvalues(\%) {
147 1 ▶ my ($hash) = @_;
Subroutine hvalues: called CC=1 SCAR=0.0
Pod documented
148  
149 1 my $old_it = hash_get_iterator($hash);
150 1 hash_init_iterator($hash);
151  
152 1 my @out = values %$hash;
153  
154 1 hash_set_iterator( $hash, $old_it );
155  
156 1 return @out;
157 }
158  
159 10 1;
160  
161 __END__
↑ Summary Build »