XML/Template/Element/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 52 SCAR 6.9
CC
1
Cov
0%
_foreach
2.0
loopinit
2.0
get_first
2.0
CRAP 2.0
1 ###############################################################################
2 # XML::Template::Element::Iterator
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Element::Iterator;
11  
12  
13 use strict;
14 use XML::Template::Element;
15  
16  
17 =pod
18  
19 =head1 NAME
20  
21 XML::Template::Element::Iterator - XML::Template module that implements
22 abstract iteration.
23  
24 =head1 SYNOPSIS
25  
26 This module provides the foreach element and the underlying methods
27 necessary for iteration. Iteration is an abstract process and is defined
28 by the modules that are derived from this one. They simply need to
29 provide several methods for iterating through the data type specific to
30 the subclass.
31  
32 =head1 CONSTRUCTOR
33  
34 XML::Template::Element::Block inherits its constructor method, C<new>,
35 from L<XML::Template::Element>.
36  
37 =head1 PRIVATE METHODS
38  
39 =head2 _foreach
40  
41 This method implements iteration using the following algorithm:
42  
43 Initialize loop
44 $__value = first element
45 While $__value is defined
46 Set look variable
47 $__value = next element
48 Finish loop
49  
50 Each step is implemented by code returned by the following iteration
51 methods, which are defined in the subclass.
52  
53 =cut
54  
55 sub _foreach {
56 my $self = shift;
57 my ($code, $attribs) = @_;
58  
59 my $module = ref ($self);
60  
61 my $loopinit_code = $self->loopinit ($attribs);
62 my $get_first_code = $self->get_first ($attribs);
63 my $set_loopvar_code = $self->set_loopvar ($attribs);
64 my $get_next_code = $self->get_next ($attribs);
65 my $loopfinish_code = $self->loopfinish ($attribs);
66  
67 my $outcode = qq!
68 my (\@__array, \$__value);
69 my \$__index = 0;
70  
71 $loopinit_code
72  
73 $get_first_code
74  
75 while (defined \$__value) {
76 $set_loopvar_code
77 $code
78 $get_next_code
79 }
80 $loopfinish_code
81 !;
82 #print $outcode;
83  
84 return $outcode;
85 }
86  
87 =pod
88  
89 =head1 ITERATOR METHODS
90  
91 =head2 loopinit
92  
93 This method returns Perl code that initializes the loop.
94  
95 =cut
96  
97 sub loopinit {
98 my $self = shift;
99 my ($vars, $attribs) = @_;
100  
101 return '';
102 }
103  
104 =pod
105  
106 =head2 get_first
107  
108 This method returns Perl code that gets the first element in the loop.
109  
110 =cut
111  
112 sub get_first {
113 my $self = shift;
114 my $attribs = shift;
115  
116 my $outcode = qq!
117 \$__value = \$__array[0];
118 !;
119  
120 return $outcode;
121 }
122  
123 =pod
124  
125 =head2 set_loopvar
126  
127 This method returns Perl code that sets the loop variable that contains
128 the value of the current element in the loop.
129  
130 =cut
131  
132 sub set_loopvar {
133 my $self = shift;
134  
135 return '';
136 }
137  
138 =pod
139  
140 =head2 get_next
141  
142 This method returns Perl code that gets the next element in the loop.
143  
144 =cut
145  
146 sub get_next {
147 my $self = shift;
148 my $attribs = shift;
149  
150 my $outcode = qq!
151 \$__value = \$__array[++\$__index];
152 !;
153  
154 return $outcode;
155 }
156  
157 =pod
158  
159 =head2 loopfinish
160  
161 This method returns Perl code that ends the loop and performs any relevant
162 cleaning up.
163  
164 =cut
165  
166 sub loopfinish {
167 my $self = shift;
168  
169 return '';
170 }
171  
172 =pod
173  
174 =head1 ITERATOR TAGSET METHODS
175  
176 =head2 foreach
177  
178 This method implements the foreach element that is inherited by a
179 subclass which defines the internals.
180  
181 =cut
182  
183 sub foreach {
184 my $self = shift;
185 my ($code, $attribs) = @_;
186  
187 my $foreach_code = $self->_foreach ($code, $attribs);
188  
189 my $outcode = qq!
190 do {
191 \$vars->create_context ();
192  
193 $foreach_code
194  
195 \$vars->delete_context ();
196 };
197 !;
198 #print $outcode;
199  
200 return $outcode;
201 }
202  
203 =pod
204  
205 =head1 AUTHOR
206  
207 Jonathan Waxman
208 <jowaxman@bbl.med.upenn.edu>
209  
210 =head1 COPYRIGHT
211  
212 Copyright (c) 2002-2003 Jonathan A. Waxman
213 All rights reserved.
214  
215 This program is free software; you can redistribute it and/or
216 modify it under the same terms as Perl itself.
217  
218 =cut
219  
220  
221 1;