blib/lib/Data/Eacherator.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 95.5% 43 / 45 CC 4 SCAR 5.5
CC
4
Cov
94%
eacherator
3.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.7
↑ Summary
1 package Data::Eacherator;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw(@EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 $VERSION = "0.01";
12 1 @EXPORT_OK = qw(eacherator);
13  
14 =head1 NAME
15  
16 Data::Eacherator - simple each-like iterator generator for hashes and arrays
17  
18 =head1 SYNOPSIS
19  
20 my $iter = eacherator($hash_or_array);
21  
22 while (my ($k, $v) = $iter->()) {
23 # ...
24 }
25  
26 =head1 DESCRIPTION
27  
28 This module is designed as a simple drop-in replacement for "each" on
29 those occasions when you need to iterate over a hash I<or> an array.
30  
31 That is, if C<$data> is a hash, and you're happily doing something
32 like:
33  
34 while (my ($k, $v) = each %$data) {
35 # ...
36 }
37  
38 but then decide that you also want to loop over C<$data> in the event
39 that it's an array, you can do:
40  
41 my $iter = eacherator($data);
42  
43 while (my ($k, $v) = $iter->()) {
44 # ...
45 }
46  
47 (You may wish to use this package if, for example, you have a module
48 that happily iterates over a hash, but then discover that you also
49 need to iterate over an "ordered" hash--in this case you can just
50 switch curly brackets to square brackets and use C<eacherator()> to
51 generate a drop-in replacement for each.)
52  
53 =head1 FUNCTIONS
54  
55 =over 4
56  
57 =item $iter_fn = eacherator($hash_or_array_ref)
58  
59 Returns a function (closure) that behaves like "each".
60  
61 =back
62  
63 =head1 PERFORMANCE
64  
65 Not tested; it's probably quite a bit slower than regular "each" on
66 hashes, though.
67  
68 =head1 SEE ALSO
69  
70 If you need something more sophisticated, or something with
71 similar--but different--behaviour, try C<Data::Iter>,
72 C<Data::Iterator>, C<Array::Each> or C<Object::Iterate>. (All of
73 these generate iterators (some with more each-like semantics than
74 others), but none are indifferent as to whether they receive a hash
75 or array.)
76  
77 Depending on what you're trying to do, C<Maptastic> may also be
78 useful.
79  
80 =head1 AUTHOR
81  
82 Michael Stillwell <mjs@beebo.org>
83  
84 =cut
85  
86 7 ▶sub eacherator {
Subroutine eacherator: called CC=3 SCAR=11.0
Pod documented
87 7 my ($data) = @_;
88
89 7 ▶ if (ref($data) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 2 5 7
elsif (ref $data eq 'ARRAY') { } 5 0 5
90 10 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 10 each %$data
92 2 };
93 }
94  
95 elsif (ref($data) eq "ARRAY") {
96 5 my $i = 0;
97 22 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
98 22 ▶ if (@$data >= $i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data >= $i + 2) { } 12 10 22
99 12 $i += 2;
100 12 return ($data->[$i-2], $data->[$i-1])
101 }
102 else {
103 10 $i = 0;
104 10 return ();
105 }
106 5 };
107 }
108
109 0 else { croak "error: can't iterate over something that's not a HASH or ARRAY" }
110 }
111  
112 1 1;
↑ Summary