| 1 | ############################################################################### | ||
| 2 | # XML::Template::Element::Condition | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Element::Condition; | ||
| 11 | use base qw(XML::Template::Element); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use XML::Template::Element; | ||
| 15 | |||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | XML::Template::Element::Condition - XML::Template plugin module for the | ||
| 22 | condition namespace tagset. | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | This XML::Template plugin module implements the condition namespace | ||
| 27 | tagset. The condition namespace includes tags for creating flow control | ||
| 28 | structures. | ||
| 29 | |||
| 30 | =head1 CONSTRUCTOR | ||
| 31 | |||
| 32 | XML::Template::Element::Block inherits its constructor method, C<new>, | ||
| 33 | from L<XML::Template::Element>. | ||
| 34 | |||
| 35 | =head1 CONDITION TAGSET METHODS | ||
| 36 | |||
| 37 | =head2 if | ||
| 38 | |||
| 39 | This method implements the beginning of an if flow control structure. The | ||
| 40 | following attributes are used: | ||
| 41 | |||
| 42 | =over 4 | ||
| 43 | |||
| 44 | =item cond | ||
| 45 | |||
| 46 | The test to perform that, if evaluates to true, passes control to the | ||
| 47 | content of the if element. | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub if { | ||
| 54 | my $self = shift; | ||
| 55 | my ($code, $attribs) = @_; | ||
| 56 | |||
| 57 | my $cond = $self->get_attrib ($attribs, 'cond') || 'undef'; | ||
| 58 | |||
| 59 | my $outcode = qq! | ||
| 60 | if ($cond) { | ||
| 61 | $code | ||
| 62 | } | ||
| 63 | !; | ||
| 64 | |||
| 65 | return $outcode; | ||
| 66 | } | ||
| 67 | |||
| 68 | =pod | ||
| 69 | |||
| 70 | =head2 elseif | ||
| 71 | |||
| 72 | This method implements the elseif section of an if flow control structure. | ||
| 73 | The following attributes are used: | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item cond | ||
| 78 | |||
| 79 | The test to perform that, if evaluates to true, passes control to the | ||
| 80 | content of the elseif element. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub elseif { | ||
| 87 | my $self = shift; | ||
| 88 | my ($code, $attribs) = @_; | ||
| 89 | |||
| 90 | my $cond = $self->get_attrib ($attribs, 'cond') || 'undef'; | ||
| 91 | |||
| 92 | my $outcode = qq! | ||
| 93 | } elsif ($cond) { | ||
| 94 | $code | ||
| 95 | !; | ||
| 96 | |||
| 97 | return $outcode; | ||
| 98 | } | ||
| 99 | |||
| 100 | =pod | ||
| 101 | |||
| 102 | =head2 else | ||
| 103 | |||
| 104 | This method implements the else section of an if flow control structure. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub else { | ||
| 109 | my $self = shift; | ||
| 110 | my ($code, $attribs) = @_; | ||
| 111 | |||
| 112 | my $outcode = qq! | ||
| 113 | } else { | ||
| 114 | $code | ||
| 115 | !; | ||
| 116 | |||
| 117 | return $outcode; | ||
| 118 | } | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head2 switch | ||
| 123 | |||
| 124 | This method implements the beginning of a switch flow control structure. | ||
| 125 | The following attributes are used: | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =item expr | ||
| 130 | |||
| 131 | The expression to compare to the value attribute of each nested case | ||
| 132 | element. When a match is found, the content of that case element is | ||
| 133 | evaluated. | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub switch { | ||
| 140 | my $self = shift; | ||
| 141 | my ($code, $attribs) = @_; | ||
| 142 | |||
| 143 | my $expr = $self->get_attrib ($attribs, 'expr') || 'undef'; | ||
| 144 | |||
| 145 | my $outcode = qq! | ||
| 146 | do { | ||
| 147 | \$vars->create_context (); | ||
| 148 | |||
| 149 | my \$__expr = $expr; | ||
| 150 | SWITCH: { | ||
| 151 | $code | ||
| 152 | } | ||
| 153 | |||
| 154 | \$vars->delete_context (); | ||
| 155 | }; | ||
| 156 | !; | ||
| 157 | |||
| 158 | return $outcode; | ||
| 159 | } | ||
| 160 | |||
| 161 | =pod | ||
| 162 | |||
| 163 | =head2 case | ||
| 164 | |||
| 165 | This method implements the case section of a switch flow control | ||
| 166 | structure. The following attributes are used: | ||
| 167 | |||
| 168 | =over 4 | ||
| 169 | |||
| 170 | =item value | ||
| 171 | |||
| 172 | If this value match the switch expression, the content of this case | ||
| 173 | element will be evaluated. | ||
| 174 | |||
| 175 | =over | ||
| 176 | |||
| 177 | sub case { | ||
| 178 | my $self = shift; | ||
| 179 | my ($code, $attribs) = @_; | ||
| 180 | |||
| 181 | my $value = $self->get_attrib ($attribs, 'value'); | ||
| 182 | |||
| 183 | # my $cond; | ||
| 184 | # if ($value =~ /^\/.*\/$/) { | ||
| 185 | # $cond = "\$expr =~ $value"; | ||
| 186 | # } else { | ||
| 187 | my $cond = "\$__expr eq '$value'"; | ||
| 188 | # } | ||
| 189 | |||
| 190 | my $outcode = qq! | ||
| 191 | $cond && do { | ||
| 192 | $code | ||
| 193 | last SWITCH; | ||
| 194 | }; | ||
| 195 | !; | ||
| 196 | |||
| 197 | return $outcode; | ||
| 198 | } | ||
| 199 | |||
| 200 | =pod | ||
| 201 | |||
| 202 | =head2 default | ||
| 203 | |||
| 204 | This method implements the default section of a switch control structure. | ||
| 205 | The content is evaluates when no case values match the switch expression. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub default { | ||
| 210 | my $self = shift; | ||
| 211 | my ($code, $attribs) = @_; | ||
| 212 | |||
| 213 | my $outcode = qq! | ||
| 214 | $code | ||
| 215 | !; | ||
| 216 | |||
| 217 | return $outcode; | ||
| 218 | } | ||
| 219 | |||
| 220 | =pod | ||
| 221 | |||
| 222 | =head1 AUTHOR | ||
| 223 | |||
| 224 | Jonathan Waxman | ||
| 225 | <jowaxman@bbl.med.upenn.edu> | ||
| 226 | |||
| 227 | =head1 COPYRIGHT | ||
| 228 | |||
| 229 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 230 | All rights reserved. | ||
| 231 | |||
| 232 | This program is free software; you can redistribute it and/or | ||
| 233 | modify it under the same terms as Perl itself. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | |||
| 238 | 1; |