1 package Excel::Template::Container::Conditional;
3 #GGG Convert <conditional> to be a special case of <switch>?
9 @ISA = qw(Excel::Template::Container);
11 use Excel::Template::Container;
16 (map { $_ => $_ } ( '>', '<', '==', '!=', '>=', '<=' )),
17 (map { $_ => $_ } ( 'gt', 'lt', 'eq', 'ne', 'ge', 'le' )),
20 sub conditional_passes
25 my $name = $context->get($self, 'NAME');
26 return 0 unless $name =~ /\S/;
28 my $val = $context->param($name);
29 $val = @{$val} while UNIVERSAL::isa($val, 'ARRAY');
30 $val = ${$val} while UNIVERSAL::isa($val, 'SCALAR');
32 my $value = $context->get($self, 'VALUE');
35 my $op = $context->get($self, 'OP');
36 $op = defined $op && exists $isOp{$op}
43 /^>$/ && do { $res = ($val > $value); last };
44 /^<$/ && do { $res = ($val < $value); last };
45 /^==$/ && do { $res = ($val == $value); last };
46 /^!=$/ && do { $res = ($val != $value); last };
47 /^>=$/ && do { $res = ($val >= $value); last };
48 /^<=$/ && do { $res = ($val <= $value); last };
49 /^gt$/ && do { $res = ($val gt $value); last };
50 /^lt$/ && do { $res = ($val lt $value); last };
51 /^eq$/ && do { $res = ($val eq $value); last };
52 /^ne$/ && do { $res = ($val ne $value); last };
53 /^ge$/ && do { $res = ($val ge $value); last };
54 /^le$/ && do { $res = ($val le $value); last };
56 die "Unknown operator in conditional resolve", $/;
62 my $istrue = $val && 1;
64 my $is = uc $context->get($self, 'IS') || 'TRUE';
67 return 0 unless $istrue;
71 warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
85 return 1 unless $self->conditional_passes($context);
87 return $self->iterate_over_children($context);
93 my ($context, $attr) = @_;
95 return 0 unless $self->conditional_passes($context);
97 return $self->SUPER::max_of($context, $attr);
103 my ($context, $attr) = @_;
105 return 0 unless $self->conditional_passes($context);
107 return $self->SUPER::total_of($context, $attr);
115 Excel::Template::Container::Conditional - Excel::Template::Container::Conditional
119 To provide conditional execution of children nodes
127 Excel::Template::Container
135 This is the name of the parameter to be testing. It is resolved like any other
140 If VALUE is set, then a comparison operation is done. The value of NAME is
141 compared to VALUE using the value of OP.
145 If VALUE is set, then this is checked. If it isn't present, then '==' (numeric
146 equality) is assumed. OP must be one of the numeric comparison operators or the
147 string comparison operators. All 6 of each kind is supported.
151 If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or
152 "FALSE". The boolean value of NAME is checked against IS.
170 <if name="__ODD__" is="false">
174 In the above example, the children will be executed if the value of __ODD__
175 (which is set by the LOOP node) is false. So, for all even iterations.
179 Rob Kinyon (rob.kinyon@gmail.com)