File Coverage

blib/lib/Dotiac/DTL/Tag/if.pm
Criterion Covered Total %
statement 144 151 95.3
branch 52 56 92.8
condition n/a
subroutine 12 14 85.7
pod 11 11 100.0
total 219 232 94.4


line stmt bran cond sub pod time code
1             #if.pm
2             #Last Change: 2009-01-19
3             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
4             #Version 0.8
5             ####################
6             #This file is part of the Dotiac::DTL project.
7             #http://search.cpan.org/perldoc?Dotiac::DTL
8             #
9             #if.pm is published under the terms of the MIT license, which basically
10             #means "Do with it whatever you want". For more information, see the
11             #license.txt file that should be enclosed with libsofu distributions. A copy of
12             #the license is (at the time of writing) also available at
13             #http://www.opensource.org/licenses/mit-license.php .
14             ###############################################################################
15            
16             package Dotiac::DTL::Tag::if;
17 11     11   64 use base qw/Dotiac::DTL::Tag/;
  11         35  
  11         964  
18 11     11   66 use strict;
  11         25  
  11         475  
19 11     11   60 use warnings;
  11         21  
  11         23447  
20            
21             our $VERSION = 0.8;
22            
23             sub new {
24 28     28 1 50 my $class=shift;
25 28         357 my $self={p=>shift()};
26 28         48 my $name=shift;
27 28         42 my $obj=shift;
28 28         37 my $data=shift;
29 28         36 my $pos=shift;
30 28         40 my $found="";
31 28         131 $self->{true}=$obj->parse($data,$pos,\$found,"else","endif");
32 28 100       79 if ($found eq "else") {
33 23         82 $self->{false}=$obj->parse($data,$pos,\$found,"endif");
34             }
35 28         123 my @cond=Dotiac::DTL::get_variables($name);
36 28         49 my $c="cond";
37 28         54 foreach my $e (@cond) {
38 62 100       178 if ($e eq "and") {
    100          
    100          
39 4         5 $c="cond";
40             }
41             elsif ($e eq "or") {
42 8         11 $self->{"or"}=1;
43 8         14 $c="cond";
44             }
45             elsif ($e eq "not") {
46 10         20 $c="not";
47             }
48             else {
49 40         47 push @{$self->{$c}},$e;
  40         112  
50 40         90 $c="cond";
51             }
52             }
53 28         81 bless $self,$class;
54 28         108 return $self;
55             }
56             sub print {
57             #TODO Keep the order
58 46     46 1 69 my $self=shift;
59 46         186 print $self->{p};
60 46 100       114 if ($self->{"or"}) {
61 12         18 foreach my $e (@{$self->{"cond"}}) {
  12         29  
62 25         78 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
63 25 100       91 if ($r) {
64 6         25 $self->{true}->print(@_);
65 6         21 $self->{n}->print(@_);
66             return
67 6         14 }
68             }
69 6         10 foreach my $e (@{$self->{"not"}}) {
  6         17  
70 6         17 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
71 6 100       25 if (not $r) {
72 3         13 $self->{true}->print(@_);
73 3         11 $self->{n}->print(@_);
74             return
75 3         7 }
76             }
77 3 50       20 $self->{false}->print(@_) if $self->{false};
78 3         11 $self->{n}->print(@_);
79             }
80             else {
81             #die "'".join("','",@{$self->{"cond"}})."'\n";
82 34         39 foreach my $e (@{$self->{"cond"}}) {
  34         75  
83 30         101 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
84 30 100       110 if (not $r) {
85 16 100       75 $self->{false}->print(@_) if $self->{false};
86 16         49 $self->{n}->print(@_);
87 16         50 return;
88             }
89             }
90 18         26 foreach my $e (@{$self->{"not"}}) {
  18         38  
91 8         22 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
92 8 100       27 if ($r) {
93 4 100       14 $self->{false}->print(@_) if $self->{false};
94 4         13 $self->{n}->print(@_);
95 4         7 return;
96             }
97             }
98 14         51 $self->{true}->print(@_);
99 14         50 $self->{n}->print(@_);
100             }
101            
102             }
103             sub string {
104 46     46 1 62 my $self=shift;
105 46 100       114 if ($self->{"or"}) {
106 12         18 foreach my $e (@{$self->{"cond"}}) {
  12         28  
107 25         73 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
108 25 100       119 return $self->{p}.$self->{true}->string(@_).$self->{n}->string(@_) if $r;
109             }
110 6         13 foreach my $e (@{$self->{"not"}}) {
  6         16  
111 6         21 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
112 6 100       36 return $self->{p}.$self->{true}->string(@_).$self->{n}->string(@_) if not $r;
113             }
114 3 50       18 return $self->{p}.($self->{false}?$self->{false}->string(@_):"").$self->{n}->string(@_);
115             }
116             else {
117 34         39 foreach my $e (@{$self->{"cond"}}) {
  34         73  
118 30         84 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
119 30 100       176 return $self->{p}.($self->{false}?$self->{false}->string(@_):"").$self->{n}->string(@_) if not $r;
    100          
120             }
121 18         26 foreach my $e (@{$self->{"not"}}) {
  18         40  
122 8         21 my $r=Dotiac::DTL::devar_raw($e,@_)->true();
123 8 100       47 return $self->{p}.($self->{false}?$self->{false}->string(@_):"").$self->{n}->string(@_) if $r;
    100          
124             }
125 14         57 return $self->{p}.$self->{true}->string(@_).$self->{n}->string(@_);
126             }
127            
128             }
129             sub perl {
130 46     46 1 79 my $self=shift;
131 46         68 my $fh=shift;
132 46         72 my $id=shift;
133 46         183 $self->SUPER::perl($fh,$id,@_);
134 46         86 print $fh "my ";
135 46         239 print $fh (Data::Dumper->Dump([$self->{cond}],["\$cond$id"]));
136 46         2001 print $fh "my ";
137 46         233 print $fh (Data::Dumper->Dump([$self->{"not"}],["\$not$id"]));
138 46         1974 $id = $self->{true}->perl($fh,$id+1,@_);
139 46 100       237 $id = $self->{false}->perl($fh,$id+1,@_) if $self->{false};
140 46         295 return $self->{n}->perl($fh,$id+1,@_)
141             }
142             sub perlinit {
143 46     46 1 64 my $self=shift;
144 46         57 my $fh=shift;
145 46         54 my $id=shift;
146 46         162 $id = $self->{true}->perlinit($fh,$id+1,@_);
147 46 100       209 $id = $self->{false}->perlinit($fh,$id+1,@_) if $self->{false};
148 46         177 return $self->{n}->perlinit($fh,$id+1,@_)
149             }
150             sub perlprint {
151 46     46 1 64 my $self=shift;
152 46         60 my $fh=shift;
153 46         57 my $id=shift;
154 46         54 my $level=shift;
155 46         150 $self->SUPER::perlprint($fh,$id,$level,@_);
156 46 100       136 print $fh "\t" x $level,"if (",join(($self->{"or"}?" or ":" and "),(map {"Dotiac::DTL::devar_raw(\$cond$id"."->[$_],\$vars,\$escape.\@_)->true()"} (0 .. $#{$self->{"cond"}})),map {"not Dotiac::DTL::devar_raw(\$not$id"."->[$_],\$vars,\$escape.\@_)->true()"} (0 .. $#{$self->{"not"}})),") {\n";
  64         222  
  46         110  
  22         185  
  46         115  
157 46         206 $id = $self->{true}->perlprint($fh,$id+1,$level+1,@_);
158 46 100       124 if ($self->{false}) {
159 41         92 print $fh "\t" x $level,"} else {\n";
160 41         138 $id = $self->{false}->perlprint($fh,$id+1,$level+1,@_);
161             }
162 46         92 print $fh "\t" x $level,"}\n";
163 46         165 return $self->{n}->perlprint($fh,$id+1,$level,@_);
164             }
165             sub perlstring {
166 46     46 1 62 my $self=shift;
167 46         56 my $fh=shift;
168 46         55 my $id=shift;
169 46         48 my $level=shift;
170 46         172 $self->SUPER::perlstring($fh,$id,$level,@_);
171 46 100       154 print $fh "\t" x $level,"if (",join(($self->{"or"}?" or ":" and "),(map {"Dotiac::DTL::devar_raw(\$cond$id"."->[$_],\$vars,\$escape.\@_)->true()"} (0 .. $#{$self->{"cond"}})),map {"not Dotiac::DTL::devar_raw(\$not$id"."->[$_],\$vars,\$escape.\@_)->true()"} (0 .. $#{$self->{"not"}})),") {\n";
  64         246  
  46         107  
  22         112  
  46         602  
172 46         211 $id = $self->{true}->perlstring($fh,$id+1,$level+1,@_);
173 46 100       139 if ($self->{false}) {
174 41         84 print $fh "\t" x $level,"} else {\n";
175 41         145 $id = $self->{false}->perlstring($fh,$id+1,$level+1,@_);
176             }
177 46         91 print $fh "\t" x $level,"}\n";
178 46         174 return $self->{n}->perlstring($fh,$id+1,$level,@_);
179             }
180             sub perleval {
181 46     46 1 76 my $self=shift;
182 46         59 my $fh=shift;
183 46         53 my $id=shift;
184 46         152 $id=$self->{true}->perleval($fh,$id+1,@_);
185 46 100       190 $id=$self->{false}->perleval($fh,$id+1,@_) if $self->{false};
186 46         138 $self->{n}->perleval($fh,$id+1,@_);
187             }
188             sub perlcount {
189 0     0 1 0 my $self=shift;
190 0         0 my $id=shift;
191 0         0 $id=$self->{true}->perlcount($id+1,@_);
192 0 0       0 $id=$self->{false}->perlcount($id+1,@_) if $self->{false};
193 0         0 return $self->{n}->perlcount($id+1);
194             }
195             sub next {
196 28     28 1 44 my $self=shift;
197 28         106 $self->{n}=shift;
198             }
199             sub eval {
200 0     0 1   my $self=shift;
201 0           $self->{n}->eval(@_);
202             }
203             1;
204            
205             __END__