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}
40 # Force numerical context on both values;
47 /^>$/ && do { $res = ($val > $value); last };
48 /^<$/ && do { $res = ($val < $value); last };
49 /^==$/ && do { $res = ($val == $value); last };
50 /^!=$/ && do { $res = ($val != $value); last };
51 /^>=$/ && do { $res = ($val >= $value); last };
52 /^<=$/ && do { $res = ($val <= $value); last };
53 /^gt$/ && do { $res = ($val gt $value); last };
54 /^lt$/ && do { $res = ($val lt $value); last };
55 /^eq$/ && do { $res = ($val eq $value); last };
56 /^ne$/ && do { $res = ($val ne $value); last };
57 /^ge$/ && do { $res = ($val ge $value); last };
58 /^le$/ && do { $res = ($val le $value); last };
60 die "Unknown operator in conditional resolve", $/;
65 elsif (my $is = uc $context->get($self, 'IS'))
67 my $istrue = $val && 1;
70 return 0 unless $istrue;
74 warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
89 return 1 unless $self->conditional_passes($context);
91 return $self->iterate_over_children($context);
97 my ($context, $attr) = @_;
99 return 0 unless $self->conditional_passes($context);
101 return $self->SUPER::max_of($context, $attr);
107 my ($context, $attr) = @_;
109 return 0 unless $self->conditional_passes($context);
111 return $self->SUPER::total_of($context, $attr);
119 Excel::Template::Container::Conditional - Excel::Template::Container::Conditional
123 To provide conditional execution of children nodes
131 Excel::Template::Container
139 This is the name of the parameter to be testing. It is resolved like any other
144 If VALUE is set, then a comparison operation is done. The value of NAME is
145 compared to VALUE using the value of OP.
149 If VALUE is set, then this is checked. If it isn't present, then '==' (numeric
150 equality) is assumed. OP must be one of the numeric comparison operators or the
151 string comparison operators. All 6 of each kind is supported.
155 If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or
156 "FALSE". The boolean value of NAME is checked against IS.
174 <if name="__ODD__" is="false">
178 In the above example, the children will be executed if the value of __ODD__
179 (which is set by the LOOP node) is false. So, for all even iterations.
183 Rob Kinyon (rob.kinyon@gmail.com)