| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Iterator | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # | ||
| 7 | # Module defining an iterator class which is used by the FOREACH | ||
| 8 | # directive for iterating through data sets. This may be | ||
| 9 | # sub-classed to define more specific iterator types. | ||
| 10 | # | ||
| 11 | # AUTHOR | ||
| 12 | # Andy Wardley <abw@wardley.org> | ||
| 13 | # | ||
| 14 | # COPYRIGHT | ||
| 15 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||
| 16 | # | ||
| 17 | # This module is free software; you can redistribute it and/or | ||
| 18 | # modify it under the same terms as Perl itself. | ||
| 19 | # | ||
| 20 | #============================================================================ | ||
| 21 | |||
| 22 | package Template::Iterator; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use base 'Template::Base'; | ||
| 27 | use Template::Constants; | ||
| 28 | use Template::Exception; | ||
| 29 | use Scalar::Util qw(blessed); | ||
| 30 | |||
| 31 | use constant ODD => 'odd'; | ||
| 32 | use constant EVEN => 'even'; | ||
| 33 | |||
| 34 | our $VERSION = '3.105'; | ||
| 35 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 36 | our $AUTOLOAD; | ||
| 37 | |||
| 38 | #======================================================================== | ||
| 39 | # ----- CLASS METHODS ----- | ||
| 40 | #======================================================================== | ||
| 41 | |||
| 42 | #------------------------------------------------------------------------ | ||
| 43 | # new(\@target, \%options) | ||
| 44 | # | ||
| 45 | # Constructor method which creates and returns a reference to a new | ||
| 46 | # Template::Iterator object. A reference to the target data (array | ||
| 47 | # or hash) may be passed for the object to iterate through. | ||
| 48 | #------------------------------------------------------------------------ | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my $class = shift; | ||
| 52 | my $data = shift || [ ]; | ||
| 53 | my $params = shift || { }; | ||
| 54 | |||
| 55 | if (ref $data eq 'HASH') { | ||
| 56 | # map a hash into a list of { key => ???, value => ??? } hashes, | ||
| 57 | # one for each key, sorted by keys | ||
| 58 | $data = [ | ||
| 59 | map { { key => $_, value => $data->{ $_ } } } | ||
| 60 | sort keys %$data | ||
| 61 | ]; | ||
| 62 | } | ||
| 63 | elsif (blessed($data) && $data->can('as_list')) { | ||
| 64 | $data = $data->as_list(); | ||
| 65 | } | ||
| 66 | elsif (ref $data ne 'ARRAY') { | ||
| 67 | # coerce any non-list data into an array reference | ||
| 68 | $data = [ $data ] ; | ||
| 69 | } | ||
| 70 | |||
| 71 | bless { | ||
| 72 | _DATA => $data, | ||
| 73 | _ERROR => '', | ||
| 74 | }, $class; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | #======================================================================== | ||
| 79 | # ----- PUBLIC OBJECT METHODS ----- | ||
| 80 | #======================================================================== | ||
| 81 | |||
| 82 | #------------------------------------------------------------------------ | ||
| 83 | # get_first() | ||
| 84 | # | ||
| 85 | # Initialises the object for iterating through the target data set. The | ||
| 86 | # first record is returned, if defined, along with the STATUS_OK value. | ||
| 87 | # If there is no target data, or the data is an empty set, then undef | ||
| 88 | # is returned with the STATUS_DONE value. | ||
| 89 | #------------------------------------------------------------------------ | ||
| 90 | |||
| 91 | sub get_first { | ||
| 92 | my $self = shift; | ||
| 93 | my $data = $self->{ _DATA }; | ||
| 94 | |||
| 95 | $self->{ _DATASET } = $self->{ _DATA }; | ||
| 96 | my $size = scalar @$data; | ||
| 97 | my $index = 0; | ||
| 98 | |||
| 99 | return (undef, Template::Constants::STATUS_DONE) unless $size; | ||
| 100 | |||
| 101 | # initialise various counters, flags, etc. | ||
| 102 | @$self{ qw( SIZE MAX INDEX COUNT FIRST LAST ) } | ||
| 103 | = ( $size, $size - 1, $index, 1, 1, $size > 1 ? 0 : 1, undef ); | ||
| 104 | @$self{ qw( PREV NEXT ) } = ( undef, $self->{ _DATASET }->[ $index + 1 ]); | ||
| 105 | |||
| 106 | return $self->{ _DATASET }->[ $index ]; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | #------------------------------------------------------------------------ | ||
| 112 | # get_next() | ||
| 113 | # | ||
| 114 | # Called repeatedly to access successive elements in the data set. | ||
| 115 | # Should only be called after calling get_first() or a warning will | ||
| 116 | # be raised and (undef, STATUS_DONE) returned. | ||
| 117 | #------------------------------------------------------------------------ | ||
| 118 | |||
| 119 | sub get_next { | ||
| 120 | my ( $max, $index ) = @{ $_[0] }{qw( MAX INDEX )}; | ||
| 121 | |||
| 122 | # warn about incorrect usage | ||
| 123 | if ( !defined $index ) { | ||
| 124 | my ( $pack, $file, $line ) = caller(); | ||
| 125 | warn("iterator get_next() called before get_first() at $file line $line\n"); | ||
| 126 | return ( undef, Template::Constants::STATUS_DONE ); ## RETURN ## | ||
| 127 | } | ||
| 128 | |||
| 129 | # if there's still some data to go... | ||
| 130 | elsif ( $index >= $max ) { | ||
| 131 | return ( undef, Template::Constants::STATUS_DONE ); ## RETURN ## | ||
| 132 | } | ||
| 133 | |||
| 134 | my $self = shift; | ||
| 135 | my $dataset = $self->{_DATASET}; | ||
| 136 | |||
| 137 | $index++; | ||
| 138 | |||
| 139 | # update counters and flags | ||
| 140 | @$self{qw( INDEX COUNT FIRST LAST PREV NEXT )} = ( | ||
| 141 | $index, # INDEX | ||
| 142 | $index + 1, # COUNT | ||
| 143 | 0, # FIRST | ||
| 144 | $index == $max ? 1 : 0, # LAST | ||
| 145 | @$dataset[ $index - 1, $index + 1 ] # PREV, NEXT | ||
| 146 | ); | ||
| 147 | |||
| 148 | return $dataset->[ $index ]; ## RETURN ## | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | #------------------------------------------------------------------------ | ||
| 153 | # get_all() | ||
| 154 | # | ||
| 155 | # Method which returns all remaining items in the iterator as a Perl list | ||
| 156 | # reference. May be called at any time in the life-cycle of the iterator. | ||
| 157 | # The get_first() method will be called automatically if necessary, and | ||
| 158 | # then subsequent get_next() calls are made, storing each returned | ||
| 159 | # result until the list is exhausted. | ||
| 160 | #------------------------------------------------------------------------ | ||
| 161 | |||
| 162 | sub get_all { | ||
| 163 | my $self = shift; | ||
| 164 | my ($max, $index) = @$self{ qw( MAX INDEX ) }; | ||
| 165 | my @data; | ||
| 166 | |||
| 167 | # handle cases where get_first() has yet to be called. | ||
| 168 | unless (defined $index) { | ||
| 169 | my ($first, $status) = $self->get_first; | ||
| 170 | |||
| 171 | # refresh $max and $index, after get_first updates MAX and INDEX | ||
| 172 | ($max, $index) = @$self{ qw( MAX INDEX ) }; | ||
| 173 | |||
| 174 | # empty lists are handled here. | ||
| 175 | if ($status && $status == Template::Constants::STATUS_DONE) { | ||
| 176 | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||
| 177 | } | ||
| 178 | |||
| 179 | push @data, $first; | ||
| 180 | |||
| 181 | ## if there's nothing left in the iterator, return the single value. | ||
| 182 | unless ($index < $max) { | ||
| 183 | return \@data; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # if there's still some data to go... | ||
| 188 | if ($index < $max) { | ||
| 189 | $index++; | ||
| 190 | push @data, @{ $self->{ _DATASET } } [ $index..$max ]; | ||
| 191 | |||
| 192 | # update counters and flags | ||
| 193 | @$self{ qw( INDEX COUNT FIRST LAST ) } | ||
| 194 | = ( $max, $max + 1, 0, 1 ); | ||
| 195 | |||
| 196 | return \@data; ## RETURN ## | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | sub odd { | ||
| 204 | shift->{ COUNT } % 2 ? 1 : 0 | ||
| 205 | } | ||
| 206 | |||
| 207 | sub even { | ||
| 208 | shift->{ COUNT } % 2 ? 0 : 1 | ||
| 209 | } | ||
| 210 | |||
| 211 | sub parity { | ||
| 212 | shift->{ COUNT } % 2 ? ODD : EVEN; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub index { | ||
| 216 | return $_[0]->{INDEX}; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub count { | ||
| 220 | return $_[0]->{COUNT}; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub number { # This is here for backward compatibility per sub AUTOLOAD | ||
| 224 | return $_[0]->{COUNT}; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub first { | ||
| 228 | return $_[0]->{FIRST}; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub last { | ||
| 232 | return $_[0]->{LAST}; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub size { | ||
| 236 | return $_[0]->{SIZE}; | ||
| 237 | } | ||
| 238 | |||
| 239 | #------------------------------------------------------------------------ | ||
| 240 | # AUTOLOAD | ||
| 241 | # | ||
| 242 | # Provides access to internal fields (e.g. prev, next, etc) | ||
| 243 | #------------------------------------------------------------------------ | ||
| 244 | |||
| 245 | sub AUTOLOAD { | ||
| 246 | my $self = shift; | ||
| 247 | my $item = $AUTOLOAD; | ||
| 248 | $item =~ s/.*:://; | ||
| 249 | return if $item eq 'DESTROY'; | ||
| 250 | |||
| 251 | # alias NUMBER to COUNT for backwards compatibility | ||
| 252 | $item = 'COUNT' if CORE::index(uc $item,'NUMBER') > -1; | ||
| 253 | |||
| 254 | return $self->{ uc $item }; | ||
| 255 | } | ||
| 256 | |||
| 257 | 1; | ||
| 258 | |||
| 259 | __END__ |