blib/lib/CAM/Template.pm

filter: Statementstmt 80.5% 178 / 221 Branchbran 62.2% 61 / 98 Conditioncond 53.6% 22 / 41 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 85.7% 18 / 21 Podpod 100% 15 / 15 totaltotal 71.6% 300 / 419 CC 69 SCAR 26.6
CC
69
Cov
72%
toString
39.1
study
13.8
addLoop
13.2
CRAP 14.2
↑ Summary
1 package CAM::Template;
2  
3 =head1 NAME
4  
5 CAM::Template - Clotho-style search/replace HTML templates
6  
7 =head1 LICENSE
8  
9 Copyright 2005 Clotho Advanced Media, Inc., <cpan@clotho.com>
10  
11 This library is free software; you can redistribute it and/or modify it
12 under the same terms as Perl itself.
13  
14 =head1 SYNOPSIS
15  
16 use CAM::Template;
17 my $tmpl = new CAM::Template($tmpldir . "/main_tmpl.html");
18 $tmpl->addParams(url => "http://foo.com/",
19 date => localtime(),
20 name => "Carol");
21 $tmpl->addParams(\%more_params);
22 $tmpl->setLoop("birthdaylist", name => "Eileen", date => "Sep 12");
23 $tmpl->addLoop("birthdaylist", name => "Chris", date => "Oct 13");
24 $tmpl->addLoop("birthdaylist", [{name => "Dan", date => "Feb 12"},
25 {name => "Scott", date => "Sep 24"}]);
26 print "Content-Type: text/html\n\n";
27 $tmpl->print();
28  
29 =head1 DESCRIPTION
30  
31 This package is intended to replace Clotho's traditional ::PARAM::
32 syntax with an object-oriented API. This syntax is overrideable by
33 subclasses. See the last section of this documentation for an
34 explanation of the default syntax.
35  
36 We recommend that you DO NOT use this module unless you have a good
37 reason. The other CPAN templating modules, like HTML::Template and
38 Template::Toolkit) are better maintained than this one. See
39 L<http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html>
40 for an excellent discussion of the various templating approaches.
41  
42 So why does this module exist? Legacy, mostly. A ton of HTML was
43 written in this templating language. So, we keep this module in good
44 condition. Additionally, we believe it's unique in the Perl community
45 in that it has a reconfigurable template syntax. That's worth a
46 little, we think.
47  
48 =cut
49  
50 #==============================
51  
52 2 require 5.005_62;
53 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 our @ISA = qw();
58 2 our $VERSION = '0.93';
59  
60 ## Global package settings
61  
62 2 our $global_include_files = 1;
63  
64 # Flags for the in-memory file cache
65 2 our $global_use_cache = 1;
66 2 our %global_filecache = ();
67  
68 #==============================
69  
70 =head1 FUNCTIONS
71  
72 =over 4
73  
74 =cut
75  
76 #==============================
77  
78 =item patterns
79  
80 This class method returns a series of regular expressions used for
81 template searching and replacing. Modules which subclass
82 CAM::Template can override this method to implement a different
83 template syntax.
84  
85 Example of the recommended way to write an override function for this
86 method in a subclass:
87  
88 sub patterns {
89 my $pkg = shift;
90 return {
91 $pkg->SUPER::patterns(),
92 # include syntax: <? include("subfile.tmpl") ?>
93 include => qr/<? *include\(\"([^\"]+)\"\) *?>/,
94 };
95 }
96  
97 =cut
98  
99 sub patterns
100 1002 ▶{
Subroutine patterns: called CC=1 SCAR=0.0
Pod documented
101 1002 my $pkg = shift;
102  
103 return {
104 # $1 is the loop name
105 1002 loopstart => qr/<cam_loop\s+name\s*=\s*\"?([\w\-]+)\"?>/i,
106 loopend => qr/<\/cam_loop>/i,
107  
108 # a string that looks like one of the "vars" below for
109 # substituting the loop variable. This will be used in:
110 # $template =~ s/loop-pattern/loop_out-pattern/;
111 loop_out => '::$1::',
112  
113 # DEPRECATED
114 # $1 is the variable name, $2 is the conditional body
115 #if => qr/\?\?([\w\-]+?)\?\?(.*?)\?\?\1\?\?/s,
116 #unless => qr/\?\?!([\w\-]+?)\?\?(.*?)\?\?!\1\?\?/s,
117  
118 # $1 is a boolean flag, $2 is the variable name,
119 # $3 is the conditional body
120 ifunless => qr/\?\?(!?)([\w\-]+?)\?\?(.*?)\?\?\1\2\?\?/s,
121 ifunless_test => qr/^!$/s,
122  
123  
124 # $1 is the variable name
125 vars => [
126 qr/<!--\s*::([\w\-]+?)::\s*-->/s,
127 qr/::([\w\-]+?)::/s,
128 qr/;;([\w\-]+?);;/s,
129 ],
130
131 # $1 is the variable name, $2 is the value to set it to
132 staticvars => qr/::([\w\-]+)==(.{0,80}?)::/,
133  
134 # $1 is the subtemplate filename
135 include => qr/<!\-\-\s*\#include\s+template=\"([^\"]+)\"\s*\-\->/,
136 };
137 }
138 #==============================
139  
140 =item new
141  
142 =item new FILENAME
143  
144 =item new FILENAME, PARAMS
145  
146 Create a new template object. You can specify the template filename and
147 the replacement dictionary right away, or do it later via methods.
148  
149 =cut
150  
151 sub new
152 1002 ▶{
Subroutine new: called CC=5 SCAR=19.1
Pod documented
153 1002 my $pkg = shift;
154  
155 1002 my $self = bless({
156 content => undef,
157 params => {},
158 use_cache => $global_use_cache,
159 include_files => $global_include_files,
160 patterns => $pkg->patterns(),
161 isloop => 0,
162 }, $pkg);
163  
164 1002 ▶ if (@_ > 0 && !$self->setFilename(shift))
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and not $self->setFilename(shift())) 0 1002 1002
Condition66%2/3
@_ > 0 and not $self->setFilename(shift())
!ll&&!rl&&r
100110
MC/DC0%0/2
@_ > 0 and not $self->setFilename(shift())
@_ > 0 not $self->setFilename(shift())
Truth table66%2/3
A and not(B)
A= @_ > 0B= not $self->setFilename(shift())
ABresult
0X0
100
111
165 {
166 0 return undef;
167 }
168 1002 ▶ if (@_ > 0 && !$self->addParams(@_))
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and not $self->addParams(@_)) 0 1002 1002
Condition33%1/3
@_ > 0 and not $self->addParams(@_)
!ll&&!rl&&r
100200
MC/DC0%0/2
@_ > 0 and not $self->addParams(@_)
@_ > 0 not $self->addParams(@_)
Truth table33%1/3
A and not(B)
A= @_ > 0B= not $self->addParams(@_)
ABresult
0X0
100
111
169 {
170 0 return undef;
171 }
172  
173 1002 return $self;
174 }
175 #==============================
176  
177 =item setFileCache 0|1
178  
179 Indicate whether the template file should be cached in memory.
180 Defaults to 1 (aka true). This can be used either on an object or
181 globally:
182  
183 my $tmpl = new CAM::Template();
184 $tmpl->setFileCache(0);
185 or
186 CAM::Template->setFileCache(0);
187  
188 The global value only affects future template objects, not existing
189 ones.
190  
191 =cut
192  
193 sub setFileCache
194 1002 ▶{
Subroutine setFileCache: called CC=2 SCAR=7.6
Pod documented
195 1002 my $self = shift;
196 1002 my $bool = shift;
197  
198 1002 ▶ if (ref($self))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1002 0 1002
199 {
200 1002 $self->{use_cache} = $bool;
201 1002 return $self;
202 }
203 else
204 {
205 0 $global_use_cache = $bool;
206 0 return 1;
207 }
208 }
209 #==============================
210  
211 =item setIncludeFiles 0|1
212  
213 Indicate whether the template file should be able to include other template files automatically via the
214  
215 <!-- #include template="<filename>" -->
216  
217 directive. Defaults to 1 (aka true). Note that this is recursive, so
218 don't have a file include itself! This method can be used either on
219 an object or globally:
220  
221 my $tmpl = new CAM::Template();
222 $tmpl->setIncludeFiles(0);
223 or
224 CAM::Template->setIncludeFiles(0);
225  
226 The global value only affects future template objects, not existing
227 ones.
228  
229 =cut
230  
231 sub setIncludeFiles
232 0 ▶{
Subroutine setIncludeFiles: not called CC=2 SCAR=17.9
Pod documented
233 0 my $self = shift;
234 0 my $bool = shift;
235  
236 0 ▶ if (ref($self))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
237 {
238 0 $self->{include_files} = $bool;
239 0 return $self;
240 }
241 else
242 {
243 0 $global_include_files = $bool;
244 0 return 1;
245 }
246 }
247 #==============================
248  
249 =item setFilename FILENAME
250  
251 Specify the template file to be used. Returns false if the file does
252 not exist or the object if it does. This loads and preparses the file.
253  
254 =cut
255  
256 sub setFilename
257 1005 ▶{
Subroutine setFilename: called CC=3 SCAR=12.3
Pod documented
258 1005 my $self = shift;
259 1005 my $filename = shift;
260  
261 # Validate input
262 1005 ▶ if ((! $filename) || (! -r $filename))
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename and -r $filename) 0 1005 1005
Condition33%1/3
$filename and -r $filename
!ll&&!rl&&r
001005
MC/DC0%0/2
$filename and -r $filename
$filename -r $filename
Truth table33%1/3
A and B
A= $filenameB= -r $filename
ABresult
0X0
100
111
263 {
264 0 &carp("File '$filename' cannot be read");
265 0 return undef;
266 }
267 1005 $self->{content} = $self->_fetchfile($filename);
268 1005 $self->{filename} = $filename;
269 1005 $self->_preparse();
270 1005 return $self;
271 }
272 #==============================
273  
274 =item setString STRING
275  
276 Specify template content to be used. Use this instead of setFilename if
277 you already have the contents in memory. This preparses the string.
278  
279 =cut
280  
281 sub setString
282 3 ▶{
Subroutine setString: called CC=1 SCAR=0.0
Pod documented
283 3 my $self = shift;
284 $self->{content} = {
285 3 string => shift,
286 studied => 0,
287 skip => {},
288 };
289 3 delete $self->{filename};
290 3 $self->_preparse();
291 3 return $self;
292 }
293 #==============================
294  
295 =item loopClass
296  
297 Template loops (i.e. C<addLoop>) usually instantiate new template
298 objects to populate the loop body. In general, we want the new
299 instance to be the same class as the main template object. However,
300 in some subclasses of CAM::Template, this is a bad thing (for example
301 PDF templates with loops in their individual pages).
302  
303 In the latter case, the subclass should override this method with
304 something like the following:
305  
306 sub loopClass { "CAM::Template" }
307  
308 =cut
309  
310 sub loopClass
311 1 ▶{
Subroutine loopClass: called CC=2 SCAR=8.1
Pod documented
312 1 my $pkg_or_self = shift;
313  
314 1 ▶ return ref($pkg_or_self) || $pkg_or_self;
Condition33%1/3
ref $pkg_or_self || $pkg_or_self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $pkg_or_self || $pkg_or_self
ref $pkg_or_self $pkg_or_self
Truth table33%1/3
A || B
A= ref $pkg_or_selfB= $pkg_or_self
ABresult
1X1
011
000
315 }
316 #==============================
317  
318 =item addLoop LOOPNAME, HASHREF | KEY => VALUE, ...
319  
320 =item addLoop LOOPNAME, ARRAYREF
321  
322 Add to an iterating portion of the page. This extracts the <cam_loop>
323 from the template, fills it with the specified parameters (and any
324 previously specified with setParams() or addParams()), and appends to
325 the LOOPNAME parameter in the params list.
326  
327 If the ARRAYREF form of the method is used, it behaves as if you had done:
328  
329 foreach my $row (@$ARRAYREF) {
330 $tmpl->addLoop($LOOPNAME, $row);
331 }
332  
333 so, the elements of the ARRAYREF are hashrefs representing a series of
334 rows to be added.
335  
336 =cut
337  
338 sub addLoop
339 7 ▶{
Subroutine addLoop: called CC=12 SCAR=25.8
Pod documented
340 7 my $self = shift;
341 7 my $loopname = shift;
342 # additional params are collected below
343  
344 7 ▶ return undef if (!$self->{content});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"content"} 0 7 7
345 7 ▶ return undef if (!defined $self->{content}->{loops}->{$loopname});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"content"}{"loops"}{$loopname} 0 7 7
346  
347 7 ▶ while (@_ > 0 && $_[0] && ref($_[0]) && ref($_[0]) eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and $_[0] and ref $_[0] and ref $_[0] eq "ARRAY") 1 6 7
Condition66%6/9
@_ > 0 and $_[0] and ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
331
@_ > 0 and $_[0] and ref $_[0]
!ll&&!rl&&r
034
@_ > 0 and $_[0]
!ll&&!rl&&r
007
MC/DC50%2/4
@_ > 0 and $_[0] and ref $_[0] and ref $_[0] eq "ARRAY"
@_ > 0 $_[0] ref $_[0] ref $_[0] eq "ARRAY"
Truth table60%3/5
A and B and C and D
A= @_ > 0B= $_[0]C= ref $_[0]D= ref $_[0] eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
348 {
349 1 my $looparray = shift;
350 1 foreach my $loop (@$looparray)
351 {
352 3 ▶ if (!$self->addLoop($loopname, $loop))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->addLoop($loopname, $loop)) 0 3 3
353 {
354 0 return undef;
355 }
356 }
357 # If we run out of arrayrefs, quit
358 1 ▶ if (@_ == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 1 0 1
359 {
360 1 return $self;
361 }
362 }
363  
364 6 my $looptemplate = $self->{content}->{loop_cache}->{$loopname};
365 6 ▶ if (!$looptemplate)
Branch: 1 branch
BranchTrueFalseTotal
unless ($looptemplate) 1 5 6
366 {
367 1 $self->{content}->{loop_cache}->{$loopname} =
368 $looptemplate = $self->loopClass()->new();
369 $looptemplate->{content} = {
370 1 skip => {%{$self->{content}->{skip}}},
371 string => $self->{content}->{loops}->{$loopname},
372 staticparams => $self->{content}->{staticparams},
373 1 };
374 1 ▶ $looptemplate->study() if ($self->{content}->{studied});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content"}{"studied"} 0 1 1
375 }
376 6 $looptemplate->setParams(\%{$self->{params}}, $loopname => "", @_);
377 6 $self->{params}->{$loopname} .= $looptemplate->toString();
378 6 return $self;
379 }
380 #==============================
381  
382 =item clearLoop LOOPNAME
383  
384 Blank the contents of the loop accumlator. This is really only useful
385 for nested loops. For example:
386  
387 foreach my $state (@states) {
388 $template->clearLoop("cities");
389 foreach my $city (@{$state->{cities}}) {
390 $template->addLoop("cities",
391 city => $city->{name},
392 pop => $city->{population});
393 }
394 $template->addLoop("state", state => $state->{name});
395 }
396  
397 =cut
398  
399 sub clearLoop
400 0 ▶{
Subroutine clearLoop: not called CC=1 SCAR=6.9
Pod documented
401 0 my $self = shift;
402 0 my $loopname = shift;
403  
404 0 $self->{params}->{$loopname} = "";
405 0 return $self;
406 }
407 #==============================
408  
409 =item setLoop LOOPNAME, HASHREF | KEY => VALUE, ...
410  
411 Exactly like addLoop above, except it clears the loop first. This is
412 useful for the first element of a nested loop.
413  
414 =cut
415  
416 sub setLoop
417 0 ▶{
Subroutine setLoop: not called CC=1 SCAR=6.9
Pod documented
418 0 my $self = shift;
419 0 my $loopname = shift;
420  
421 0 $self->clearLoop($loopname);
422 0 return $self->addLoop($loopname, @_);
423 }
424 #==============================
425  
426 =item study
427  
428 Takes a moment to analyze the template to see if any time can be
429 gained by skipping unused portions of the replacement syntax. This is
430 obviously more useful for templates that are replaced often, like
431 loops.
432  
433 Implementation note as of v0.77: In practice this rarely helps except
434 on large, simplistic templates. Hopefully this will improve in the
435 future.
436  
437 =cut
438  
439 sub study
440 2 ▶{
Subroutine study: called CC=13 SCAR=26.2
Pod documented
441 2 my $self = shift;
442
443 2 ▶ return undef if (!$self->{content});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"content"} 0 2 2
444 2 ▶ return undef if (!defined $self->{content}->{string});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"content"}{"string"} 0 2 2
445 #study $self->{content}->{string};
446 2 my $re_hash = $self->{patterns};
447 2 my $content = $self->{content};
448 2 foreach my $key ("if", "unless", "ifunless")
449 {
450 6 ▶ next if (!$re_hash->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $re_hash->{$key} 4 2 6
451 2 ▶ next if ($content->{skip}->{$key}); # for loops
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"skip"}{$key} 0 2 2
452 2 ▶ if ($content->{string} !~ /$$re_hash{$key}/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->{"string"} =~ /$re_hash->{$key}/) 1 1 2
453 {
454 1 $content->{skip}->{$key} = 1;
455 }
456 }
457  
458 2 my $i = 0;
459 2 foreach my $re (@{$re_hash->{vars}})
460 {
461 6 my $key = "vars".++$i;
462 6 ▶ next if ($content->{skip}->{$key}); # for loops
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"skip"}{$key} 0 6 6
463 6 ▶ if ($content->{string} !~ /$re/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->{"string"} =~ /$re/) 3 3 6
464 {
465 3 $content->{skip}->{$key} = 1;
466 }
467 }
468  
469 $content->{skip}->{cond} = 1 if (($content->{skip}->{if} &&
470 $content->{skip}->{unless}) ||
471 2 ▶ $content->{skip}->{ifunless});
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"skip"}{"if"} and $content->{"skip"}{"unless"} or $content->{"skip"}{"ifunless"} 1 1 2
Condition50%3/6
$content->{"skip"}{"if"} and $content->{"skip"}{"unless"} or $content->{"skip"}{"ifunless"}
l!l&&r!l&&!r
011
$content->{"skip"}{"if"} and $content->{"skip"}{"unless"}
!ll&&!rl&&r
200
MC/DC33%1/3
$content->{"skip"}{"if"} and $content->{"skip"}{"unless"} or $content->{"skip"}{"ifunless"}
$content->{"skip"}{"if"} $content->{"skip"}{"unless"} $content->{"skip"}{"ifunless"}
Truth table40%2/5
A and B or C
A= $content->{"skip"}{"if"}B= $content->{"skip"}{"unless"}C= $content->{"skip"}{"ifunless"}
ABCresult
11X1
0X11
1011
0X00
1000
472 2 $content->{studied} = 1;
473 2 return $self;
474 }
475 #==============================
476  
477 =item addParams [HASHREF | KEY => VALUE], ...
478  
479 Specify the search/replace dictionary for the template. The arguments
480 can either be key value pairs, or hash references (it is permitted to
481 mix the two as of v0.71 of this library). For example:
482  
483 my %hash = (name => "chris", age => 30);
484 $tmpl1->addParams(%hash);
485
486 my $hashref = \%hash;
487 $tmpl2->addParams($hashref);
488  
489 Returns false if the hash has an uneven number of arguments, or the
490 argument is not a hash reference. Returns the object otherwise.
491  
492 Note: this I<appends> to the parameter list. To replace the list, use
493 the setParams method instead.
494  
495 =cut
496  
497 sub addParams
498 8 ▶{
Subroutine addParams: called CC=7 SCAR=21.5
Pod documented
499 8 my $self = shift;
500 # additional arguments processed below
501  
502  
503 # store everything up in a temp hash so we can detect errors and
504 # quit before applying these params to the object.
505 8 my %params = ();
506  
507 8 ▶ while (@_ > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 22 8 30
508 {
509 22 ▶ if (!defined $_[0])
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 0 22 22
elsif (ref $_[0]) { } 10 12 22
elsif (@_ == 1) { } 0 12 12
510 {
511 0 &carp("Undefined key in the parameter list");
512 0 return undef;
513 }
514 elsif (ref($_[0]))
515 {
516 10 my $ref = shift;
517 10 ▶ if (ref($ref) =~ /^(?:SCALAR|ARRAY|CODE)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (ref($ref) =~ /^(?:SCALAR|ARRAY|CODE)$/) 0 10 10
518 {
519 0 &carp("Parameter list has a reference that is not a hash reference");
520 0 return undef;
521 }
522 10 %params = (%params, %$ref);
523 }
524 elsif (@_ == 1)
525 {
526 0 &carp("Uneven number of arguments in key/value pair list");
527 0 return undef;
528 }
529 else
530 {
531 # get a key value pair
532 12 my $key = shift;
533 12 $params{$key} = shift;
534 }
535 }
536  
537 8 foreach my $key (keys %params)
538 {
539 47 $self->{params}->{$key} = $params{$key};
540 }
541 8 return $self;
542 }
543 #==============================
544  
545 =item setParams HASHREF | KEY => VALUE, ...
546  
547 Exactly like addParams above, except it clears the parameter list first.
548  
549 =cut
550  
551 sub setParams
552 7 ▶{
Subroutine setParams: called CC=1 SCAR=0.0
Pod documented
553 7 my $self = shift;
554
555 7 $self->{params} = {};
556 7 return $self->addParams(@_);
557 }
558 #==============================
559  
560 # PRIVATE FUNCTION
561 sub _preparse
562 1008 ▶{
Subroutine _preparse: called CC=4 SCAR=13.9
563 1008 my $self = shift;
564  
565 1008 my $content = $self->{content};
566 1008 ▶ return $self if ($content->{parsed});
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"parsed"} 501 507 1008
567  
568 507 $content->{skip} = {};
569 507 $content->{studied} = 0;
570 507 $content->{loops} = {};
571 507 $content->{loop_cache} = {};
572 507 $content->{staticparams} = {};
573  
574 # Retrieve constant parameters set in the template files
575 507 my $static_re = $self->{patterns}->{staticvars};
576 507 $content->{string} =~ s/$static_re/$$content{staticparams}{$1}=$2; ""/ge;
577  
578 # Break up all loops
579 507 my $re1 = $self->{patterns}->{loopstart};
580 507 my $re2 = $self->{patterns}->{loopend};
581 507 my ($start,$end) = split /\$1/, $self->{patterns}->{loop_out}, 2;
582 507 my @parts = split /$re1/, $content->{string};
583 507 ▶ while (@parts > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 2) 505 507 1012
584 505 my $tail = pop @parts;
585 505 my $name = pop @parts;
586 505 ▶ if ($tail =~ s/^(.*?)$re2/$start$name$end/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ s/^(.*?)$re2/$start$name$end/s) { } 505 0 505
587 {
588 505 $content->{loops}->{$name} = $1;
589 }
590 else
591 {
592 0 warn "Found loop start for '$name' but no loop end";
593 }
594 505 $parts[$#parts] .= $tail;
595 }
596 507 $content->{string} = $parts[0];
597 507 $content->{parsed} = 1;
598  
599 507 return $self;
600 }
601 #==============================
602  
603 # PRIVATE FUNCTION
604 sub _fetchfile
605 1005 ▶{
Subroutine _fetchfile: called CC=10 SCAR=23.3
606 1005 my $self = shift;
607 1005 my $filename = shift;
608  
609 1005 my $cache;
610 1005 ▶ if ($self->{use_cache})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_cache"}) 502 503 1005
611 {
612 502 my $pkg = ref($self);
613 502 ▶ $global_filecache{$pkg} ||= {};
Condition100%2/2
$CAM::Template::global_filecache{$pkg} ||= {}
l!l
5011
MC/DC100%1/1
$CAM::Template::global_filecache{$pkg} ||= {}
$CAM::Template::global_filecache{$pkg}
Truth table100%2/2
A
A= $CAM::Template::global_filecache{$pkg}
Aresult
11
00
614 502 $cache = $global_filecache{$pkg};
615 }
616
617 1005 ▶ if ($self->{use_cache} && exists $cache->{$filename} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_cache'} and exists $cache->{$filename} and $cache->{$filename}{'time'} >= (stat $filename)[9]) { } 501 504 1005
Condition83%5/6
$self->{"use_cache"} and exists $cache->{$filename} and $cache->{$filename}{"time"} >= (stat $filename)[9]
!ll&&!rl&&r
5040501
$self->{"use_cache"} and exists $cache->{$filename}
!ll&&!rl&&r
5031501
MC/DC66%2/3
$self->{"use_cache"} and exists $cache->{$filename} and $cache->{$filename}{"time"} >= (stat $filename)[9]
$self->{"use_cache"} exists $cache->{$filename} $cache->{$filename}{"time"} >= (stat $filename)[9]
Truth table75%3/4
A and B and C
A= $self->{"use_cache"}B= exists $cache->{$filename}C= $cache->{$filename}{"time"} >= (stat $filename)[9]
ABCresult
0XX0
10X0
1100
1111
618 $cache->{$filename}->{time} >= (stat($filename))[9])
619 {
620 501 return $cache->{$filename};
621 }
622 else
623 {
624 504 my $struct = {
625 studied => 0,
626 skip => {},
627 };
628 504 local *FILE;
629 504 ▶ if (!open(FILE, $filename))
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, $filename) 0 504 504
630 {
631 0 &carp("Failed to open file '$filename': $!");
632 0 return undef;
633 }
634 504 local $/ = undef;
635 504 $struct->{string} = <FILE>;
636 504 close(FILE);
637  
638 504 ▶ if ($self->{include_files})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_files"}) 504 0 504
639 {
640 # Recursively add included files -- must be in the same directory
641 504 my $dir = $filename;
642 504 $dir =~ s,/[^/]+$,,; # remove filename
643 504 ▶ $dir .= "/" if ($dir =~ /[^\/]$/);
Branch: 1 branch
BranchTrueFalseTotal
if $dir =~ m[[^/]$] 504 0 504
644 504 my $re = $self->{patterns}->{include};
645 0 $struct->{string} =~ s/$re/ $self->_fetchfile("$dir$1")->{string} /ge;
646 }
647  
648 504 ▶ if ($self->{use_cache})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_cache"}) 1 503 504
649 {
650 1 $struct->{time} = (stat($filename))[9];
651 1 $cache->{$filename} = $struct;
652 }
653 504 return $struct;
654 }
655 }
656  
657 #==============================
658  
659 =item toString
660  
661 Executes the search/replace and returns the content.
662  
663 =cut
664  
665 sub toString
666 3010 ▶{
Subroutine toString: called CC=17 SCAR=36.7
Pod documented
667 3010 my $self = shift;
668  
669 3010 ▶ return "" unless ($self->{content});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"content"} 0 3010 3010
670 3010 my $content = $self->{content}->{string};
671 3010 ▶ return "" unless (defined $content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 3010 3010
672  
673 3010 my $re_hash = $self->{patterns};
674 3010 my $skip = $self->{content}->{skip};
675 {
676 # Turn off warnings, since it is likely that some parameters
677 # will be undefined
678 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
679  
680 # incoming params can override template params
681 my %params = (
682 "__filename__" => $self->{filename},
683 3010 %{$self->{content}->{staticparams}},
684 3010 %{$self->{params}},
685 );
686  
687 3010 ▶ unless ($skip->{cond})
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip->{"cond"}) 2510 500 3010
688 {
689 # Do the following multiple times to handle nested conditionals
690  
691 2510 ▶ if ($re_hash->{if} && $re_hash->{unless}) # legacy subclassing
Branch: 1 branch
BranchTrueFalseTotal
if ($re_hash->{'if'} and $re_hash->{'unless'}) { } 0 2510 2510
Condition33%1/3
$re_hash->{"if"} and $re_hash->{"unless"}
!ll&&!rl&&r
251000
MC/DC0%0/2
$re_hash->{"if"} and $re_hash->{"unless"}
$re_hash->{"if"} $re_hash->{"unless"}
Truth table33%1/3
A and B
A= $re_hash->{"if"}B= $re_hash->{"unless"}
ABresult
0X0
100
111
692 {
693  
694 0 &carp("DEPRECATED: please use 'ifunless' instead of 'if' and 'unless'\n" .
695 "in your patterns. There was a subtle bug in the old way, and\n" .
696 "the new way is too slow with 'if' and 'unless'\n");
697  
698 0 my $pos = 1;
699 0 my $neg = 1;
700 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$neg || $pos and do { do { if ($neg) { $neg = $content =~ s/$re_hash->{"unless"}/!$params{$1} ? $2 : "";/eg; } ; if ($pos) { $pos = $content =~ s/$re_hash->{"if"}/$params{$1} ? $2 : "";/eg; } } } 0 0 0
Condition0%0/3
$neg or $pos
l!l&&r!l&&!r
000
MC/DC0%0/2
$neg or $pos
$neg $pos
Truth table0%0/3
A or B
A= $negB= $pos
ABresult
1X1
011
000
701 0 ▶ if ($neg)
Branch: 1 branch
BranchTrueFalseTotal
if ($neg) 0 0 0
702 {
703 0 ▶ $neg = ($content =~ s/$$re_hash{unless}/(!$params{$1}) ? $2 : ''/ge);
Branch: 1 branch
BranchTrueFalseTotal
!$params{$1} ? : 0 0 0
704 }
705 0 ▶ if ($pos)
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) 0 0 0
706 {
707 0 ▶ $pos = ($content =~ s/$$re_hash{if}/$params{$1} ? $2 : ''/ge);
Branch: 1 branch
BranchTrueFalseTotal
$params{$1} ? : 0 0 0
708 }
709 } while ($neg || $pos);
710 }
711 else
712 {
713 2510 ▶ do {} while ($content =~ s/$$re_hash{ifunless}/
Branch: 1 branch
BranchTrueFalseTotal
$content =~ s[$re_hash->{"ifunless"}][() = ($1, $2, $3); ($bool =~ /$re_hash->{"ifunless_test"}/ ? !$params{$var} : $params{$var}) ? $body : "";]seg and do { do { () } } 5012 2510 7522
714 13032 my($bool,$var,$body)=($1,$2,$3);
715 13032 ▶ ($bool =~ m,$$re_hash{ifunless_test}, ? !$params{$var} : $params{$var}) ? $body : ''
Branch: 2 branches
BranchTrueFalseTotal
($bool =~ /$re_hash->{'ifunless_test'}/ ? !$params{$var} : $params{$var}) ? : 10024 3008 13032
$bool =~ /$re_hash->{'ifunless_test'}/ ? : 9020 4012 13032
716 /gse);
717 }
718 }
719  
720 3010 my $i = 0;
721 3010 foreach my $re (@{$re_hash->{vars}})
722 {
723 9030 ▶ next if ($skip->{"vars".++$i});
Branch: 1 branch
BranchTrueFalseTotal
if $$skip{'vars' . ++$i} 1500 7530 9030
724 7530 $content =~ s/$re/$params{$1}/g;
725 }
726 }
727  
728 3010 return $content;
729 }
730  
731 #==============================
732  
733 =item print
734  
735 =item print FILEHANDLE
736  
737 Sends the replaced content to the currently selected output (usually
738 STDOUT) or the supplied filehandle.
739  
740 =cut
741  
742 sub print
743 1 ▶{
Subroutine print: called CC=3 SCAR=11.4
Pod documented
744 1 my $self = shift;
745 1 my $filehandle = shift;
746  
747 1 my $content = $self->toString();
748 1 ▶ return undef if (!defined $content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 1 1
749  
750 1 ▶ if ($filehandle)
Branch: 1 branch
BranchTrueFalseTotal
if ($filehandle) { } 1 0 1
751 {
752 1 print $filehandle $content;
753 }
754 else
755 {
756 0 print $content;
757 }
758 1 return $self;
759 }
760 #==============================
761  
762 2 1;
763 __END__
↑ Summary