XML/Template/Element/Var.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 145 SCAR 24.8
CC
3
Cov
0%
length
12.0
set
2.0
unset
2.0
CRAP 12.0
1 ###############################################################################
2 # XML::Template::Element::Var
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::Var;
11 use base qw(XML::Template::Element XML::Template::Element::Iterator);
12  
13  
14 use strict;
15 use Exporter;
16 use IO::String;
17 use Data::Dumper;
18  
19  
20 =pod
21  
22 =head1 NAME
23  
24 XML::Template::Element::Var - XML::Template plugin module for the var
25 namespace.
26  
27 =head1 SYNOPSIS
28  
29 This XML::Template plugin module implements the var namespace tagset.
30 The var namespace includes tags that handle scalar, array, nested, and
31 XPath variables.
32  
33 =head1 CONSTRUCTOR
34  
35 XML::Template::Element::Var inherits its constructor method, C<new>, from
36 L<XML::Template::Element>.
37  
38 =head1 VAR TAGSET METHODS
39  
40 =head2 set
41  
42 This method implements the set element which set a variable. The value of
43 the variable is contained in the content. The following attributes are
44 used:
45  
46 =over 4
47  
48 =item name
49  
50 The name of the variable to set in XML::Template variable format.
51  
52 =item scope
53  
54 The context in which to create the new variable: C<global> or C<local>.
55 The default value is C<local>.
56  
57 =back
58  
59 =cut
60  
61 sub set {
62 my $self = shift;
63 my ($code, $attribs) = @_;
64  
65 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
66 my $scope = $self->get_attrib ($attribs, 'scope') || "'local'";
67  
68 my $outcode = qq!
69 do {
70 my \$__varname = $name;
71 my \$__set = 1;
72 my \$__i = 0;
73  
74 my \$cgi_header = \$process->{_cgi_header};
75 \$process->{_cgi_header} = 0;
76  
77 my \$value;
78 my \$io = IO::String->new (\$value);
79 my \$ofh = select \$io;
80  
81 $code
82  
83 select \$ofh;
84  
85 if (\$__set) {
86 \$value =~ s/^\\s*//;
87 \$value =~ s/\\s*\$//;
88 if ($scope eq 'global') {
89 \$vars->set_global (\$__varname => \$value);
90 } else {
91 \$vars->set (\$__varname => \$value);
92 }
93 }
94  
95 \$process->{_cgi_header} = \$cgi_header;
96 };
97 !;
98 #print $outcode;
99  
100 return $outcode;
101 }
102  
103 =pod
104  
105 =head2 unset
106  
107 This metho implements the unset element which unsets (deletes) a variable
108 from the current variable context. The following attributes are used:
109  
110 =over 4
111  
112 =item name
113  
114 The name of the variable to unset in XML::Template variable format.
115  
116 =back
117  
118 =cut
119  
120 sub unset {
121 my $self = shift;
122 my ($code, $attribs) = @_;
123  
124 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
125  
126 my $outcode = qq{
127 do {
128 \$vars->unset ($name);
129 };
130 };
131  
132 return $outcode;
133 }
134  
135 =pod
136  
137 =head2 element
138  
139 This method implements the element element which must be nested in the set
140 element. It is used to set array values or nested variable values. It
141 uses the following attributes:
142  
143 =over 4
144  
145 =item name
146  
147 If you supply the name attribute, a new nested variable is created under
148 the parent variable with this name. Otherwise, an additional array value
149 is appended to the parent array.
150  
151 For instance,
152  
153 <xml xmlns:var="http://syrme.net/xml-template/var/v1">
154 <var:set name="array">
155 <var:element>ONE</var:element>
156 <var:element>TWO</var:element>
157 <var:element>THREE</var:element>
158 </var:set>
159  
160 <var:set name="nested1">
161 <var:element name="nested2">
162 <var:element name="nested3">
163 <var:element>ONE</var:element>
164 <var:element>TWO</var:element>
165 <var:element>THREE</var:element>
166 </var:element>
167 <var:element>TWO</element>
168 </var:element>
169 </var:set>
170 </xml>
171  
172 =cut
173  
174 sub element {
175 my $self = shift;
176 my ($code, $attribs) = @_;
177  
178 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
179  
180 my $outcode = qq!
181 do {
182 my \$__varname = \$__varname;
183 if (defined $name) {
184 \$__varname .= "." . $name;
185 } else {
186 \$__varname .= "[\$__i]";
187 \$__i++;
188 }
189 \$__set = 0;
190 my \$__set = 1;
191 my \$__i = 0;
192  
193 my \$value;
194 my \$io = IO::String->new (\$value);
195 my \$ofh = select \$io;
196  
197 $code
198  
199 select \$ofh;
200  
201 if (\$__set) {
202 \$value =~ s/^\\s*//;
203 \$value =~ s/\\s*\$//;
204 \$vars->set (\$__varname => \$value);
205 }
206 };
207 !;
208  
209 return $outcode;
210 }
211  
212 =pod
213  
214 =head2 get
215  
216 This method implements the get element which returns the value of a
217 variable. The following attributes are used:
218  
219 =over 4
220  
221 =item name
222  
223 The name of the variable whose value you wish to return in XML::Template
224 variable format.
225  
226 =back
227  
228 =cut
229  
230 sub get {
231 my $self = shift;
232 my ($code, $attribs) = @_;
233  
234 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
235  
236 my $outcode = qq!
237 print \$vars->get ($name);
238 !;
239  
240 return $outcode;
241 }
242  
243 =pod
244  
245 =head2 dump
246  
247 This method implements the dump element which dumps the contents of a
248 variable using Data::Dumper. The following attributes are used:
249  
250 =over 4
251  
252 =item name
253  
254 The name of the variable to dump in XML::Template variable name format.
255  
256 =back
257  
258 =cut
259  
260 sub dump {
261 my $self = shift;
262 my ($code, $attribs) = @_;
263  
264 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
265  
266 my $outcode = qq!
267 print Dumper (\$vars->get ($name));
268 !;
269  
270 return $outcode;
271 }
272  
273 =pod
274  
275 =head2 foreach
276  
277 XML::Template::Element::Var is a subclass of
278 L<XML::Template::Element::Iterator>, so it inherits the C<foreach> method,
279 which in conjunction with the iterator methods defined in this module,
280 implements iteration through arrays values. Note that XPath variables may
281 return an array of matched element through with you may iterate. The
282 following attributes are used:
283  
284 =over 4
285  
286 =item src
287  
288 The name of the variable whose value you wish to iterate through.
289  
290 =item var
291  
292 The name of the variable, available in the content, that contains the
293 current array element value.
294  
295 =back
296  
297 For instance,
298  
299 <xml xmlns:var="syrme.net/xml-template/var/v1">
300 <var:foreach src="nested.array" var="el">
301 ${el}
302 </var:foreach>
303  
304 <var:foreach src="xml[2]/employee[@type="fulltime"]/lastname/text()"
305 var="lastname">
306 ${lastname}
307 </var:foreach>
308 </xml>
309  
310 =cut
311  
312 sub loopinit {
313 my $self = shift;
314 my $attribs = shift;
315  
316 my $array = $self->get_attrib ($attribs, 'array') || 'undef';
317 # xxx do replace in get_attrib ?
318 # $array =~ s/\@/\\\@/g;
319 my $var = $self->get_attrib ($attribs, 'var') || 'undef';
320  
321 my $outcode = qq!
322 my \$__var = $var;
323 my \$array = $array;
324 if (ref (\$array) eq 'ARRAY') {
325 \@__array = \@\$array;
326 } elsif (ref (\$array) eq 'HASH') {
327 \@__array = keys \%\$array;
328 } elsif (ref (\$array)) {
329 push (\@__array, \$array);
330 } else {
331 my \@array = split (/(?<\!\\\\),/, \$array);
332 foreach my \$str (\@array) {
333 if (\$str =~ /^([^\.]+)\\.\\.(.+)\$/) {
334 push (\@__array, \$1..\$2);
335 } else {
336 push (\@__array, \$str);
337 }
338 }
339 }
340 !;
341  
342 return $outcode;
343 }
344  
345 sub set_loopvar {
346 my $self = shift;
347 my $attribs = shift;
348  
349 my $outcode = qq!
350 \$vars->set (\$__var => \$__value);
351 !;
352  
353 return $outcode;
354 }
355  
356 =pod
357  
358 =head1 XML::Template SUBROUTINES
359  
360 Several method exist in XML::Template::Element::Var that implement
361 XML::Template subroutines that operate on variables. This module should
362 be associated with them in the XML::Template configuration file. See
363 L<XML::Template::Subroutine> for more details.
364  
365 =head2 push
366  
367 This subroutine pushes a value onto the end of the array referenced by the
368 value. The additional parameter is the new value to add.
369  
370 =cut
371  
372 sub push {
373 my $class = shift;
374 my $process = shift;
375 my ($var, $value, $el) = @_;
376  
377 $el = $class->strip ($el);
378  
379 CORE::push (@$value, $el);
380  
381 return '';
382 }
383  
384 =pod
385  
386 =head2 pop
387  
388 This method pops a value off the end of the array referenced by the value.
389  
390 =cut
391  
392 sub pop {
393 my $class = shift;
394 my $process = shift;
395 my ($var, $value) = @_;
396  
397 CORE::pop (@$value);
398  
399 return '';
400 }
401  
402 =pod
403  
404 =head2
405  
406 This method unshifts a new value onto the beginning of the array
407 referenced by the value. The additional parameter is the new value to
408 add.
409  
410 =cut
411  
412 sub unshift {
413 my $class = shift;
414 my $process = shift;
415 my ($var, $value, $el) = @_;
416  
417 $el = $class->strip ($el);
418  
419 CORE::unshift (@$value, $el);
420  
421 return '';
422 }
423  
424 =pod
425  
426 =head2 shift
427  
428 This method shifts a value off the beginning of the array referenced by
429 the value.
430  
431 =cut
432  
433 sub shift {
434 my $class = shift;
435 my $process = shift;
436 my ($var, $value) = @_;
437  
438 CORE::shift (@$value);
439  
440 return '';
441 }
442  
443 =pod
444  
445 =head2 join
446  
447 This method joins the strings in the array referenced by the value. The
448 additional parameter specified the separator to use between the strings.
449  
450 =cut
451  
452 sub join {
453 my $class = shift;
454 my $process = shift;
455 my ($var, $value, $sep) = @_;
456  
457 $sep = $class->strip ($sep);
458  
459 return CORE::join ($sep, @$value);
460 }
461  
462 =pod
463  
464 =head2 split
465  
466 This method splits a string into substrings and returns a reference to an
467 array containing them. The additional parameter specified the separator
468 to split the string on.
469  
470 =cut
471  
472 sub split {
473 my $class = shift;
474 my $process = shift;
475 my ($var, $value, $sep) = @_;
476  
477 $sep = $class->strip ($sep);
478  
479 my @array = CORE::split ($sep, $value);
480 return \@array;
481 }
482  
483 =pod
484  
485 =head2 length
486  
487 This method returns the number of elements in the array referenced by
488 value.
489  
490 =cut
491  
492 sub length {
493 my $class = shift;
494 my $process = shift;
495 my ($var, $value) = @_;
496  
497 if (defined $value) {
498 if (ref ($value) eq 'ARRAY') {
499 return scalar (@$value);
500 } else {
501 return 1;
502 }
503 } else {
504 return 0;
505 }
506 }
507  
508 =pod
509  
510 =head1 AUTHOR
511  
512 Jonathan Waxman
513 <jowaxman@bbl.med.upenn.edu>
514  
515 =head1 COPYRIGHT
516  
517 Copyright (c) 2002-2003 Jonathan A. Waxman
518 All rights reserved.
519  
520 This program is free software; you can redistribute it and/or
521 modify it under the same terms as Perl itself.
522  
523 =cut
524  
525  
526 1;