lib/Template/Config.pm

filter: Statementstmt 100% 88 / 88 Branchbran 76.0% 35 / 46 Conditioncond 50.9% 27 / 53 MC/DCmcdc 25.7% 9 / 35 Subroutinesub 100% 18 / 18 Podpod 100% 12 / 12 totaltotal 75.0% 189 / 252 CC 42 SCAR 15.5
CC
42
Cov
80%
constants
7.0
parser
5.6
provider
5.6
CRAP 4.7
« lib/Template/Plugin/Directory.pm ↑ Summary lib/Template/Plugin/URL.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 117 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 117 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 117 our $VERSION = '3.106';
26  
27 117 our $DEBUG;
28 117 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Config::DEBUG 117 0 117
29 117 our $ERROR = '';
30 117 our $CONTEXT = 'Template::Context';
31 117 our $FILTERS = 'Template::Filters';
32 117 our $ITERATOR = 'Template::Iterator';
33 117 our $PARSER = 'Template::Parser';
34 117 our $PLUGINS = 'Template::Plugins';
35 117 our $PROVIDER = 'Template::Provider';
36 117 our $SERVICE = 'Template::Service';
37 117 our $STASH;
38 117 $STASH = 'Template::Stash::XS';
39 117 our $CONSTANTS = 'Template::Namespace::Constants';
40  
41 117 our $LATEX_PATH;
42 117 our $PDFLATEX_PATH;
43 117 our $DVIPS_PATH;
44  
45 117 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 117 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 3 ▶sub preload {
Subroutine preload: called CC=3 SCAR=11.0
Pod documented
66 3 my $class = shift;
67  
68 3 foreach my $module (@PRELOAD, @_) {
69 26 ▶ $class->load($module) || return;
Branch: 1 branch
BranchTrueFalseTotal
$class->load($module) or return 25 1 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 2143 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod documented
84 2143 my ($class, $module) = @_;
85 2143 $module =~ s[::][/]g;
86 2143 $module .= '.pm';
87 2143 ▶ return 1 if $INC{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$module} 1614 529 2143
88 529 eval { require $module; };
89 529 ▶ return $@ ? $class->error("failed to load $module: $@") : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 527 529
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 231 ▶sub parser {
Subroutine parser: called CC=5 SCAR=17.2
Pod documented
104 231 my $class = shift;
105 231 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 229 2 231
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
02229
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 231 ▶ return undef unless $class->load($PARSER);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PARSER) 0 231 231
110 231 ▶ 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
23100
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 268 ▶sub provider {
Subroutine provider: called CC=5 SCAR=17.2
Pod documented
123 268 my $class = shift;
124 268 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 264 4 268
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
04264
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 268 ▶ return undef unless $class->load($PROVIDER);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PROVIDER) 0 268 268
129 268 ▶ 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
26800
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 268 ▶sub plugins {
Subroutine plugins: called CC=5 SCAR=17.2
Pod documented
145 268 my $class = shift;
146 268 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 267 1 268
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01267
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 268 ▶ return undef unless $class->load($PLUGINS);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::PLUGINS) 0 268 268
151 268 ▶ 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
26800
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 268 ▶sub filters {
Subroutine filters: called CC=5 SCAR=17.2
Pod documented
167 268 my $class = shift;
168 268 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 267 1 268
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01267
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 268 ▶ return undef unless $class->load($FILTERS);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::FILTERS) 0 268 268
173 268 ▶ 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
26800
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 274 ▶sub iterator {
Subroutine iterator: called CC=3 SCAR=12.0
Pod documented
189 274 my $class = shift;
190 274 my $list = shift;
191  
192 274 ▶ return undef unless $class->load($ITERATOR);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::ITERATOR) 0 274 274
193 274 ▶ 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
27400
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 274 ▶sub stash {
Subroutine stash: called CC=5 SCAR=17.2
Pod documented
206 274 my $class = shift;
207 274 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 273 1 274
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01273
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 274 ▶ return undef unless $class->load($STASH);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::STASH) 0 274 274
212 274 ▶ 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
27400
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 267 ▶sub context {
Subroutine context: called CC=5 SCAR=16.6
Pod documented
228 267 my $class = shift;
229 267 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 265 2 267
Condition100%3/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
11265
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 267 ▶ return undef unless $class->load($CONTEXT);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::CONTEXT) 0 267 267
234 267 ▶ 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
26700
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 253 ▶sub service {
Subroutine service: called CC=5 SCAR=17.2
Pod documented
250 253 my $class = shift;
251 253 ▶ my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 252 1 253
Condition66%2/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
01252
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 253 ▶ return undef unless $class->load($SERVICE);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->load($Template::Config::SERVICE) 0 253 253
256 253 ▶ 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
25300
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 10 ▶sub constants {
Subroutine constants: called CC=5 SCAR=19.4
Pod documented
273 10 my $class = shift;
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 6 ▶sub instdir {
Subroutine instdir: called CC=4 SCAR=13.9
Pod documented
295 6 my ($class, $dir) = @_;
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 15 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
319 15 my ($class, $textref) = @_;
320 15 bless $textref, $class;
321 }
322 7 ▶sub PRINT {
Subroutine PRINT: called CC=1 SCAR=0.0
323 7 my $self = shift;
324 7 $$self .= join('', @_);
325 }
326 3 ▶sub PRINTF {
Subroutine PRINTF: called CC=1 SCAR=0.0
327 3 my $self = shift;
328 3 my $fmt = shift;
329 3 $$self .= sprintf($fmt, @_);
330 }
331  
332  
333  
334 117 1;
335  
336 __END__
« lib/Template/Plugin/Directory.pm ↑ Summary lib/Template/Plugin/URL.pm »