lib/Template/Config.pm

filter: Statementstmt 100% 85 / 85 Branchbran 76.0% 35 / 46 Conditioncond 50.9% 27 / 53 MC/DCmcdc 25.7% 9 / 35 Subroutinesub 100% 17 / 17 Podpod 100% 12 / 12 totaltotal 74.6% 185 / 248 CC 42 SCAR 15.7
CC
42
Cov
80%
constants
7.0
parser
5.6
provider
5.6
CRAP 4.8
« lib/Template/Plugin/Directory.pm ↑ Summary lib/Template/Base.pm »
1 #============================================================= -*-perl-*-
2 #
3 # Template::Config
4 #
5 # DESCRIPTION
6 # Template Toolkit configuration module.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 #========================================================================
18  
19 package Template::Config;
20  
21 114 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 114 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 114 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 114 our $VERSION = '3.100';
26  
27 114 our $DEBUG;
28 114 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Config::DEBUG 114 0 114
29 114 our $ERROR = '';
30 114 our $CONTEXT = 'Template::Context';
31 114 our $FILTERS = 'Template::Filters';
32 114 our $ITERATOR = 'Template::Iterator';
33 114 our $PARSER = 'Template::Parser';
34 114 our $PLUGINS = 'Template::Plugins';
35 114 our $PROVIDER = 'Template::Provider';
36 114 our $SERVICE = 'Template::Service';
37 114 our $STASH;
38 114 $STASH = 'Template::Stash::XS';
39 114 our $CONSTANTS = 'Template::Namespace::Constants';
40  
41 114 our $LATEX_PATH;
42 114 our $PDFLATEX_PATH;
43 114 our $DVIPS_PATH;
44  
45 114 our @PRELOAD = (
46 $CONTEXT, $FILTERS, $ITERATOR, $PARSER,
47 $PLUGINS, $PROVIDER, $SERVICE, $STASH
48 );
49  
50 # the following is set at installation time by the Makefile.PL
51 114 our $INSTDIR = '';
52  
53  
54 #========================================================================
55 # --- CLASS METHODS ---
56 #========================================================================
57  
58 #------------------------------------------------------------------------
59 # preload($module, $module, ...)
60 #
61 # Preloads all the standard TT modules that are likely to be used, along
62 # with any other passed as arguments.
63 #------------------------------------------------------------------------
64  
65 sub preload {
66 3 ▶ my $class = shift;
Subroutine preload: called CC=3 SCAR=11.0
Pod documented
67  
68 3 foreach my $module (@PRELOAD, @_) {
69 26 ▶ $class->load($module) || return;
Branch: 1 branch
BranchTrueFalseTotal
$class->load($module) or return 1 25 26
70 };
71 2 return 1;
72 }
73  
74  
75 #------------------------------------------------------------------------
76 # load($module)
77 #
78 # Load a module via require(). Any occurrences of '::' in the module name
79 # are be converted to '/' and '.pm' is appended. Returns 1 on success
80 # or undef on error. Use $class->error() to examine the error string.
81 #------------------------------------------------------------------------
82  
83 sub load {
84 1980 ▶ my ($class, $module) = @_;
Subroutine load: called CC=3 SCAR=11.0
Pod documented
85 1980 $module =~ s[::][/]g;
86 1980 $module .= '.pm';
87 1980 ▶ return 1 if $INC{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$module} 1472 508 1980
88 508 eval { require $module; };
89 508 ▶ return $@ ? $class->error("failed to load $module: $@") : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 506 508
90 }
91  
92  
93 #------------------------------------------------------------------------
94 # parser(\%params)
95 #
96 # Instantiate a new parser object of the class whose name is denoted by
97 # the package variable $PARSER (default: Template::Parser). Returns
98 # a reference to a newly instantiated parser object or undef on error.
99 # The class error() method can be called without arguments to examine
100 # the error message generated by this failure.
101 #------------------------------------------------------------------------
102  
103 sub parser {
104 212 ▶ my $class = shift;
Subroutine parser: called CC=5 SCAR=17.2
Pod documented
105 212 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 210 2 212
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
02210
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
106 ? shift
107 : { @_ };
108  
109 212 ▶ return undef unless $class->load($PARSER);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PARSER) 0 212 212
110 212 ▶ return $PARSER->new($params)
Condition33%1/3
$Template::Config::PARSER->new($params) || $class->error('failed to create parser: ', $Template::Config::PARSER->error)
l!l&&r!l&&!r
21200
MC/DC0%0/2
$Template::Config::PARSER->new($params) || $class->error('failed to create parser: ', $Template::Config::PARSER->error)
$Template::Config::PARSER->new($params) $class->error('failed to create parser: ', $Template::Config::PARSER->error)
Truth table33%1/3
A || B
A= $Template::Config::PARSER->new($params)B= $class->error('failed to create parser: ', $Template::Config::PARSER->error)
ABresult
1X1
011
000
111 || $class->error("failed to create parser: ", $PARSER->error);
112 }
113  
114  
115 #------------------------------------------------------------------------
116 # provider(\%params)
117 #
118 # Instantiate a new template provider object (default: Template::Provider).
119 # Returns an object reference or undef on error, as above.
120 #------------------------------------------------------------------------
121  
122 sub provider {
123 249 ▶ my $class = shift;
Subroutine provider: called CC=5 SCAR=17.2
Pod documented
124 249 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 245 4 249
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
04245
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
125 ? shift
126 : { @_ };
127  
128 249 ▶ return undef unless $class->load($PROVIDER);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PROVIDER) 0 249 249
129 249 ▶ return $PROVIDER->new($params)
Condition33%1/3
$Template::Config::PROVIDER->new($params) || $class->error('failed to create template provider: ', $Template::Config::PROVIDER->error)
l!l&&r!l&&!r
24900
MC/DC0%0/2
$Template::Config::PROVIDER->new($params) || $class->error('failed to create template provider: ', $Template::Config::PROVIDER->error)
$Template::Config::PROVIDER->new($params) $class->error('failed to create template provider: ', $Template::Config::PROVIDER->error)
Truth table33%1/3
A || B
A= $Template::Config::PROVIDER->new($params)B= $class->error('failed to create template provider: ', $Template::Config::PROVIDER->error)
ABresult
1X1
011
000
130 || $class->error(
131 "failed to create template provider: ",
132 $PROVIDER->error
133 );
134 }
135  
136  
137 #------------------------------------------------------------------------
138 # plugins(\%params)
139 #
140 # Instantiate a new plugins provider object (default: Template::Plugins).
141 # Returns an object reference or undef on error, as above.
142 #------------------------------------------------------------------------
143  
144 sub plugins {
145 249 ▶ my $class = shift;
Subroutine plugins: called CC=5 SCAR=17.2
Pod documented
146 249 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 248 1 249
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01248
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
147 ? shift
148 : { @_ };
149  
150 249 ▶ return undef unless $class->load($PLUGINS);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PLUGINS) 0 249 249
151 249 ▶ return $PLUGINS->new($params)
Condition33%1/3
$Template::Config::PLUGINS->new($params) || $class->error('failed to create plugin provider: ', $Template::Config::PLUGINS->error)
l!l&&r!l&&!r
24900
MC/DC0%0/2
$Template::Config::PLUGINS->new($params) || $class->error('failed to create plugin provider: ', $Template::Config::PLUGINS->error)
$Template::Config::PLUGINS->new($params) $class->error('failed to create plugin provider: ', $Template::Config::PLUGINS->error)
Truth table33%1/3
A || B
A= $Template::Config::PLUGINS->new($params)B= $class->error('failed to create plugin provider: ', $Template::Config::PLUGINS->error)
ABresult
1X1
011
000
152 || $class->error(
153 "failed to create plugin provider: ",
154 $PLUGINS->error
155 );
156 }
157  
158  
159 #------------------------------------------------------------------------
160 # filters(\%params)
161 #
162 # Instantiate a new filters provider object (default: Template::Filters).
163 # Returns an object reference or undef on error, as above.
164 #------------------------------------------------------------------------
165  
166 sub filters {
167 249 ▶ my $class = shift;
Subroutine filters: called CC=5 SCAR=17.2
Pod documented
168 249 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 248 1 249
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01248
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
169 ? shift
170 : { @_ };
171  
172 249 ▶ return undef unless $class->load($FILTERS);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::FILTERS) 0 249 249
173 249 ▶ return $FILTERS->new($params)
Condition33%1/3
$Template::Config::FILTERS->new($params) || $class->error('failed to create filter provider: ', $Template::Config::FILTERS->error)
l!l&&r!l&&!r
24900
MC/DC0%0/2
$Template::Config::FILTERS->new($params) || $class->error('failed to create filter provider: ', $Template::Config::FILTERS->error)
$Template::Config::FILTERS->new($params) $class->error('failed to create filter provider: ', $Template::Config::FILTERS->error)
Truth table33%1/3
A || B
A= $Template::Config::FILTERS->new($params)B= $class->error('failed to create filter provider: ', $Template::Config::FILTERS->error)
ABresult
1X1
011
000
174 || $class->error(
175 "failed to create filter provider: ",
176 $FILTERS->error
177 );
178 }
179  
180  
181 #------------------------------------------------------------------------
182 # iterator(\@list)
183 #
184 # Instantiate a new Template::Iterator object (default: Template::Iterator).
185 # Returns an object reference or undef on error, as above.
186 #------------------------------------------------------------------------
187  
188 sub iterator {
189 244 ▶ my $class = shift;
Subroutine iterator: called CC=3 SCAR=12.0
Pod documented
190 244 my $list = shift;
191  
192 244 ▶ return undef unless $class->load($ITERATOR);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::ITERATOR) 0 244 244
193 244 ▶ return $ITERATOR->new($list, @_)
Condition33%1/3
$Template::Config::ITERATOR->new($list, @_) || $class->error('failed to create iterator: ', $Template::Config::ITERATOR->error)
l!l&&r!l&&!r
24400
MC/DC0%0/2
$Template::Config::ITERATOR->new($list, @_) || $class->error('failed to create iterator: ', $Template::Config::ITERATOR->error)
$Template::Config::ITERATOR->new($list, @_) $class->error('failed to create iterator: ', $Template::Config::ITERATOR->error)
Truth table33%1/3
A || B
A= $Template::Config::ITERATOR->new($list, @_)B= $class->error('failed to create iterator: ', $Template::Config::ITERATOR->error)
ABresult
1X1
011
000
194 || $class->error("failed to create iterator: ", $ITERATOR->error);
195 }
196  
197  
198 #------------------------------------------------------------------------
199 # stash(\%vars)
200 #
201 # Instantiate a new template variable stash object (default:
202 # Template::Stash). Returns object or undef, as above.
203 #------------------------------------------------------------------------
204  
205 sub stash {
206 255 ▶ my $class = shift;
Subroutine stash: called CC=5 SCAR=17.2
Pod documented
207 255 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 254 1 255
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01254
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
208 ? shift
209 : { @_ };
210  
211 255 ▶ return undef unless $class->load($STASH);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::STASH) 0 255 255
212 255 ▶ return $STASH->new($params)
Condition33%1/3
$Template::Config::STASH->new($params) || $class->error('failed to create stash: ', $Template::Config::STASH->error)
l!l&&r!l&&!r
25500
MC/DC0%0/2
$Template::Config::STASH->new($params) || $class->error('failed to create stash: ', $Template::Config::STASH->error)
$Template::Config::STASH->new($params) $class->error('failed to create stash: ', $Template::Config::STASH->error)
Truth table33%1/3
A || B
A= $Template::Config::STASH->new($params)B= $class->error('failed to create stash: ', $Template::Config::STASH->error)
ABresult
1X1
011
000
213 || $class->error(
214 "failed to create stash: ",
215 $STASH->error
216 );
217 }
218  
219  
220 #------------------------------------------------------------------------
221 # context(\%params)
222 #
223 # Instantiate a new template context object (default: Template::Context).
224 # Returns object or undef, as above.
225 #------------------------------------------------------------------------
226  
227 sub context {
228 248 ▶ my $class = shift;
Subroutine context: called CC=5 SCAR=16.6
Pod documented
229 248 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 246 2 248
Condition100%3/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
11246
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
230 ? shift
231 : { @_ };
232  
233 248 ▶ return undef unless $class->load($CONTEXT);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::CONTEXT) 0 248 248
234 248 ▶ return $CONTEXT->new($params)
Condition33%1/3
$Template::Config::CONTEXT->new($params) || $class->error('failed to create context: ', $Template::Config::CONTEXT->error)
l!l&&r!l&&!r
24800
MC/DC0%0/2
$Template::Config::CONTEXT->new($params) || $class->error('failed to create context: ', $Template::Config::CONTEXT->error)
$Template::Config::CONTEXT->new($params) $class->error('failed to create context: ', $Template::Config::CONTEXT->error)
Truth table33%1/3
A || B
A= $Template::Config::CONTEXT->new($params)B= $class->error('failed to create context: ', $Template::Config::CONTEXT->error)
ABresult
1X1
011
000
235 || $class->error(
236 "failed to create context: ",
237 $CONTEXT->error
238 );
239 }
240  
241  
242 #------------------------------------------------------------------------
243 # service(\%params)
244 #
245 # Instantiate a new template context object (default: Template::Service).
246 # Returns object or undef, as above.
247 #------------------------------------------------------------------------
248  
249 sub service {
250 234 ▶ my $class = shift;
Subroutine service: called CC=5 SCAR=17.2
Pod documented
251 234 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 233 1 234
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01233
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
252 ? shift
253 : { @_ };
254  
255 234 ▶ return undef unless $class->load($SERVICE);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::SERVICE) 0 234 234
256 234 ▶ return $SERVICE->new($params)
Condition33%1/3
$Template::Config::SERVICE->new($params) || $class->error('failed to create context: ', $Template::Config::SERVICE->error)
l!l&&r!l&&!r
23400
MC/DC0%0/2
$Template::Config::SERVICE->new($params) || $class->error('failed to create context: ', $Template::Config::SERVICE->error)
$Template::Config::SERVICE->new($params) $class->error('failed to create context: ', $Template::Config::SERVICE->error)
Truth table33%1/3
A || B
A= $Template::Config::SERVICE->new($params)B= $class->error('failed to create context: ', $Template::Config::SERVICE->error)
ABresult
1X1
011
000
257 || $class->error(
258 "failed to create context: ",
259 $SERVICE->error
260 );
261 }
262  
263  
264 #------------------------------------------------------------------------
265 # constants(\%params)
266 #
267 # Instantiate a new namespace handler for compile time constant folding
268 # (default: Template::Namespace::Constants).
269 # Returns object or undef, as above.
270 #------------------------------------------------------------------------
271  
272 sub constants {
273 10 ▶ my $class = shift;
Subroutine constants: called CC=5 SCAR=19.4
Pod documented
274 10 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 10 0 10
Condition33%1/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
275 ? shift
276 : { @_ };
277  
278 10 ▶ return undef unless $class->load($CONSTANTS);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::CONSTANTS) 0 10 10
279 10 ▶ return $CONSTANTS->new($params)
Condition33%1/3
$Template::Config::CONSTANTS->new($params) || $class->error('failed to create constants namespace: ', $Template::Config::CONSTANTS->error)
l!l&&r!l&&!r
1000
MC/DC0%0/2
$Template::Config::CONSTANTS->new($params) || $class->error('failed to create constants namespace: ', $Template::Config::CONSTANTS->error)
$Template::Config::CONSTANTS->new($params) $class->error('failed to create constants namespace: ', $Template::Config::CONSTANTS->error)
Truth table33%1/3
A || B
A= $Template::Config::CONSTANTS->new($params)B= $class->error('failed to create constants namespace: ', $Template::Config::CONSTANTS->error)
ABresult
1X1
011
000
280 || $class->error(
281 "failed to create constants namespace: ",
282 $CONSTANTS->error
283 );
284 }
285  
286  
287 #------------------------------------------------------------------------
288 # instdir($dir)
289 #
290 # Returns the root installation directory appended with any local
291 # component directory passed as an argument.
292 #------------------------------------------------------------------------
293  
294 sub instdir {
295 6 ▶ my ($class, $dir) = @_;
Subroutine instdir: called CC=4 SCAR=13.9
Pod documented
296 6 ▶ my $inst = $INSTDIR
Condition100%2/2
$Template::Config::INSTDIR || (return $class->error('no installation directory'))
l!l
33
MC/DC100%1/1
$Template::Config::INSTDIR || (return $class->error('no installation directory'))
$Template::Config::INSTDIR
Truth table100%2/2
A
A= $Template::Config::INSTDIR
Aresult
11
00
297 || return $class->error("no installation directory");
298 3 ▶ chop $inst while substr($inst,-1) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
substr($inst, -1) eq "/" and do { chop $inst } 1 3 4
299 3 ▶ $inst .= "/$dir" if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 2 1 3
300 3 return $inst;
301 }
302  
303  
304 #========================================================================
305 # This should probably be moved somewhere else in the long term, but for
306 # now it ensures that Template::TieString is available even if the
307 # Template::Directive module hasn't been loaded, as is the case when
308 # using compiled templates and Template::Parser hasn't yet been loaded
309 # on demand.
310 #========================================================================
311  
312 #------------------------------------------------------------------------
313 # simple package for tying $output variable to STDOUT, used by perl()
314 #------------------------------------------------------------------------
315  
316 package Template::TieString;
317  
318 sub TIEHANDLE {
319 13 ▶ my ($class, $textref) = @_;
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
320 13 bless $textref, $class;
321 }
322 sub PRINT {
323 7 ▶ my $self = shift;
Subroutine PRINT: called CC=1 SCAR=0.0
324 7 $$self .= join('', @_);
325 }
326  
327  
328  
329 114 1;
330  
331 __END__
« lib/Template/Plugin/Directory.pm ↑ Summary lib/Template/Base.pm »