lib/Template/Document.pm

filter: Statementstmt 96.8% 93 / 96 Branchbran 68.7% 22 / 32 Conditioncond 65.2% 15 / 23 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 85.1% 161 / 189 CC 25 SCAR 18.2
CC
25
Cov
86%
new
9.6
write_perl_file
9.3
BEGIN
8.9
CRAP 6.1
« lib/Template/Plugin/File.pm ↑ Summary lib/Template/Filters.pm »
1 ##============================================================= -*-Perl-*-
2 #
3 # Template::Document
4 #
5 # DESCRIPTION
6 # Module defining a class of objects which encapsulate compiled
7 # templates, storing additional block definitions and metadata
8 # as well as the compiled Perl sub-routine representing the main
9 # template content.
10 #
11 # AUTHOR
12 # Andy Wardley <abw@wardley.org>
13 #
14 # COPYRIGHT
15 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
16 #
17 # This module is free software; you can redistribute it and/or
18 # modify it under the same terms as Perl itself.
19 #
20 #============================================================================
21  
22 package Template::Document;
23  
24 117 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 117 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 117 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 117 our $VERSION = '3.106';
30 117 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Document::DEBUG 117 0 117
31 117 our $ERROR = '';
32 117 our ($COMPERR, $AUTOLOAD, $UNICODE);
33  
34 117 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=21.9
35 # UNICODE is supported in versions of Perl from 5.008 onwards
36 117 ▶ if ($UNICODE = $] > 5.007 ? 1 : 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Template::Document::UNICODE = $] > "5.007" ? 1 : 0) 117 0 117
$] > 5.007 ? : 117 0 117
37 117 ▶ if ($] > 5.008) {
Branch: 2 branches
BranchTrueFalseTotal
if ($] > 5.008) { } 117 0 117
elsif ($] == 5.008) { } 0 0 0
38 # utf8::is_utf8() available from Perl 5.8.1 onwards
39 117 *is_utf8 = \&utf8::is_utf8;
40 }
41 elsif ($] == 5.008) {
42 # use Encode::is_utf8() for Perl 5.8.0
43 0 require Encode;
44 0 *is_utf8 = \&Encode::is_utf8;
45 }
46 }
47 }
48  
49  
50 #========================================================================
51 # ----- PUBLIC METHODS -----
52 #========================================================================
53  
54 #------------------------------------------------------------------------
55 # new(\%document)
56 #
57 # Creates a new self-contained Template::Document object which
58 # encapsulates a compiled Perl sub-routine, $block, any additional
59 # BLOCKs defined within the document ($defblocks, also Perl sub-routines)
60 # and additional $metadata about the document.
61 #------------------------------------------------------------------------
62  
63 1504 ▶sub new {
Subroutine new: called CC=9 SCAR=22.7
Pod documented
64 1504 my ($class, $doc) = @_;
65 1504 my ($block, $defblocks, $variables, $metadata) = @$doc{ qw( BLOCK DEFBLOCKS VARIABLES METADATA ) };
66 1504 ▶ $defblocks ||= { };
Condition100%2/2
$defblocks ||= {}
l!l
15022
MC/DC100%1/1
$defblocks ||= {}
$defblocks
Truth table100%2/2
A
A= $defblocks
Aresult
11
00
67 1504 ▶ $metadata ||= { };
Condition50%1/2
$metadata ||= {}
l!l
15040
MC/DC0%0/1
$metadata ||= {}
$metadata
Truth table50%1/2
A
A= $metadata
Aresult
11
00
68  
69 # evaluate Perl code in $block to create sub-routine reference if necessary
70 1504 ▶ unless (ref $block) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $block) 1485 19 1504
71 1485 local $SIG{__WARN__} = \&catch_warnings;
72 1485 $COMPERR = '';
73  
74 # DON'T LOOK NOW! - blindly untainting can make you go blind!
75 {
76 117 ▶ no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 0 ▶ $block = each %{ { $block => undef } } if ${^TAINT}; #untaint
Branch: 1 branch
BranchTrueFalseTotal
if ${^TAINT} 0 1485 1485
78 }
79  
80 1485 $block = eval $block;
81 1485 ▶ return $class->error($@)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block 0 1485 1485
82 unless defined $block;
83 }
84  
85 # same for any additional BLOCK definitions
86 @$defblocks{ keys %$defblocks } =
87 # MORE BLIND UNTAINTING - turn away if you're squeamish
88 map {
89 1504 ▶ ref($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 7 138 145
Condition40%2/5
/(.*)/s and eval $1 or return $class->error($@)
l!l
1380
/(.*)/s and eval $1
!ll&&!rl&&r
00138
MC/DC0%0/2
/(.*)/s and eval $1 or return $class->error($@)
/(.*)/s eval $1
Truth table33%1/3
A and B
A= /(.*)/sB= eval $1
ABresult
111
0X0
100
90 ? $_
91 : ( /(.*)/s && eval($1) or return $class->error($@) )
92 } values %$defblocks;
93  
94 1504 bless {
95 %$metadata,
96 _BLOCK => $block,
97 _DEFBLOCKS => $defblocks,
98 _VARIABLES => $variables,
99 _HOT => 0,
100 }, $class;
101 }
102  
103  
104 #------------------------------------------------------------------------
105 # block()
106 #
107 # Returns a reference to the internal sub-routine reference, _BLOCK,
108 # that constitutes the main document template.
109 #------------------------------------------------------------------------
110  
111 3 ▶sub block {
Subroutine block: called CC=1 SCAR=0.0
Pod documented
112 3 return $_[0]->{ _BLOCK };
113 }
114  
115  
116 #------------------------------------------------------------------------
117 # blocks()
118 #
119 # Returns a reference to a hash array containing any BLOCK definitions
120 # from the template. The hash keys are the BLOCK name and the values
121 # are references to Template::Document objects. Returns 0 (# an empty hash)
122 # if no blocks are defined.
123 #------------------------------------------------------------------------
124  
125 1491 ▶sub blocks {
Subroutine blocks: called CC=1 SCAR=0.0
Pod documented
126 1491 return $_[0]->{ _DEFBLOCKS };
127 }
128  
129  
130 #-----------------------------------------------------------------------
131 # variables()
132 #
133 # Returns a reference to a hash of variables used in the template.
134 # This requires the TRACE_VARS option to be enabled.
135 #-----------------------------------------------------------------------
136  
137 2 ▶sub variables {
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
138 2 return $_[0]->{ _VARIABLES };
139 }
140  
141 #------------------------------------------------------------------------
142 # process($context)
143 #
144 # Process the document in a particular context. Checks for recursion,
145 # registers the document with the context via visit(), processes itself,
146 # and then unwinds with a large gin and tonic.
147 #------------------------------------------------------------------------
148  
149 1551 ▶sub process {
Subroutine process: called CC=4 SCAR=13.9
Pod documented
150 1551 my ($self, $context) = @_;
151 1551 my $defblocks = $self->{ _DEFBLOCKS };
152 1551 my $output;
153  
154  
155 # check we're not already visiting this template
156 return $context->throw(
157 Template::Constants::ERROR_FILE,
158 "recursion into '$self->{ name }'"
159 1551 ▶ ) if $self->{ _HOT } && ! $context->{ RECURSION }; ## RETURN ##
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_HOT"} and not $context->{"RECURSION"} 1 1550 1551
Condition100%3/3
$self->{"_HOT"} and not $context->{"RECURSION"}
!ll&&!rl&&r
154821
MC/DC100%2/2
$self->{"_HOT"} and not $context->{"RECURSION"}
$self->{"_HOT"} not $context->{"RECURSION"}
Truth table100%3/3
A and not(B)
A= $self->{"_HOT"}B= not $context->{"RECURSION"}
ABresult
0X0
100
111
160  
161 1550 $context->visit($self, $defblocks);
162  
163 1550 $self->{ _HOT } = 1;
164 1550 eval {
165 1550 my $block = $self->{ _BLOCK };
166 1550 $output = &$block($context);
167 };
168 1550 my $e = $@; # Fragile, capture now
169 1550 $self->{ _HOT } = 0;
170  
171 1550 $context->leave();
172  
173 1550 ▶ die $context->catch($e)
Branch: 1 branch
BranchTrueFalseTotal
if $e 25 1525 1550
174 if $e;
175  
176 1525 return $output;
177 }
178  
179 #------------------------------------------------------------------------
180 # meta()
181 #
182 # Return the META items, i.e. anything that isn't prefixed with a _, e.g.
183 # _BLOCKS, or the name or modtime items.
184 #------------------------------------------------------------------------
185  
186 2 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod documented
187 2 my $self = shift;
188 return {
189 3 map { $_ => $self->{ $_ } }
190 2 grep { ! /^(_|modtime$|name$)/ }
191 keys %$self
192 };
193 }
194  
195  
196 #------------------------------------------------------------------------
197 # AUTOLOAD
198 #
199 # Provides pseudo-methods for read-only access to various internal
200 # members.
201 #------------------------------------------------------------------------
202  
203 37 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
204 37 my $self = shift;
205 37 my $method = $AUTOLOAD;
206  
207 37 $method =~ s/.*:://;
208 37 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 0 37 37
209 # my ($pkg, $file, $line) = caller();
210 # print STDERR "called $self->AUTOLOAD($method) from $file line $line\n";
211 37 return $self->{ $method };
212 }
213  
214 #========================================================================
215 # ----- CLASS METHODS -----
216 #========================================================================
217  
218 #------------------------------------------------------------------------
219 # as_perl($content)
220 #
221 # This method expects a reference to a hash passed as the first argument
222 # containing 3 items:
223 # METADATA # a hash of template metadata
224 # BLOCK # string containing Perl sub definition for main block
225 # DEFBLOCKS # hash containing further subs for addional BLOCK defs
226 # It returns a string containing Perl code which, when evaluated and
227 # executed, will instantiate a new Template::Document object with the
228 # above data. On error, it returns undef with an appropriate error
229 # message set in $ERROR.
230 #------------------------------------------------------------------------
231  
232 23 ▶sub as_perl {
Subroutine as_perl: called CC=1 SCAR=0.0
Pod documented
233 23 my ($class, $content) = @_;
234 23 my ($block, $defblocks, $metadata) = @$content{ qw( BLOCK DEFBLOCKS METADATA ) };
235  
236 23 $block =~ s/\s+$//;
237  
238 $defblocks = join('', map {
239 23 my $code = $defblocks->{ $_ };
240 8 $code =~ s/\s*$//;
241 8 " '$_' => $code,\n";
242 } keys %$defblocks);
243 23 $defblocks =~ s/\s+$//;
244  
245 $metadata = join(
246 '',
247 map {
248 23 my $x = $metadata->{ $_ };
249 50 $x =~ s/(['\\])/\\$1/g;
250 50 " '$_' => '$x',\n";
251 }
252 keys %$metadata
253 );
254 23 $metadata =~ s/\s+$//;
255  
256 return <<EOF
257 #------------------------------------------------------------------------
258 # Compiled template generated by the Template Toolkit version $Template::VERSION
259 #------------------------------------------------------------------------
260  
261 $class->new({
262 METADATA => {
263 $metadata
264 },
265 BLOCK => $block,
266 DEFBLOCKS => {
267 $defblocks
268 },
269 });
270 EOF
271 23 }
272  
273  
274 #------------------------------------------------------------------------
275 # write_perl_file($filename, \%content)
276 #
277 # This method calls as_perl() to generate the Perl code to represent a
278 # compiled template with the content passed as the second argument.
279 # It then writes this to the file denoted by the first argument.
280 #
281 # Returns 1 on success. On error, sets the $ERROR package variable
282 # to contain an error message and returns undef.
283 #------------------------------------------------------------------------
284  
285 22 ▶sub write_perl_file {
Subroutine write_perl_file: called CC=9 SCAR=22.3
Pod documented
286 22 my ($class, $file, $content) = @_;
287 22 my ($fh, $tmpfile);
288  
289 22 ▶ return $class->error("invalid filename: " . (defined $file ? $file : ''))
Branch: 2 branches
BranchTrueFalseTotal
unless defined $file and length $file 2 20 22
defined $file ? : 1 1 2
Condition100%3/3
defined $file and length $file
!ll&&!rl&&r
1120
MC/DC100%2/2
defined $file and length $file
defined $file length $file
Truth table100%3/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
290 unless defined $file && length $file;
291  
292 20 eval {
293 20 require File::Temp;
294 20 require File::Basename;
295 20 ($fh, $tmpfile) = File::Temp::tempfile(
296 DIR => File::Basename::dirname($file)
297 );
298 20 ▶ my $perlcode = $class->as_perl($content) || die $!;
Condition50%1/2
$class->as_perl($content) || die($!)
l!l
200
MC/DC0%0/1
$class->as_perl($content) || die($!)
$class->as_perl($content)
Truth table50%1/2
A
A= $class->as_perl($content)
Aresult
11
00
299  
300 20 ▶ if ($UNICODE && is_utf8($perlcode)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Template::Document::UNICODE and is_utf8($perlcode)) 5 15 20
Condition66%2/3
$Template::Document::UNICODE and is_utf8($perlcode)
!ll&&!rl&&r
0155
MC/DC50%1/2
$Template::Document::UNICODE and is_utf8($perlcode)
$Template::Document::UNICODE is_utf8($perlcode)
Truth table66%2/3
A and B
A= $Template::Document::UNICODEB= is_utf8($perlcode)
ABresult
0X0
100
111
301 5 $perlcode = "use utf8;\n\n$perlcode";
302 5 binmode $fh, ":utf8";
303 }
304 20 print $fh $perlcode;
305 20 close($fh);
306 };
307 20 ▶ return $class->error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
308 20 ▶ return rename($tmpfile, $file)
Condition33%1/3
rename($tmpfile, $file) || $class->error($!)
l!l&&r!l&&!r
2000
MC/DC0%0/2
rename($tmpfile, $file) || $class->error($!)
rename($tmpfile, $file) $class->error($!)
Truth table33%1/3
A || B
A= rename($tmpfile, $file)B= $class->error($!)
ABresult
1X1
011
000
309 || $class->error($!);
310 }
311  
312  
313 #------------------------------------------------------------------------
314 # catch_warnings($msg)
315 #
316 # Installed as
317 #------------------------------------------------------------------------
318  
319 1 ▶sub catch_warnings {
Subroutine catch_warnings: called CC=1 SCAR=0.0
Pod documented
320 1 $COMPERR .= join('', @_);
321 }
322  
323  
324 117 1;
325  
326 __END__
« lib/Template/Plugin/File.pm ↑ Summary lib/Template/Filters.pm »