Remove warning
[p5sagit/Excel-Template.git] / lib / Excel / Template / Container / Conditional.pm
CommitLineData
d0eafc11 1package Excel::Template::Container::Conditional;
2
3#GGG Convert <conditional> to be a special case of <switch>?
4
5use strict;
6
7BEGIN {
8 use vars qw(@ISA);
9 @ISA = qw(Excel::Template::Container);
10
11 use Excel::Template::Container;
12}
13
14my %isOp = (
15 '=' => '==',
16 (map { $_ => $_ } ( '>', '<', '==', '!=', '>=', '<=' )),
17 (map { $_ => $_ } ( 'gt', 'lt', 'eq', 'ne', 'ge', 'le' )),
18);
19
6dd4c89d 20sub _conditional_passes
d0eafc11 21{
22 my $self = shift;
23 my ($context) = @_;
24
25 my $name = $context->get($self, 'NAME');
26 return 0 unless $name =~ /\S/;
27
28 my $val = $context->param($name);
d01e4722 29 $val = @{$val} while ref $val eq 'ARRAY';
30 $val = ${$val} while ref $val eq 'SCALAR';
d0eafc11 31
32 my $value = $context->get($self, 'VALUE');
33 if (defined $value)
34 {
35 my $op = $context->get($self, 'OP');
36 $op = defined $op && exists $isOp{$op}
37 ? $isOp{$op}
38 : '==';
39
d0eafc11 40 my $res;
41 for ($op)
42 {
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 };
55
8fd01531 56 die "Unknown operator '$op' in conditional resolve", $/;
d0eafc11 57 }
58
dba6a68e 59 return $res && 1;
d0eafc11 60 }
dba6a68e 61
62 my $istrue = $val && 1;
63
6b8b6f4b 64 my $is = uc($context->get($self, 'IS') || 'TRUE');
dba6a68e 65 if ($is eq 'TRUE')
d0eafc11 66 {
dba6a68e 67 return 0 unless $istrue;
68 }
69 else
70 {
71 warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
dee1f239 72 if $is ne 'FALSE' && $^W;
d0eafc11 73
dba6a68e 74 return 0 if $istrue;
d0eafc11 75 }
76
77 return 1;
78}
79
80sub render
81{
82 my $self = shift;
83 my ($context) = @_;
84
6dd4c89d 85 return 1 unless $self->_conditional_passes($context);
d0eafc11 86
87 return $self->iterate_over_children($context);
88}
89
e976988f 90#sub max_of
91#{
92# my $self = shift;
93# my ($context, $attr) = @_;
94#
95# return 0 unless $self->_conditional_passes($context);
96#
97# return $self->SUPER::max_of($context, $attr);
98#}
99#
100#sub total_of
101#{
102# my $self = shift;
103# my ($context, $attr) = @_;
104#
105# return 0 unless $self->_conditional_passes($context);
106#
107# return $self->SUPER::total_of($context, $attr);
108#}
d0eafc11 109
1101;
111__END__
112
113=head1 NAME
114
115Excel::Template::Container::Conditional - Excel::Template::Container::Conditional
116
117=head1 PURPOSE
118
119To provide conditional execution of children nodes
120
121=head1 NODE NAME
122
123IF
124
125=head1 INHERITANCE
126
8fd01531 127L<CONTAINER|Excel::Template::Container>
d0eafc11 128
129=head1 ATTRIBUTES
130
131=over 4
132
133=item * NAME
134
8fd01531 135This is the name of the parameter to test. It is resolved like any other parameter name. (q.v. L<VAR|Excel::Template::Element::Var> for more info.)
d0eafc11 136
137=item * VALUE
138
8fd01531 139If VALUE is set, then a comparison operation is done. The value of NAME is compared to VALUE using the value of OP.
d0eafc11 140
141=item * OP
142
8fd01531 143If VALUE is set, then this is checked. If it isn't present, then '==' (numeric equality) is assumed. OP must be one of Perl the numeric comparison operators or the string comparison operators. All 6 of each kind is supported.
144
145B<Note>: If you want to use < or <=, you must instead use &lt; or &lt;=. This is to make sure it will parse with L<XML::Parser>. You should not need to use &gt; or &gt;= instead of > and >=, respectively.
d0eafc11 146
147=item * IS
148
8fd01531 149If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or "FALSE". The boolean value of NAME is checked against IS.
d0eafc11 150
6dd4c89d 151=back
d0eafc11 152
153=head1 CHILDREN
154
155None
156
157=head1 EFFECTS
158
159None
160
161=head1 DEPENDENCIES
162
163None
164
165=head1 USAGE
166
167 <if name="__ODD__" is="false">
168 ... Children here
169 </if>
170
8fd01531 171In the above example, the children will be executed if the value of __ODD__ (which is set by the L<LOOP|Excel::Template::Container::Loop> node) is false. So, for all even iterations.
d0eafc11 172
173=head1 AUTHOR
174
c09684ff 175Rob Kinyon (rob.kinyon@gmail.com)
d0eafc11 176
177=head1 SEE ALSO
178
8fd01531 179L<LOOP|Excel::Template::Container::Loop>, L<VAR|Excel::Template::Element::Var>
d0eafc11 180
181=cut