File Coverage

blib/lib/HTML/Macro/Loop.pm
Criterion Covered Total %
statement 6 98 6.1
branch 0 30 0.0
condition 0 12 0.0
subroutine 2 15 13.3
pod 0 13 0.0
total 8 168 4.7


line stmt bran cond sub pod time code
1             # HTML::Macro::Loop; Loop.pm
2             # Copyright (c) 2001,2002 Michael Sokolov and Interactive Factory. All rights
3             # reserved. This program is free software; you can redistribute it and/or
4             # modify it under the same terms as Perl itself.
5              
6             package HTML::Macro::Loop;
7              
8 1     1   9 use strict;
  1         2  
  1         36  
9 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         1  
  1         1325  
10              
11             require Exporter;
12             require AutoLoader;
13              
14             @ISA = qw(Exporter AutoLoader);
15             # Items to export into callers namespace by default. Note: do not export
16             # names by default without a very good reason. Use EXPORT_OK instead.
17             # Do not simply export all your public functions/methods/constants.
18             @EXPORT = qw(
19            
20             );
21             $VERSION = '1.06';
22              
23              
24             # Preloaded methods go here.
25              
26             sub new ($$$)
27             {
28 0     0 0   my ($class, $page) = @_;
29 0           my $self = {
30             'vars' => [],
31             'rows' => [],
32             '@parent' => $page,
33             };
34 0           bless $self, $class;
35 0           return $self;
36             }
37              
38             sub declare ($@)
39             # use this to indicate which vars are expected in each iteration.
40             # Fills the vars array.
41             {
42 0     0 0   my ($self, @vars) = @_;
43 0           @ {$$self {'vars'}} = @vars;
  0            
44             }
45              
46             sub push_array ($@)
47             # values must be pushed in the same order as they were declared, and all
48             # must be present
49             {
50 0     0 0   my ($self, @vals) = @_;
51 0           die "HTML::Macro::Loop::push_array: number of vals pushed(" . (@vals+0) . ") does not match number declared: " . (@ {$$self{'vars'}} + 0)
  0            
52 0 0         if (@vals + 0 != @ {$$self{'vars'}});
53 0           my $row = &new_row;
54 0           my $i = 0;
55 0           foreach my $var (@ {$$self{'vars'}})
  0            
56             {
57 0           $row->set ($var, $vals[$i++]);
58             }
59 0           push @ {$$self{'rows'}}, $row;
  0            
60             }
61              
62             sub new_row
63             {
64 0     0 0   my ($self) = @_;
65 0           my $row = new HTML::Macro;
66 0           $row->set ('@parent', $self);
67 0           $row->{'@attr'} = $self->{'@parent'}->{'@attr'};
68 0           $row->{'@incpath'} = $self->{'@parent'}->{'@incpath'};
69 0           return $row;
70             }
71              
72             sub pushall_arrays ($@)
73             # values must be pushed in the same order as they were declared, and all
74             # must be present. Arg is an array filled with refs to arrays for each row
75             {
76 0     0 0   my ($self, @rows) = @_;
77 0           foreach my $row (@rows) {
78 0           $self->push_array (@$row);
79             }
80             }
81              
82             sub push_hash ($$)
83             # values passed with var labels so they may come in any order and some may be
84             # absent (in which case zero is subtituted). However, any values passed whose
85             # vars were not declared are -silently- ignored unless there has been no
86             # declaration, in which case the keys of the hash are accepted as an implicit
87             # declaration.
88             {
89 0     0 0   my ($self, $pvals) = @_;
90 0           my @ordered_vals;
91 0           my $row = &new_row;
92 0 0         $self->declare (keys %$pvals) if (!@ {$$self{'vars'}}) ;
  0            
93 0           my $i = 0;
94 0           foreach my $var (@ {$$self{'vars'}})
  0            
95             {
96 0 0         $row->set ($var, defined($$pvals{$var}) ? $$pvals{$var} : '');
97             }
98 0           push @ {$$self{'rows'}}, $row;;
  0            
99             }
100              
101             sub set ($@ )
102             # set more values in the most recent row, or add a row if none exists
103             {
104 0     0 0   my $self = shift;
105 0 0         if (! $$self{'rows'} )
106             {
107 0           $self->push_hash (\@_);
108             } else {
109 0           my $rows = $$self{'rows'};
110 0           my $row = $$rows[$#$rows];
111 0           $row->set (@_);
112             }
113             }
114              
115             sub set_hash ($$ )
116             # set more values in the most recent row, or add a row if none exists
117             {
118 0     0 0   my $self = shift;
119 0 0         if (! $$self{'rows'} )
120             {
121 0           $self->push_hash (@_);
122             } else {
123 0           my $rows = $$self{'rows'};
124 0           my $row = $$rows[$#$rows];
125 0           $row->set_hash (@_);
126             }
127             }
128              
129             sub get ()
130             # get values from the most recent row
131             {
132 0     0 0   my ($self, $var) = @_;
133 0           my $rows = $$self{'rows'};
134 0 0         if ($rows) {
135 0           my $row = $$rows[$#$rows];
136 0           return $row->get($var);
137             }
138 0           return undef;
139             }
140              
141             sub doloop ($$$$ )
142             # perform repeated processing a-la HTML::Macro on the loop body $body,
143             # concatenate the results and return that.
144             {
145 0     0 0   my ($self, $body, $separator, $separator_final, $collapse) = @_;
146 0           my $buf = '';
147 0           my $markup_seen;
148             my @row_output;
149 0           foreach my $row (@ {$$self{'rows'}})
  0            
150             {
151 0           $row->{'@cwd'} = $self->{'@parent'}->{'@cwd'};
152 0           $row->{'@dynamic'} = $self->{'@dynamic'};
153 0           my $row_markup = $row->process_buf ($body);
154 0 0 0       next if ($collapse && !$row_markup);
155 0           push @row_output, $row_markup;
156             }
157 0           my $n = scalar @row_output;
158 0           foreach my $row_markup (@row_output)
159             {
160 0           -- $n;
161 0 0 0       next if ($collapse && !$row_markup);
162 0 0         if ($markup_seen) {
163             # show a separator (we skip if collapse and the row generated no content)
164 0 0 0       if ($separator_final && $n == 0) {
    0          
165 0           $buf .= $separator_final;
166             } elsif ($separator) {
167 0           $buf .= $separator;
168             }
169             }
170 0           $buf .= $row_markup;
171 0           $markup_seen = 1;
172             }
173 0           return $buf;
174             }
175              
176              
177             sub new_loop ()
178             {
179 0     0 0   my ($self, $name, @loop_vars) = @_;
180              
181 0           my $rows = $$self{'rows'};
182 0 0         die "HTML::Loop::new_loop: no rows in loop - call a push method" if !@$rows;
183 0           my $new_loop = new HTML::Macro::Loop ($$rows [$#$rows]);
184              
185 0 0         if ($name) {
186 0           $self->set ($name, $new_loop);
187             }
188 0 0         if (@loop_vars) {
189 0           $new_loop->declare (@loop_vars);
190             }
191 0           return $new_loop;
192             }
193              
194             sub is_empty ()
195             {
196 0     0 0   my ($self) = @_;
197 0   0       return ! ($self->{'rows'} && (@ {$self->{'rows'}} > 0));
198             }
199              
200             sub keys ()
201             {
202 0     0 0   my ($self) = @_;
203 0 0         return () if $self->is_empty();
204 0           my $rows = $$self{'rows'};
205 0           return ($$rows [$#$rows])->keys();
206             }
207              
208             # Autoload methods go after =cut, and are processed by the autosplit program.
209              
210             1;
211             __END__