lib/Template.pm

filter: Statementstmt 94.7% 91 / 96 Branchbran 78.2% 36 / 46 Conditioncond 69.7% 23 / 33 MC/DCmcdc 47.3% 9 / 19 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 83.7% 180 / 215 CC 35 SCAR 23.4
CC
35
Cov
86%
_output
17.8
_init
11.4
process
11.3
CRAP 10.4
« lib/Template/Plugin/HTML.pm ↑ Summary blib/lib/Template/Base.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 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 113 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 113 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 113 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 113 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 113 ▶use Template::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 113 ▶use Template::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 113 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 113 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 113 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 113 our $VERSION = '3.103';
36 113 our $ERROR = '';
37 113 our $DEBUG = 0;
38 113 ▶our $BINMODE = 0 unless defined $BINMODE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::BINMODE 113 0 113
39 113 our $AUTOLOAD;
40  
41 # preload all modules if we're running under mod_perl
42 113 ▶Template::Config->preload() if $ENV{ MOD_PERL };
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOD_PERL"} 0 113 113
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 sub process {
54 1346 ▶ my ($self, $template, $vars, $outstream, @opts) = @_;
Subroutine process: called CC=11 SCAR=24.2
Pod documented
55 1346 my ($output, $error);
56 1346 ▶ my $options = (@opts == 1) && ref($opts[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
@opts == 1 && ref $opts[0] eq 'HASH' ? : 1 1345 1346
Condition66%2/3
@opts == 1 and ref $opts[0] eq "HASH"
!ll&&!rl&&r
134501
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 $options->{ binmode } = $BINMODE
60 1346 ▶ unless defined $options->{ binmode };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"binmode"} 1344 2 1346
61  
62 # we're using this for testing in t/output.t and t/filter.t so
63 # don't remove it if you don't want tests to fail...
64 1346 ▶ $self->DEBUG("set binmode\n") if $DEBUG && $options->{ binmode };
Branch: 1 branch
BranchTrueFalseTotal
if $Template::DEBUG and $options->{"binmode"} 2 1344 1346
Condition66%2/3
$Template::DEBUG and $options->{"binmode"}
!ll&&!rl&&r
134402
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
65  
66 1346 $output = $self->{ SERVICE }->process($template, $vars);
67  
68 1346 ▶ if (defined $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 1336 10 1346
69 1336 ▶ $outstream ||= $self->{ OUTPUT };
Condition66%2/3
$outstream ||= $self->{"OUTPUT"}
l!l&&r!l&&!r
133330
MC/DC0%0/2
$outstream ||= $self->{"OUTPUT"}
$outstream $self->{"OUTPUT"}
Truth table66%2/3
A ||= B
A= $outstreamB= $self->{"OUTPUT"}
ABresult
1X1
011
000
70 1336 ▶ unless (ref $outstream) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $outstream) 4 1332 1336
71 4 my $outpath = $self->{ OUTPUT_PATH };
72 4 ▶ $outstream = "$outpath/$outstream" if $outpath;
Branch: 1 branch
BranchTrueFalseTotal
if $outpath 4 0 4
73 }
74  
75 # send processed template to output stream, checking for error
76 1336 ▶ return ($self->error($error))
Branch: 1 branch
BranchTrueFalseTotal
if $error = &_output($outstream, \$output, $options) 0 1336 1336
77 if ($error = &_output($outstream, \$output, $options));
78  
79 1336 return 1;
80 }
81 else {
82 10 return $self->error($self->{ SERVICE }->error);
83 }
84 }
85  
86  
87 #------------------------------------------------------------------------
88 # service()
89 #
90 # Returns a reference to the internal SERVICE object which handles
91 # all requests for this Template object
92 #------------------------------------------------------------------------
93  
94 sub service {
95 3 ▶ my $self = shift;
Subroutine service: called CC=1 SCAR=0.0
Pod documented
96 3 return $self->{ SERVICE };
97 }
98  
99  
100 #------------------------------------------------------------------------
101 # context()
102 #
103 # Returns a reference to the CONTEXT object within the SERVICE
104 # object.
105 #------------------------------------------------------------------------
106  
107 sub context {
108 12 ▶ my $self = shift;
Subroutine context: called CC=1 SCAR=0.0
Pod documented
109 12 return $self->{ SERVICE }->{ CONTEXT };
110 }
111  
112 sub template {
113 2 ▶ shift->context->template(@_);
Subroutine template: called CC=1 SCAR=0.0
Pod documented
114 }
115  
116  
117 #========================================================================
118 # -- PRIVATE METHODS --
119 #========================================================================
120  
121 #------------------------------------------------------------------------
122 # _init(\%config)
123 #------------------------------------------------------------------------
124 sub _init {
125 233 ▶ my ($self, $config) = @_;
Subroutine _init: called CC=11 SCAR=24.3
126  
127 # convert any textual DEBUG args to numerical form
128 233 my $debug = $config->{ DEBUG };
129 233 ▶ $config->{ DEBUG } = Template::Constants::debug_flags($self, $debug)
Branch: 1 branch
BranchTrueFalseTotal
if defined $debug and not $debug =~ /^\d+$/ 1 232 233
Condition80%4/5
defined $debug and not $debug =~ /^\d+$/
!ll&&!rl&&r
213191
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
130 || return if defined $debug && $debug !~ /^\d+$/;
131  
132 # prepare a namespace handler for any CONSTANTS definition
133 233 ▶ if (my $constants = $config->{ CONSTANTS }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $constants = $config->{"CONSTANTS"}) 9 224 233
134 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
135 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
136 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
137 || return $self->error(Template::Config->error);
138 9 $ns->{ $cns } = $constants;
139 }
140  
141 $self->{ SERVICE } = $config->{ SERVICE }
142 233 ▶ || Template::Config->service($config)
Condition40%2/5
$config->{'SERVICE'} || 'Template::Config'->service($config) || (return $self->error('Template::Config'->error))
l!l
2330
$config->{"SERVICE"} or "Template::Config"->service($config)
l!l&&r!l&&!r
02330
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
143 || return $self->error(Template::Config->error);
144  
145 233 ▶ $self->{ OUTPUT } = $config->{ OUTPUT } || \*STDOUT;
Condition100%2/2
$config->{'OUTPUT'} || \*STDOUT
l!l
3230
MC/DC100%1/1
$config->{'OUTPUT'} || \*STDOUT
$config->{'OUTPUT'}
Truth table100%2/2
A
A= $config->{'OUTPUT'}
Aresult
11
00
146 233 $self->{ OUTPUT_PATH } = $config->{ OUTPUT_PATH };
147  
148 233 return $self;
149 }
150  
151  
152 #------------------------------------------------------------------------
153 # _output($where, $text)
154 #------------------------------------------------------------------------
155  
156 sub _output {
157 1337 ▶ my ($where, $textref, $options) = @_;
Subroutine _output: called CC=15 SCAR=28.8
158 1337 my $reftype;
159 1337 my $error = 0;
160  
161 # call a CODE reference
162 1337 ▶ if (($reftype = ref($where)) eq 'CODE') {
Branch: 6 branches
BranchTrueFalseTotal
if (($reftype = ref $where) eq 'CODE') { } 1 1336 1337
elsif ($reftype eq 'GLOB') { } 0 1336 1336
elsif ($reftype eq 'SCALAR') { } 1329 7 1336
elsif ($reftype eq 'ARRAY') { } 1 6 7
elsif (builtin::blessed($where) and $where->can('print')) { } 1 5 6
elsif (not $reftype) { } 5 0 5
163 1 &$where($$textref);
164 }
165 # print to a glob (such as \*STDOUT)
166 elsif ($reftype eq 'GLOB') {
167 0 print $where $$textref;
168 }
169 # append output to a SCALAR ref
170 elsif ($reftype eq 'SCALAR') {
171 1329 $$where .= $$textref;
172 }
173 # push onto ARRAY ref
174 elsif ($reftype eq 'ARRAY') {
175 1 push @$where, $$textref;
176 }
177 # call the print() method on an object that implements the method
178 # (e.g. IO::Handle, Apache::Request, etc)
179 ▶ 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
180 1 $where->print($$textref);
181 }
182 # a simple string is taken as a filename
183 elsif (! $reftype) {
184 5 local *FP;
185 # make destination directory if it doesn't exist
186 5 my $dir = dirname($where);
187 5 ▶ eval { mkpath($dir) unless -d $dir; };
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 5 5
188 5 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 5 5
elsif (open FP, '>', $where) { } 5 0 5
189 # strip file name and line number from error raised by die()
190 0 ($error = $@) =~ s/ at \S+ line \d+\n?$//;
191 }
192 elsif (open(FP, '>', $where)) {
193 # binmode option can be 1 or a specific layer, e.g. :utf8
194 5 my $bm = $options->{ binmode };
195 5 ▶ if (not(defined $bm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bm) 1 4 5
196 1 $bm = $BINMODE;
197 }
198 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
199 2 binmode FP;
200 }
201 elsif ($bm){
202 0 binmode FP, $bm;
203 }
204 5 print FP $$textref;
205 5 close FP;
206 }
207 else {
208 0 $error = "$where: $!";
209 }
210 }
211 # give up, we've done our best
212 else {
213 0 $error = "output_handler() cannot determine target type ($where)\n";
214 }
215  
216 1337 return $error;
217 }
218  
219  
220 113 1;
221  
222 __END__
« lib/Template/Plugin/HTML.pm ↑ Summary blib/lib/Template/Base.pm »