File Coverage

blib/lib/Dotiac/DTL/Tag/ifchanged.pm
Criterion Covered Total %
statement 144 151 95.3
branch 49 52 94.2
condition 6 9 66.6
subroutine 13 15 86.6
pod 11 11 100.0
total 223 238 93.7


line stmt bran cond sub pod time code
1             #ifchanged.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             #ifchanged.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::ifchanged;
17 11     11   65 use base qw/Dotiac::DTL::Tag/;
  11         25  
  11         927  
18 11     11   69 use strict;
  11         28  
  11         356  
19 11     11   67 use warnings;
  11         25  
  11         11839  
20            
21             our $VERSION = 0.8;
22            
23             sub new {
24 4     4 1 13 my $class=shift;
25 4         17 my $self={p=>shift()};
26 4         10 my $name=shift;
27 4         7 my $obj=shift;
28 4         9 my $data=shift;
29 4         8 my $pos=shift;
30 4         8 my $found="";
31 4         26 $self->{true}=$obj->parse($data,$pos,\$found,"else","endifchanged"); #Else might also be useful here, but not pyDjango style
32 4 100       18 if ($found eq "else") {
33 2         10 $self->{false}=$obj->parse($data,$pos,\$found,"endifchanged");
34             }
35 4         11 my $n=$name;
36 4 100       18 $name=~s/^\s+// if $name;
37 4 100       18 $name=~s/\s+$// if $name;
38 4 100 66     35 $self->{var}=(Dotiac::DTL::get_variables($n))[0] if $name and $name=~/\w/;
39 4         15 bless $self,$class;
40 4         20 return $self;
41             }
42             sub print {
43 43     43 1 79 my $self=shift;
44 43 100 66     267 $Dotiac::DTL::globals{ifchanged}->{$self} = "" unless $Dotiac::DTL::globals{ifchanged} and $Dotiac::DTL::globals{ifchanged}->{$self};
45 43         73 print $self->{p};
46 43         333 my $r = "";
47 43 100       93 if ($self->{var}) {
48 21         388 $r = Dotiac::DTL::devar($self->{var},@_);
49             }
50             else {
51 22         70 $r = $self->{true}->string(@_);
52             }
53 43 100       205 if ($r ne $Dotiac::DTL::globals{ifchanged}->{$self}) {
54 21         67 $Dotiac::DTL::globals{ifchanged}->{$self}=$r;
55 21 100       48 if ($self->{var}) {
56 11         55 $self->{true}->print(@_);
57             }
58             else {
59 10         28 print $r;
60             }
61             }
62             else {
63 22 100       146 $self->{false}->print(@_) if $self->{false};
64             }
65 43         140 $self->{n}->print(@_);
66             }
67             sub string {
68 43     43 1 65 my $self=shift;
69 43 100 66     554 $Dotiac::DTL::globals{ifchanged}->{$self} = "" unless $Dotiac::DTL::globals{ifchanged} and $Dotiac::DTL::globals{ifchanged}->{$self};
70 43         106 print $self->{p};
71 43         64 my $r = "";
72 43 100       90 if ($self->{var}) {
73 21         60 $r = Dotiac::DTL::devar($self->{var},@_);
74             }
75             else {
76 22         81 $r = $self->{true}->string(@_);
77             }
78 43 100       193 if ($r ne $Dotiac::DTL::globals{ifchanged}->{$self}) {
79 21         54 $Dotiac::DTL::globals{ifchanged}->{$self}=$r;
80 21 100       48 if ($self->{var}) {
81 11         54 return $self->{p}.$self->{true}->string(@_).$self->{n}->string(@_);
82             }
83             else {
84 10         39 return $self->{p}.$r.$self->{n}->string(@_);
85             }
86             }
87             else {
88 22 100       110 return $self->{p}.($self->{false}?$self->{false}->string(@_):"").$self->{n}->string(@_);
89             }
90            
91            
92             }
93             sub perl {
94 4     4 1 11 my $self=shift;
95 4         9 my $fh=shift;
96 4         10 my $id=shift;
97 4         29 $self->SUPER::perl($fh,$id,@_);
98 4 100       22 if ($self->{var}) {
99 2         5 print $fh "my ";
100 2         16 print $fh (Data::Dumper->Dump([$self->{var}],["\$var$id"]));
101             }
102 4         105 $id = $self->{true}->perl($fh,$id+1,@_);
103 4 100       30 $id = $self->{false}->perl($fh,$id+1,@_) if $self->{false};
104 4         23 return $self->{n}->perl($fh,$id+1,@_)
105             }
106             sub perlinit {
107 4     4 1 10 my $self=shift;
108 4         9 my $fh=shift;
109 4         8 my $id=shift;
110 4         27 $id = $self->{true}->perlinit($fh,$id+1,@_);
111 4 100       27 $id = $self->{false}->perlinit($fh,$id+1,@_) if $self->{false};
112 4         17 return $self->{n}->perlinit($fh,$id+1,@_)
113             }
114             sub perlprint {
115 4     4 1 11 my $self=shift;
116 4         10 my $fh=shift;
117 4         7 my $id=shift;
118 4         6 my $level=shift;
119 4         21 $self->SUPER::perlprint($fh,$id,$level,@_);
120 11 50   11   75 use Carp; confess unless $_[0];
  11         20  
  11         17595  
  4         25  
121 4         15 my $name="\"$_[0]-$id\"";
122 4         10 my $nid=$id;
123 4 100       13 if ($self->{var}) {
124 2         11 print $fh "\t" x $level,"\$Dotiac::DTL::globals{ifchanged}->{$name} = \"\" unless \$Dotiac::DTL::globals{ifchanged} and \$Dotiac::DTL::globals{ifchanged}->{$name};\n";
125 2         9 print $fh "\t" x $level,"my \$change$id = Dotiac::DTL::devar(\$var$id,\$vars,\$escape,\@_);\n";
126 2         8 print $fh "\t" x $level,"if (\$Dotiac::DTL::globals{ifchanged}->{$name} ne \$change$id) {\n";
127 2         8 print $fh "\t" x $level,"\t\$Dotiac::DTL::globals{ifchanged}->{$name}=\$change$id;\n";
128 2         15 $nid = $self->{true}->perlprint($fh,$id+1,$level+1,@_);
129 2 100       8 if ($self->{false}) {
130 1         5 print $fh "\t" x $level,"} else {\n";
131 1         6 $nid = $self->{false}->perlprint($fh,$nid+1,$level+1,@_);
132             }
133 2         7 print $fh "\t" x $level,"}\n";
134             }
135             else {
136 2         14 print $fh "\t" x $level,"\$Dotiac::DTL::globals{ifchanged}->{$name} = \"\" unless \$Dotiac::DTL::globals{ifchanged} and \$Dotiac::DTL::globals{ifchanged}->{$name};\n";
137 2         8 print $fh "\t" x $level,"my \$change$id = \"\";\n";
138 2         5 print $fh "\t" x $level,"{\n";
139 2         4 print $fh "\t" x $level,"\tmy \$r=\"\";\n";
140 2         12 $nid = $self->{true}->perlstring($fh,$id+1,$level+1,@_);
141 2         9 print $fh "\t" x $level,"\$change$id = \$r;\n";
142 2         6 print $fh "\t" x $level,"}\n";
143 2         7 print $fh "\t" x $level,"if (\$Dotiac::DTL::globals{ifchanged}->{$name} ne \$change$id) {\n";
144 2         11 print $fh "\t" x $level,"\t\$Dotiac::DTL::globals{ifchanged}->{$name}=\$change$id;\n";
145 2         8 print $fh "\t" x $level,"\tprint \$change$id;\n";
146 2 100       8 if ($self->{false}) {
147 1         3 print $fh "\t" x $level,"} else {\n";
148 1         6 $nid = $self->{false}->perlprint($fh,$nid+1,$level+1,@_);
149             }
150 2         5 print $fh "\t" x $level,"}\n";
151             }
152 4         23 return $self->{n}->perlprint($fh,$nid+1,$level,@_);
153             }
154             sub perlstring {
155 4     4 1 10 my $self=shift;
156 4         6 my $fh=shift;
157 4         9 my $id=shift;
158 4         6 my $level=shift;
159 4         32 $self->SUPER::perlstring($fh,$id,$level,@_);
160 4         13 my $name="\"$_[0]-$id\"";
161 4         9 my $nid=$id;
162 4 100       15 if ($self->{var}) {
163 2         11 print $fh "\t" x $level,"\$Dotiac::DTL::globals{ifchanged}->{$name} = \"\" unless \$Dotiac::DTL::globals{ifchanged} and \$Dotiac::DTL::globals{ifchanged}->{$name};\n";
164 2         9 print $fh "\t" x $level,"my \$change$id = Dotiac::DTL::devar(\$var$id,\$vars,\$escape,\@_);\n";
165 2         11 print $fh "\t" x $level,"if (\$Dotiac::DTL::globals{ifchanged}->{$name} ne \$change$id) {\n";
166 2         10 print $fh "\t" x $level,"\t\$Dotiac::DTL::globals{ifchanged}->{$name}=\$change$id;\n";
167 2         16 $nid = $self->{true}->perlstring($fh,$id+1,$level+1,@_);
168 2 100       11 if ($self->{false}) {
169 1         3 print $fh "\t" x $level,"} else {\n";
170 1         6 $nid = $self->{false}->perlstring($fh,$nid+1,$level+1,@_);
171             }
172 2         7 print $fh "\t" x $level,"}\n";
173             }
174             else {
175 2         11 print $fh "\t" x $level,"\$Dotiac::DTL::globals{ifchanged}->{$name} = \"\" unless \$Dotiac::DTL::globals{ifchanged} and \$Dotiac::DTL::globals{ifchanged}->{$name};\n";
176 2         9 print $fh "\t" x $level,"my \$change$id = \"\";\n";
177 2         5 print $fh "\t" x $level,"{\n";
178 2         5 print $fh "\t" x $level,"\tmy \$r=\"\";\n";
179 2         16 $nid = $self->{true}->perlstring($fh,$id+1,$level+1,@_);
180 2         7 print $fh "\t" x $level,"\t\$change$id=\$r;\n";
181 2         6 print $fh "\t" x $level,"}\n";
182 2         7 print $fh "\t" x $level,"if (\$Dotiac::DTL::globals{ifchanged}->{$name} ne \$change$id) {\n";
183 2         9 print $fh "\t" x $level,"\t\$Dotiac::DTL::globals{ifchanged}->{$name}=\$change$id;\n";
184 2         6 print $fh "\t" x $level,"\t\$r.=\$change$id;\n";
185 2 100       8 if ($self->{false}) {
186 1         3 print $fh "\t" x $level,"} else {\n";
187 1         6 $nid = $self->{false}->perlstring($fh,$nid+1,$level+1,@_);
188             }
189 2         6 print $fh "\t" x $level,"}\n";
190             }
191 4         17 return $self->{n}->perlstring($fh,$nid+1,$level,@_);
192             }
193             sub perleval {
194 4     4 1 8 my $self=shift;
195 4         6 my $fh=shift;
196 4         9 my $id=shift;
197 4         179 $id=$self->{true}->perleval($fh,$id+1,@_);
198 4 100       24 $id=$self->{false}->perleval($fh,$id+1,@_) if $self->{false};
199 4         18 $self->{n}->perleval($fh,$id+1,@_);
200             }
201             sub perlcount {
202 0     0 1 0 my $self=shift;
203 0         0 my $id=shift;
204 0         0 $id=$self->{true}->perlcount($id+1,@_);
205 0 0       0 $id=$self->{false}->perlcount($id+1,@_) if $self->{false};
206 0         0 return $self->{n}->perlcount($id+1);
207             }
208             sub next {
209 4     4 1 9 my $self=shift;
210 4         30 $self->{n}=shift;
211             }
212             sub eval {
213 0     0 1   my $self=shift;
214 0           $self->{n}->eval(@_);
215             }
216             1;
217            
218             __END__