Iterate.pm

filter: Statementstmt 100% 60 / 60 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 95.7% 91 / 95 CC 12 SCAR 11.0
CC
12
Cov
95%
igrep
5.0
_check_object
4.0
iterate
3.0
CRAP 3.0
↑ Summary Iterate/Tester.pm »
1 package Object::Iterate;
2 10 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
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 10 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Object::Iterate - iterators for objects that know the next element
13  
14 =head1 SYNOPSIS
15  
16 use Object::Iterate qw(iterate igrep imap);
17  
18 iterate {...} $object;
19  
20 my @filtered = igrep {...} $object;
21  
22 my @transformed = imap {...} $object;
23  
24 =head1 DESCRIPTION
25  
26 This module provides control structures to iterate through the
27 elements of an object that cannot be represented as list of items all
28 at once. Objects can represent a virtual collection that is beyond
29 the reaches of foreach, map, and grep because they cannot turn
30 themselves into a list.
31  
32 If the object can return a next element, it can use this module.
33 Iterate assumes that the object responds to C<__next__> with the next
34 element, and to C<__more__> with TRUE or FALSE if more elements remain
35 to be processed. The C<__init__> method is called before the first
36 iteration (if it exists), and is silently skipped otherwise. The
37 control structure continues until the C<__more__> method returns FALSE
38 (which does not mean that it visited all of the elements but that the
39 object has decided to stop iterating). At the end of all iterations
40 (when C<__more__> returns false), C<Object::Iterate> calls
41 C<__final__> if it exists, and skips it otherwise.
42  
43 Each control structure sets C<$_> to the current element, just like
44 foreach, map, and grep.
45  
46 =head2 Mutable method names
47  
48 You do not really have to use the C<__next__>, C<__more__>,
49 C<__init__>, or C<__final__> names. They are just the defaults which
50 <Object::Iterate> stores in the package variables C<$Next>, C<$More>,
51 C<$Init>, and C<$Final> respectively. This module does not export
52 these variables, so you need to use the full package specification to
53 change them (I<i.e.> C<$Object::Iterate::Next>). If your object does
54 not have the specified methods, the functions will die. You may want
55 to wrap them in eval blocks.
56  
57 Since this module uses package variables to store these methods names,
58 the method names apply to every use of the functions no matter the
59 object. You might want to local()-ise the variables for different
60 objects.
61  
62 Before any control structure does its job, it checks the object to see
63 if it can respond to these two methods, whatever you decide to call
64 them, so your object must know that it can respond to these methods.
65 AUTOLOADed methods cannot work since the module cannot know if they
66 exist.
67  
68 =cut
69  
70 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 10 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 10 our @EXPORT_OK = qw(iterate igrep imap);
73 10 our %EXPORT_TAGS = (
74 all => \@EXPORT_OK,
75 );
76  
77 10 our $VERSION = '1.152';
78  
79 10 our $Next = '__next__';
80 10 our $More = '__more__';
81 10 our $Init = '__init__';
82 10 our $Final = '__final__';
83  
84 sub _check_object {
85 croak( "iterate object has no $Next() method" )
86 12 ▶ unless eval { $_[0]->can( $Next ) };
Subroutine _check_object: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_[0]->can($Object::Iterate::Next) } } 5 7 12
87 croak( "iterate object has no $More() method" )
88 7 ▶ unless eval { $_[0]->can( $More ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $_[0]->can($Object::Iterate::More) } } 0 7 7
89  
90 7 ▶ $_[0]->$Init() if eval { $_[0]->can( $Init ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $_[0]->can($Object::Iterate::Init) } } 3 4 7
91  
92 7 return 1;
93 }
94  
95 =over 4
96  
97 =item iterate BLOCK, OBJECT
98  
99 Applies BLOCK to each item returned by C<< OBJECT->__next__ >>.
100  
101 iterate { print "$_\n" } $object;
102  
103 This is the same thing as using a while loop, but C<iterate()>
104 stays out of your way.
105  
106 while( $object->__more__ ) {
107 local $_ = $object->__next__;
108 ...BLOCK...
109 }
110  
111 =cut
112  
113 sub iterate :prototype(&$) {
114 1 ▶ my $sub = shift;
Subroutine iterate: called CC=3 SCAR=11.0
Pod documented
115 1 my $object = shift;
116  
117 1 _check_object( $object );
118  
119 1 ▶ while( $object->$More() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->$Object::Iterate::More) 6 1 7
120 6 local $_;
121 6 $_ = $object->$Next();
122 6 $sub->();
123 }
124  
125 1 ▶ $object->$Final() if $object->can( $Final );
Branch: 1 branch
BranchTrueFalseTotal
if $object->can($Object::Iterate::Final) 0 1 1
126 }
127  
128 =item igrep BLOCK, OBJECT
129  
130 Applies BLOCK to each item returned by C<< OBJECT->__next__ >>, and
131 returns all of the elements for which the BLOCK returns TRUE.
132  
133 my $output = igrep { print "$_\n" } $object;
134  
135 This is a grep for something that cannot be represented as a
136 list at one time.
137  
138 while( $object->__more__ ) {
139 local $_ = $object->__next__;
140 push @output, $_ if ...BLOCK...;
141 }
142  
143 =cut
144  
145 sub igrep :prototype(&$) {
146 1 ▶ my $sub = shift;
Subroutine igrep: called CC=5 SCAR=16.2
Pod documented
147 1 my $object = shift;
148  
149 1 _check_object( $object );
150  
151 1 my @output = ();
152  
153 1 ▶ while( $object->$More() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->$Object::Iterate::More) 6 1 7
154 6 local $_;
155 6 $_ = $object->$Next();
156 6 ▶ push @output, $_ if $sub->();
Branch: 1 branch
BranchTrueFalseTotal
if &$sub() 2 4 6
157 }
158  
159 1 ▶ $object->$Final() if $object->can( $Final );
Branch: 1 branch
BranchTrueFalseTotal
if $object->can($Object::Iterate::Final) 0 1 1
160  
161 1 ▶ wantarray ? @output : scalar @output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
162 }
163  
164 =item imap BLOCK, OBJECT
165  
166 Applies BLOCK to each item returned by C<< OBJECT->__next__ >>, and
167 returns the combined lists that BLOCK returns for each of the
168 elements.
169  
170 my @output = imap { print "$_\n" } $object;
171  
172 This is a map for something that cannot be represented as a
173 list at one time.
174  
175 while( $object->$More ) {
176 local $_ = $object->__next__;
177  
178 push @output, ...BLOCK...;
179 }
180  
181 =cut
182  
183 sub imap :prototype(&$) {
184 4 ▶ my $sub = shift;
Subroutine imap: called CC=3 SCAR=11.0
Pod documented
185 4 my $object = shift;
186  
187 4 _check_object( $object );
188  
189 4 my @output = ();
190  
191 4 ▶ while( $object->$More ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->$Object::Iterate::More) 31 4 35
192 31 local $_;
193 31 $_ = $object->$Next;
194 31 push @output, $sub->();
195 }
196  
197 4 ▶ $object->$Final if $object->can( $Final );
Branch: 1 branch
BranchTrueFalseTotal
if $object->can($Object::Iterate::Final) 1 3 4
198  
199 4 @output;
200 }
201  
202 =back
203  
204 =head1 ERROR MESSAGES
205  
206 =over 4
207  
208 =item iterate object has no C<__more__()> method at script line N
209  
210 You need to provide the method to let C<Object::Iterate> determine if
211 more elements are available. You don't have to call it C<__more__> if
212 you change the value of C<$Object::Iterate::More>.
213  
214 =item iterate object has no C<__next__()> method at script line N
215  
216 You need to provide the method to let Object::Iterate fetch the next
217 element. You don't have to call it C<__next__> if you change the
218 value of C<$Object::Iterate::Next>.
219  
220 =back
221  
222 =head1 SOURCE AVAILABILITY
223  
224 This module is on Github:
225  
226 http://github.com/briandfoy/object-iterate
227  
228 =head1 TO DO
229  
230 * let the methods discover the method names per object.
231  
232 =head1 CREDITS
233  
234 Thanks to Slaven Rezic for adding C<__init__> support
235  
236 =head1 AUTHOR
237  
238 brian d foy, C<< <briandfoy@pobox.com> >>.
239  
240 =head1 COPYRIGHT AND LICENSE
241  
242 Copyright © 2002-2024, brian d foy <briandfoy@pobox.com>. All rights reserved.
243  
244 This program is free software; you can redistribute it and/or modify
245 it under the terms of the Artistic License 2.0.
246  
247 =cut
248  
249 10 1;
↑ Summary Iterate/Tester.pm »