1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
#============================================================= -*-perl-*-
#
# Template::Config
#
# DESCRIPTION
# Template Toolkit configuration module.
#
# AUTHOR
# Andy Wardley <abw@kfs.org>
#
# COPYRIGHT
# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#------------------------------------------------------------------------
#
# $Id: Config.pm,v 2.62 2003/04/24 09:14:38 abw Exp $
#
#========================================================================
package Template::Config;
require 5.004;
use strict;
use base qw( Template::Base );
use vars qw( $VERSION $DEBUG $ERROR $INSTDIR
$PARSER $PROVIDER $PLUGINS $FILTERS $ITERATOR
$LATEX_PATH $PDFLATEX_PATH $DVIPS_PATH
$STASH $SERVICE $CONTEXT $CONSTANTS @PRELOAD );
$VERSION = sprintf("%d.%02d", q$Revision: 2.62 $ =~ /(\d+)\.(\d+)/);
$DEBUG = 0 unless defined $DEBUG;
$ERROR = '';
$CONTEXT = 'Template::Context';
$FILTERS = 'Template::Filters';
$ITERATOR = 'Template::Iterator';
$PARSER = 'Template::Parser';
$PLUGINS = 'Template::Plugins';
$PROVIDER = 'Template::Provider';
$SERVICE = 'Template::Service';
$STASH = 'Template::Stash';
$CONSTANTS = 'Template::Namespace::Constants';
@PRELOAD = ( $CONTEXT, $FILTERS, $ITERATOR, $PARSER,
$PLUGINS, $PROVIDER, $SERVICE, $STASH );
# the following is set at installation time by the Makefile.PL
$INSTDIR = '';
# LaTeX executable paths set at installation time by the Makefile.PL
# Empty strings cause the latex(pdf|dvi|ps) filters to throw an error.
$LATEX_PATH = '';
$PDFLATEX_PATH = '';
$DVIPS_PATH = '';
#========================================================================
# --- CLASS METHODS ---
#========================================================================
#------------------------------------------------------------------------
# preload($module, $module, ...)
#
# Preloads all the standard TT modules that are likely to be used, along
# with any other passed as arguments.
#------------------------------------------------------------------------
sub preload {
my $class = shift;
foreach my $module (@PRELOAD, @_) {
$class->load($module) || return;
};
return 1;
}
#------------------------------------------------------------------------
# load($module)
#
# Load a module via require(). Any occurences of '::' in the module name
# are be converted to '/' and '.pm' is appended. Returns 1 on success
# or undef on error. Use $class->error() to examine the error string.
#------------------------------------------------------------------------
sub load {
my ($class, $module) = @_;
$module =~ s[::][/]g;
$module .= '.pm';
# print STDERR "loading $module\n"
# if $DEBUG;
eval {
require $module;
};
return $@ ? $class->error("failed to load $module: $@") : 1;
}
#------------------------------------------------------------------------
# parser(\%params)
#
# Instantiate a new parser object of the class whose name is denoted by
# the package variable $PARSER (default: Template::Parser). Returns
# a reference to a newly instantiated parser object or undef on error.
# The class error() method can be called without arguments to examine
# the error message generated by this failure.
#------------------------------------------------------------------------
sub parser {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($PARSER);
return $PARSER->new($params)
|| $class->error("failed to create parser: ", $PARSER->error);
}
#------------------------------------------------------------------------
# provider(\%params)
#
# Instantiate a new template provider object (default: Template::Provider).
# Returns an object reference or undef on error, as above.
#------------------------------------------------------------------------
sub provider {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($PROVIDER);
return $PROVIDER->new($params)
|| $class->error("failed to create template provider: ",
$PROVIDER->error);
}
#------------------------------------------------------------------------
# plugins(\%params)
#
# Instantiate a new plugins provider object (default: Template::Plugins).
# Returns an object reference or undef on error, as above.
#------------------------------------------------------------------------
sub plugins {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($PLUGINS);
return $PLUGINS->new($params)
|| $class->error("failed to create plugin provider: ",
$PLUGINS->error);
}
#------------------------------------------------------------------------
# filters(\%params)
#
# Instantiate a new filters provider object (default: Template::Filters).
# Returns an object reference or undef on error, as above.
#------------------------------------------------------------------------
sub filters {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($FILTERS);
return $FILTERS->new($params)
|| $class->error("failed to create filter provider: ",
$FILTERS->error);
}
#------------------------------------------------------------------------
# iterator(\@list)
#
# Instantiate a new Template::Iterator object (default: Template::Iterator).
# Returns an object reference or undef on error, as above.
#------------------------------------------------------------------------
sub iterator {
my $class = shift;
my $list = shift;
return undef unless $class->load($ITERATOR);
return $ITERATOR->new($list, @_)
|| $class->error("failed to create iterator: ", $ITERATOR->error);
}
#------------------------------------------------------------------------
# stash(\%vars)
#
# Instantiate a new template variable stash object (default:
# Template::Stash). Returns object or undef, as above.
#------------------------------------------------------------------------
sub stash {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($STASH);
return $STASH->new($params)
|| $class->error("failed to create stash: ", $STASH->error);
}
#------------------------------------------------------------------------
# context(\%params)
#
# Instantiate a new template context object (default: Template::Context).
# Returns object or undef, as above.
#------------------------------------------------------------------------
sub context {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($CONTEXT);
return $CONTEXT->new($params)
|| $class->error("failed to create context: ", $CONTEXT->error);
}
#------------------------------------------------------------------------
# service(\%params)
#
# Instantiate a new template context object (default: Template::Service).
# Returns object or undef, as above.
#------------------------------------------------------------------------
sub service {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($SERVICE);
return $SERVICE->new($params)
|| $class->error("failed to create context: ", $SERVICE->error);
}
#------------------------------------------------------------------------
# constants(\%params)
#
# Instantiate a new namespace handler for compile time constant folding
# (default: Template::Namespace::Constants).
# Returns object or undef, as above.
#------------------------------------------------------------------------
sub constants {
my $class = shift;
my $params = defined($_[0]) && UNIVERSAL::isa($_[0], 'HASH')
? shift : { @_ };
return undef unless $class->load($CONSTANTS);
return $CONSTANTS->new($params)
|| $class->error("failed to create constants namespace: ",
$CONSTANTS->error);
}
#------------------------------------------------------------------------
# instdir($dir)
#
# Returns the root installation directory appended with any local
# component directory passed as an argument.
#------------------------------------------------------------------------
sub instdir {
my ($class, $dir) = @_;
my $inst = $INSTDIR
|| return $class->error("no installation directory");
$inst =~ s[/$][]g;
$inst .= "/$dir" if $dir;
return $inst;
}
#------------------------------------------------------------------------
# latexpaths()
#
# Returns a reference to a three element array:
# [latex_path, pdf2latex_path, dvips_path]
# These values are determined by Makefile.PL at installation time
# and are used by the latex(pdf|dvi|ps) filters.
#------------------------------------------------------------------------
sub latexpaths {
return [$LATEX_PATH, $PDFLATEX_PATH, $DVIPS_PATH];
}
#========================================================================
# This should probably be moved somewhere else in the long term, but for
# now it ensures that Template::TieString is available even if the
# Template::Directive module hasn't been loaded, as is the case when
# using compiled templates and Template::Parser hasn't yet been loaded
# on demand.
#========================================================================
#------------------------------------------------------------------------
# simple package for tying $output variable to STDOUT, used by perl()
#------------------------------------------------------------------------
package Template::TieString;
sub TIEHANDLE {
my ($class, $textref) = @_;
bless $textref, $class;
}
sub PRINT {
my $self = shift;
$$self .= join('', @_);
}
1;
__END__
#------------------------------------------------------------------------
# IMPORTANT NOTE
# This documentation is generated automatically from source
# templates. Any changes you make here may be lost.
#
# The 'docsrc' documentation source bundle is available for download
# from http://www.template-toolkit.org/docs.html and contains all
# the source templates, XML files, scripts, etc., from which the
# documentation for the Template Toolkit is built.
#------------------------------------------------------------------------
=head1 NAME
Template::Config - Factory module for instantiating other TT2 modules
=head1 SYNOPSIS
use Template::Config;
=head1 DESCRIPTION
This module implements various methods for loading and instantiating
other modules that comprise the Template Toolkit. It provides a consistent
way to create toolkit components and allows custom modules to be used in
place of the regular ones.
Package variables such as $STASH, $SERVICE, $CONTEXT, etc., contain
the default module/package name for each component (Template::Stash,
Template::Service and Template::Context, respectively) and are used by
the various factory methods (stash(), service() and context()) to load
the appropriate module. Changing these package variables will cause
subsequent calls to the relevant factory method to load and instantiate
an object from the new class.
=head1 PUBLIC METHODS
=head2 load($module)
Load a module via require(). Any occurences of '::' in the module name
are be converted to '/' and '.pm' is appended. Returns 1 on success
or undef on error. Use $class-E<gt>error() to examine the error string.
=head2 preload()
This method preloads all the other Template::* modules that are likely
to be used. It is called by the Template module when running under
mod_perl ($ENV{MOD_PERL} is set).
=head2 parser(\%config)
Instantiate a new parser object of the class whose name is denoted by
the package variable $PARSER (default: Template::Parser). Returns
a reference to a newly instantiated parser object or undef on error.
=head2 provider(\%config)
Instantiate a new template provider object (default: Template::Provider).
Returns an object reference or undef on error, as above.
=head2 plugins(\%config)
Instantiate a new plugins provider object (default: Template::Plugins).
Returns an object reference or undef on error, as above.
=head2 filters(\%config)
Instantiate a new filter provider object (default: Template::Filters).
Returns an object reference or undef on error, as above.
=head2 stash(\%vars)
Instantiate a new stash object (Template::Stash or Template::Stash::XS
depending on the default set at installation time) using the contents
of the optional hash array passed by parameter as initial variable
definitions. Returns an object reference or undef on error, as above.
=head2 context(\%config)
Instantiate a new template context object (default: Template::Context).
Returns an object reference or undef on error, as above.
=head2 service(\%config)
Instantiate a new template service object (default: Template::Service).
Returns an object reference or undef on error, as above.
=head2 instdir($dir)
Returns the root directory of the Template Toolkit installation under
which optional components are installed. Any relative directory specified
as an argument will be appended to the returned directory.
# e.g. returns '/usr/local/tt2'
my $ttroot = Template::Config->instdir()
|| die "$Template::Config::ERROR\n";
# e.g. returns '/usr/local/tt2/templates'
my $template = Template::Config->instdir('templates')
|| die "$Template::Config::ERROR\n";
Returns undef and sets $Template::Config::ERROR appropriately if the
optional components of the Template Toolkit have not been installed.
=head1 AUTHOR
Andy Wardley E<lt>abw@andywardley.comE<gt>
L<http://www.andywardley.com/|http://www.andywardley.com/>
=head1 VERSION
2.62, distributed as part of the
Template Toolkit version 2.10, released on 24 July 2003.
=head1 COPYRIGHT
Copyright (C) 1996-2003 Andy Wardley. All Rights Reserved.
Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<Template|Template>
|