MrGamoo/Iter.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 51 CC 6 SCAR 19.3
CC
6
Cov
0%
_next
12.0
key
6.0
next
6.0
CRAP 6.9
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Nov-10 11:48 (EST)
6 # Function: iterator for reducing
7 #
8 # $Id: Iter.pm,v 1.1 2010/11/01 18:41:42 jaw Exp $
9  
10 package AC::MrGamoo::Iter;
11 use strict;
12  
13 sub key {
14 my $me = shift;
15  
16 # move ahead to 1st row of next key
17 while( defined(my $r = $me->_nextrow()) ){
18 next if $r->[0] eq $me->{key};
19  
20 $me->_putback($r);
21 $me->{key} = $r->[0];
22 return $r->[0];
23 }
24  
25 return; # eof
26 }
27  
28 sub next {
29  
30 my($data, $end) = _next(@_);
31 if( wantarray ){
32 return ($data, $end);
33 }else{
34 return $data;
35 }
36 }
37  
38 sub _next {
39 my $me = shift;
40  
41 my $r = $me->_nextrow();
42 return (undef, 1) unless $r; # eof
43  
44 return $r->[1] if $me->{key} eq $r->[0];
45  
46 # end of key
47 $me->_putback($r);
48 return (undef, 1);
49 }
50  
51 sub foreach {
52 my $me = shift;
53 my $sub = shift;
54  
55 while(1){
56 my($r,$end) = $me->next();
57 last if $end;
58 $sub->($r);
59 }
60 }
61  
62 ################################################################
63  
64 sub _putback {
65 my $me = shift;
66 my $r = shift;
67  
68 $me->{buf} = $r;
69 }
70  
71  
72 1;