lib/Template.pm

filter: Statementstmt 94.7% 91 / 96 Branchbran 77.2% 34 / 44 Conditioncond 69.4% 25 / 36 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 82.5% 180 / 218 CC 35 SCAR 23.5
CC
35
Cov
85%
_output
17.8
process
11.5
_init
11.4
CRAP 10.4
« lib/Template/Plugin/Datafile.pm ↑ Summary lib/Template/Plugin/HTML.pm »
1 #============================================================= -*-perl-*-
2 #
3 # Template
4 #
5 # DESCRIPTION
6 # Module implementing a simple, user-oriented front-end to the Template
7 # Toolkit.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #========================================================================
19  
20 package Template;
21  
22 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 116 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 116 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 116 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 116 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 116 ▶use Template::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 116 ▶use Template::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 116 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 116 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 116 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 116 our $VERSION = '3.106';
36 116 our $ERROR = '';
37 116 our $DEBUG = 0;
38 116 ▶our $BINMODE = 0 unless defined $BINMODE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::BINMODE 116 0 116
39 116 our $AUTOLOAD;
40  
41 # preload all modules if we're running under mod_perl
42 116 ▶Template::Config->preload() if $ENV{ MOD_PERL };
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOD_PERL"} 0 116 116
43  
44  
45 #------------------------------------------------------------------------
46 # process($input, \%replace, $output)
47 #
48 # Main entry point for the Template Toolkit. The Template module
49 # delegates most of the processing effort to the underlying SERVICE
50 # object, an instance of the Template::Service class.
51 #------------------------------------------------------------------------
52  
53 1406 ▶sub process {
Subroutine process: called CC=11 SCAR=24.4
Pod documented
54 1406 my ($self, $template, $vars, $outstream, @opts) = @_;
55 1406 my ($output, $error);
56 1406 ▶ my $options = (@opts == 1) && ref($opts[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
@opts == 1 && ref $opts[0] eq 'HASH' ? : 1 1405 1406
Condition66%2/3
@opts == 1 and ref $opts[0] eq "HASH"
!ll&&!rl&&r
140501
MC/DC50%1/2
@opts == 1 and ref $opts[0] eq "HASH"
@opts == 1 ref $opts[0] eq "HASH"
Truth table66%2/3
A and B
A= @opts == 1B= ref $opts[0] eq "HASH"
ABresult
0X0
100
111
57 ? shift(@opts) : { @opts };
58  
59 1406 ▶ $options->{ binmode } //= $BINMODE;
Condition66%2/3
$options->{"binmode"} //= $Template::BINMODE
l!l&&r!l&&!r
214040
MC/DC0%0/2
$options->{"binmode"} //= $Template::BINMODE
$options->{"binmode"} $Template::BINMODE
Truth table66%2/3
A //= B
A= $options->{"binmode"}B= $Template::BINMODE
ABresult
1X1
011
000
60  
61 # we're using this for testing in t/output.t and t/filter.t so
62 # don't remove it if you don't want tests to fail...
63 1406 ▶ $self->DEBUG("set binmode\n") if $DEBUG && $options->{ binmode };
Branch: 1 branch
BranchTrueFalseTotal
if $Template::DEBUG and $options->{"binmode"} 2 1404 1406
Condition66%2/3
$Template::DEBUG and $options->{"binmode"}
!ll&&!rl&&r
140402
MC/DC50%1/2
$Template::DEBUG and $options->{"binmode"}
$Template::DEBUG $options->{"binmode"}
Truth table66%2/3
A and B
A= $Template::DEBUGB= $options->{"binmode"}
ABresult
0X0
100
111
64  
65 1406 $output = $self->{ SERVICE }->process($template, $vars);
66  
67 1406 ▶ if (defined $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 1394 12 1406
68 1394 ▶ $outstream ||= $self->{ OUTPUT };
Condition66%2/3
$outstream ||= $self->{"OUTPUT"}
l!l&&r!l&&!r
139130
MC/DC0%0/2
$outstream ||= $self->{"OUTPUT"}
$outstream $self->{"OUTPUT"}
Truth table66%2/3
A ||= B
A= $outstreamB= $self->{"OUTPUT"}
ABresult
1X1
011
000
69 1394 ▶ unless (ref $outstream) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $outstream) 4 1390 1394
70 4 my $outpath = $self->{ OUTPUT_PATH };
71 4 ▶ $outstream = "$outpath/$outstream" if $outpath;
Branch: 1 branch
BranchTrueFalseTotal
if $outpath 4 0 4
72 }
73  
74 # send processed template to output stream, checking for error
75 1394 ▶ return ($self->error($error))
Branch: 1 branch
BranchTrueFalseTotal
if $error = &_output($outstream, \$output, $options) 0 1394 1394
76 if ($error = &_output($outstream, \$output, $options));
77  
78 1394 return 1;
79 }
80 else {
81 12 return $self->error($self->{ SERVICE }->error);
82 }
83 }
84  
85  
86 #------------------------------------------------------------------------
87 # service()
88 #
89 # Returns a reference to the internal SERVICE object which handles
90 # all requests for this Template object
91 #------------------------------------------------------------------------
92  
93 3 ▶sub service {
Subroutine service: called CC=1 SCAR=0.0
Pod documented
94 3 my $self = shift;
95 3 return $self->{ SERVICE };
96 }
97  
98  
99 #------------------------------------------------------------------------
100 # context()
101 #
102 # Returns a reference to the CONTEXT object within the SERVICE
103 # object.
104 #------------------------------------------------------------------------
105  
106 12 ▶sub context {
Subroutine context: called CC=1 SCAR=0.0
Pod documented
107 12 my $self = shift;
108 12 return $self->{ SERVICE }->{ CONTEXT };
109 }
110  
111 2 ▶sub template {
Subroutine template: called CC=1 SCAR=0.0
Pod documented
112 2 shift->context->template(@_);
113 }
114  
115  
116 #========================================================================
117 # -- PRIVATE METHODS --
118 #========================================================================
119  
120 #------------------------------------------------------------------------
121 # _init(\%config)
122 #------------------------------------------------------------------------
123 252 ▶sub _init {
Subroutine _init: called CC=11 SCAR=24.3
124 252 my ($self, $config) = @_;
125  
126 # convert any textual DEBUG args to numerical form
127 252 my $debug = $config->{ DEBUG };
128 252 ▶ $config->{ DEBUG } = Template::Constants::debug_flags($self, $debug)
Branch: 1 branch
BranchTrueFalseTotal
if defined $debug and not $debug =~ /^\d+$/ 1 251 252
Condition80%4/5
defined $debug and not $debug =~ /^\d+$/
!ll&&!rl&&r
232191
Template::Constants::debug_flags($self, $debug) || (return)
l!l
10
MC/DC100%2/2
defined $debug and not $debug =~ /^\d+$/
defined $debug not $debug =~ /^\d+$/
MC/DC0%0/1
Template::Constants::debug_flags($self, $debug) || (return)
Template::Constants::debug_flags($self, $debug)
Truth table100%3/3
A and not(B)
A= defined $debugB= not $debug =~ /^\d+$/
ABresult
0X0
100
111
Truth table50%1/2
A
A= Template::Constants::debug_flags($self, $debug)
Aresult
11
00
129 || return if defined $debug && $debug !~ /^\d+$/;
130  
131 # prepare a namespace handler for any CONSTANTS definition
132 252 ▶ if (my $constants = $config->{ CONSTANTS }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $constants = $config->{"CONSTANTS"}) 9 243 252
133 9 ▶ my $ns = $config->{ NAMESPACE } ||= { };
Condition100%2/2
$config->{"NAMESPACE"} ||= {}
l!l
18
MC/DC100%1/1
$config->{"NAMESPACE"} ||= {}
$config->{"NAMESPACE"}
Truth table100%2/2
A
A= $config->{"NAMESPACE"}
Aresult
11
00
134 9 ▶ my $cns = $config->{ CONSTANTS_NAMESPACE } || 'constants';
Condition100%2/2
$config->{'CONSTANTS_NAMESPACE'} || 'constants'
l!l
18
MC/DC100%1/1
$config->{'CONSTANTS_NAMESPACE'} || 'constants'
$config->{'CONSTANTS_NAMESPACE'}
Truth table100%2/2
A
A= $config->{'CONSTANTS_NAMESPACE'}
Aresult
11
00
135 9 ▶ $constants = Template::Config->constants($constants)
Condition50%1/2
'Template::Config'->constants($constants) || (return $self->error('Template::Config'->error))
l!l
90
MC/DC0%0/1
'Template::Config'->constants($constants) || (return $self->error('Template::Config'->error))
'Template::Config'->constants($constants)
Truth table50%1/2
A
A= 'Template::Config'->constants($constants)
Aresult
11
00
136 || return $self->error(Template::Config->error);
137 9 $ns->{ $cns } = $constants;
138 }
139  
140 $self->{ SERVICE } = $config->{ SERVICE }
141 252 ▶ || Template::Config->service($config)
Condition40%2/5
$config->{'SERVICE'} || 'Template::Config'->service($config) || (return $self->error('Template::Config'->error))
l!l
2520
$config->{"SERVICE"} or "Template::Config"->service($config)
l!l&&r!l&&!r
02520
MC/DC0%0/2
$config->{'SERVICE'} || 'Template::Config'->service($config) || (return $self->error('Template::Config'->error))
$config->{"SERVICE"} "Template::Config"->service($config)
Truth table33%1/3
A or B
A= $config->{"SERVICE"}B= "Template::Config"->service($config)
ABresult
1X1
011
000
142 || return $self->error(Template::Config->error);
143  
144 252 ▶ $self->{ OUTPUT } = $config->{ OUTPUT } || \*STDOUT;
Condition100%2/2
$config->{'OUTPUT'} || \*STDOUT
l!l
3249
MC/DC100%1/1
$config->{'OUTPUT'} || \*STDOUT
$config->{'OUTPUT'}
Truth table100%2/2
A
A= $config->{'OUTPUT'}
Aresult
11
00
145 252 $self->{ OUTPUT_PATH } = $config->{ OUTPUT_PATH };
146  
147 252 return $self;
148 }
149  
150  
151 #------------------------------------------------------------------------
152 # _output($where, $text)
153 #------------------------------------------------------------------------
154  
155 1395 ▶sub _output {
Subroutine _output: called CC=15 SCAR=28.8
156 1395 my ($where, $textref, $options) = @_;
157 1395 my $reftype;
158 1395 my $error = 0;
159  
160 # call a CODE reference
161 1395 ▶ if (($reftype = ref($where)) eq 'CODE') {
Branch: 6 branches
BranchTrueFalseTotal
if (($reftype = ref $where) eq 'CODE') { } 1 1394 1395
elsif ($reftype eq 'GLOB') { } 0 1394 1394
elsif ($reftype eq 'SCALAR') { } 1387 7 1394
elsif ($reftype eq 'ARRAY') { } 1 6 7
elsif (builtin::blessed($where) and $where->can('print')) { } 1 5 6
elsif (not $reftype) { } 5 0 5
162 1 &$where($$textref);
163 }
164 # print to a glob (such as \*STDOUT)
165 elsif ($reftype eq 'GLOB') {
166 0 print $where $$textref;
167 }
168 # append output to a SCALAR ref
169 elsif ($reftype eq 'SCALAR') {
170 1387 $$where .= $$textref;
171 }
172 # push onto ARRAY ref
173 elsif ($reftype eq 'ARRAY') {
174 1 push @$where, $$textref;
175 }
176 # call the print() method on an object that implements the method
177 # (e.g. IO::Handle, Apache::Request, etc)
178 ▶ elsif (blessed($where) && $where->can('print')) {
Condition66%2/3
builtin::blessed($where) and $where->can("print")
!ll&&!rl&&r
501
MC/DC50%1/2
builtin::blessed($where) and $where->can("print")
builtin::blessed($where) $where->can("print")
Truth table66%2/3
A and B
A= builtin::blessed($where)B= $where->can("print")
ABresult
0X0
100
111
179 1 $where->print($$textref);
180 }
181 # a simple string is taken as a filename
182 elsif (! $reftype) {
183 5 local *FP;
184 # make destination directory if it doesn't exist
185 5 my $dir = dirname($where);
186 5 ▶ eval { mkpath($dir) unless -d $dir; };
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 5 5
187 5 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 5 5
elsif (open FP, '>', $where) { } 5 0 5
188 # strip file name and line number from error raised by die()
189 0 ($error = $@) =~ s/ at \S+ line \d+\n?$//;
190 }
191 elsif (open(FP, '>', $where)) {
192 # binmode option can be 1 or a specific layer, e.g. :utf8
193 5 my $bm = $options->{ binmode };
194 5 ▶ if (not(defined $bm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bm) 1 4 5
195 1 $bm = $BINMODE;
196 }
197 5 ▶ if ($bm && $bm eq 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bm and $bm eq 1) { } 2 3 5
elsif ($bm) { } 0 3 3
Condition66%2/3
$bm and $bm eq 1
!ll&&!rl&&r
302
MC/DC50%1/2
$bm and $bm eq 1
$bm $bm eq 1
Truth table66%2/3
A and B
A= $bmB= $bm eq 1
ABresult
0X0
100
111
198 2 binmode FP;
199 }
200 elsif ($bm){
201 0 binmode FP, $bm;
202 }
203 5 print FP $$textref;
204 5 close FP;
205 }
206 else {
207 0 $error = "$where: $!";
208 }
209 }
210 # give up, we've done our best
211 else {
212 0 $error = "output_handler() cannot determine target type ($where)\n";
213 }
214  
215 1395 return $error;
216 }
217  
218  
219 116 1;
220  
221 __END__
« lib/Template/Plugin/Datafile.pm ↑ Summary lib/Template/Plugin/HTML.pm »