File Coverage

blib/lib/Data/Iter.pm
Criterion Covered Total %
statement 12 93 12.9
branch 0 24 0.0
condition 0 8 0.0
subroutine 4 32 12.5
pod 14 21 66.6
total 30 178 16.8


line stmt bran cond sub pod time code
1             package Data::Iter;
2            
3 1     1   33361 use 5.006;
  1         4  
  1         55  
4 1     1   6 use strict;
  1         2  
  1         35  
5 1     1   5 use warnings;
  1         6  
  1         28  
6            
7 1     1   6 use Carp;
  1         1  
  1         1767  
8            
9            
10             $Carp::Verbose = 1;
11            
12            
13             require Exporter;
14            
15             our @ISA = qw(Exporter);
16            
17             our %EXPORT_TAGS = ( 'all' => [ qw(iter counter COUNTER LAST_COUNTER value VALUE key KEY get GET getnext GETNEXT GETPREV IS_LAST IS_FIRST transform_array_to_hash) ] );
18            
19             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
20            
21             our @EXPORT = qw();
22            
23             our $VERSION = '0.2';
24            
25             # Preloaded methods go here.
26            
27             #prototype of ($$) is important for sort functions
28            
29 0     0 0   sub sort_alpha($$) { $_[0] cmp $_[1] }
30 0     0 0   sub sort_num($$) { $_[0] <=> $_[1] }
31            
32             our $cref_sort = sub { $a cmp $b };
33            
34 0     0 0   sub default_sort { $cref_sort->(@_) }
35            
36             our $Sort = 'default_sort';
37            
38             our $options = { };
39            
40 0     0     sub _KEY { 0 }
41 0     0     sub _VALUE { 1 }
42 0     0     sub _COUNT { 2 }
43 0     0     sub _KEY_REF { 3 }
44 0     0     sub _VALUE_REF { 5 }
45 0     0     sub _LIST_REF { 6 }
46            
47             sub iter
48             {
49 0 0 0 0 1   my $this = shift if ( ref $_[0] || $_[0] ) eq __PACKAGE__;
50            
51 0           my $cnt = 0;
52            
53 0           my $ref_data = shift;
54            
55 0           my @result = ();
56            
57 0 0         unless( @_ )
58             {
59 0 0         if( ref $ref_data eq 'HASH' )
    0          
60             {
61 0           foreach my $key ( sort $Sort keys %{ $ref_data } )
  0            
62             {
63 0           my $obj = [];
64            
65 0           @$obj[ _KEY, _VALUE, _COUNT, _KEY_REF, _VALUE_REF, _LIST_REF ] = ( $key, $ref_data->{$key}, $cnt, \$key, \( $ref_data->{$key}, \@result ) );
66            
67 0           push @result, bless $obj, __PACKAGE__;
68            
69 0           $cnt++;
70             }
71             }
72             elsif( ref $ref_data eq 'ARRAY' )
73             {
74 0           @result = ();
75            
76 0           foreach my $value ( @$ref_data )
77             {
78 0           my $obj = [];
79            
80 0           @$obj[ _KEY, _VALUE, _COUNT, _VALUE_REF, _LIST_REF ] = ( $cnt, $value, $cnt, \( $ref_data->[$cnt] ), \@result );
81            
82 0           push @result, bless $obj, __PACKAGE__;
83            
84 0           $cnt++;
85             }
86             }
87             else
88             {
89 0           croak "iter() only accepts reference to ARRAY or HASH. Found: ". ref( $ref_data );
90             }
91             }
92             else
93             {
94 0           croak "iter() only accepts one parameter (reference to ARRAY or HASH). Found extra args: ", scalar @_;
95             }
96            
97 0           return @result;
98             }
99            
100             sub _handle_this
101             {
102 0     0     my $this;
103            
104             # called as method ?
105            
106 0 0 0       $this = shift @{$_[0]} if ( ref $_[0]->[0] || defined $_[0]->[0] ) eq __PACKAGE__;
  0            
107            
108             # no, so use $_ as obj
109            
110 0 0         $this = $_ unless $this;
111            
112 0           return $this;
113             }
114            
115            
116            
117             sub counter
118             {
119 0     0 1   my $this = _handle_this( \@_ );
120            
121 0           return $this->[_COUNT];
122             }
123            
124 0     0 1   sub COUNTER { goto &counter }
125            
126             sub value
127             {
128 0     0 1   my $this = _handle_this( \@_ );
129            
130             # set value if argument given
131            
132 0 0         $this->[_VALUE] = ${ $this->[_VALUE_REF] } = $_[0] if @_;
  0            
133            
134 0           return $this->[_VALUE];
135             }
136            
137 0     0 1   sub VALUE { goto &value }
138            
139             sub key
140             {
141 0     0 0   my $this = _handle_this( \@_ );
142            
143             # set value if argument given
144            
145 0 0         $this->[_VALUE] = ${ $this->[_KEY_REF] } = $_[0] if @_;
  0            
146            
147 0           return $this->[_KEY];
148             }
149            
150 0     0 1   sub KEY { goto &key }
151            
152             sub get
153             {
154 0     0 0   my $this = _handle_this( \@_ );
155            
156 0   0       my $pos = shift || -1;
157            
158 0           return $this->[_LIST_REF]->[$pos];
159             }
160            
161 0     0 1   sub GET { goto &get }
162            
163             sub getnext
164             {
165 0     0 0   my $this = _handle_this( \@_ );
166            
167 0           my $pos = -1;
168            
169 0           my $result = $this->get( $this->counter+1 );
170            
171 0 0         return $result unless $result;
172            
173 0           return $result;
174             }
175            
176 0     0 1   sub GETNEXT { goto &getnext }
177            
178             sub GETPREV
179             {
180 0     0 1   my $this = _handle_this( \@_ );
181            
182 0           return $this->getnext( $this->counter-1 );
183             }
184            
185             sub LAST_COUNTER
186             {
187 0     0 1   my $this = _handle_this( \@_ );
188            
189 0           return scalar @{ $this->[_LIST_REF] } - 1;
  0            
190             }
191            
192             sub IS_LAST
193             {
194 0     0 1   my $this = _handle_this( \@_ );
195            
196 0           return $this->COUNTER == $this->LAST_COUNTER;
197             }
198            
199             sub IS_FIRST
200             {
201 0     0 1   my $this = _handle_this( \@_ );
202            
203 0           return $this->COUNTER == 0;
204             }
205            
206            
207            
208             sub pair
209             {
210 0     0 0   my $this = _handle_this( \@_ );
211            
212            
213 0 0         if( $this->COUNTER() % 2 == 0 )
214             {
215 0           return ( $this->VALUE, $this->GETNEXT->VALUE );
216             }
217            
218 0           return ();
219             }
220            
221 0     0 1   sub PAIR { goto &pair }
222            
223             # some nice service functions
224            
225             sub transform_array_to_hash
226             {
227 0     0 1   my $array = shift;
228            
229            
230 0           my $result;
231            
232 0           foreach ( iter $array )
233             {
234 0 0         if( COUNTER() % 2 == 0 )
235             {
236             #printfln q{%s => %s}, VALUE, GETNEXT->VALUE;
237            
238 0 0         if( exists $result->{ VALUE() } )
239             {
240 0           push @{ $result->{ VALUE() } }, GETNEXT->VALUE;
  0            
241             }
242             else
243             {
244 0           $result->{ VALUE() } = [ GETNEXT->VALUE ];
245             }
246             }
247             }
248            
249 0           return $result;
250             }
251            
252             1;
253             __END__