This file is indexed.

/usr/lib/perl5/Template/Service.pm is in libtemplate-perl 2.24-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#============================================================= -*-Perl-*-
#
# Template::Service
#
# DESCRIPTION
#   Module implementing a template processing service which wraps a
#   template within PRE_PROCESS and POST_PROCESS templates and offers 
#   ERROR recovery.
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
# COPYRIGHT
#   Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
# 
#============================================================================

package Template::Service;

use strict;
use warnings;
use base 'Template::Base';
use Template::Config;
use Template::Exception;
use Template::Constants;
use Scalar::Util 'blessed';

use constant EXCEPTION => 'Template::Exception';

our $VERSION = 2.80;
our $DEBUG   = 0 unless defined $DEBUG;
our $ERROR   = '';


#========================================================================
#                     -----  PUBLIC METHODS -----
#========================================================================

#------------------------------------------------------------------------
# process($template, \%params)
#
# Process a template within a service framework.  A service may encompass
# PRE_PROCESS and POST_PROCESS templates and an ERROR hash which names
# templates to be substituted for the main template document in case of
# error.  Each service invocation begins by resetting the state of the 
# context object via a call to reset().  The AUTO_RESET option may be set 
# to 0 (default: 1) to bypass this step.
#------------------------------------------------------------------------

sub process {
    my ($self, $template, $params) = @_;
    my $context = $self->{ CONTEXT };
    my ($name, $output, $procout, $error);
    $output = '';

    $self->debug("process($template, ", 
                 defined $params ? $params : '<no params>',
                 ')') if $self->{ DEBUG };

    $context->reset()
        if $self->{ AUTO_RESET };

    # pre-request compiled template from context so that we can alias it 
    # in the stash for pre-processed templates to reference
    eval { $template = $context->template($template) };
    return $self->error($@)
        if $@;

    # localise the variable stash with any parameters passed
    # and set the 'template' variable
    $params ||= { };
    # TODO: change this to C<||=> so we can use a template parameter
    $params->{ template } = $template 
        unless ref $template eq 'CODE';
    $context->localise($params);

    SERVICE: {
        # PRE_PROCESS
        eval {
            foreach $name (@{ $self->{ PRE_PROCESS } }) {
                $self->debug("PRE_PROCESS: $name") if $self->{ DEBUG };
                $output .= $context->process($name);
            }
        };
        last SERVICE if ($error = $@);

        # PROCESS
        eval {
            foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
                $self->debug("PROCESS: $name") if $self->{ DEBUG };
                $procout .= $context->process($name);
            }
        };
        if ($error = $@) {
            last SERVICE
                unless defined ($procout = $self->_recover(\$error));
        }
        
        if (defined $procout) {
            # WRAPPER
            eval {
                foreach $name (reverse @{ $self->{ WRAPPER } }) {
                    $self->debug("WRAPPER: $name") if $self->{ DEBUG };
                    $procout = $context->process($name, { content => $procout });
                }
            };
            last SERVICE if ($error = $@);
            $output .= $procout;
        }
        
        # POST_PROCESS
        eval {
            foreach $name (@{ $self->{ POST_PROCESS } }) {
                $self->debug("POST_PROCESS: $name") if $self->{ DEBUG };
                $output .= $context->process($name);
            }
        };
        last SERVICE if ($error = $@);
    }

    $context->delocalise();
    delete $params->{ template };

    if ($error) {
    #   $error = $error->as_string if ref $error;
        return $self->error($error);
    }

    return $output;
}


#------------------------------------------------------------------------
# context()
# 
# Returns the internal CONTEXT reference.
#------------------------------------------------------------------------

sub context {
    return $_[0]->{ CONTEXT };
}


#========================================================================
#                     -- PRIVATE METHODS --
#========================================================================

sub _init {
    my ($self, $config) = @_;
    my ($item, $data, $context, $block, $blocks);
    my $delim = $config->{ DELIMITER };
    $delim = ':' unless defined $delim;

    # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary, 
    # by splitting on non-word characters
    foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS WRAPPER )) {
        $data = $config->{ $item };
        $self->{ $item } = [ ], next unless (defined $data);
        $data = [ split($delim, $data || '') ]
            unless ref $data eq 'ARRAY';
        $self->{ $item } = $data;
    }
    # unset PROCESS option unless explicitly specified in config
    $self->{ PROCESS } = undef
        unless defined $config->{ PROCESS };
    
    $self->{ ERROR      } = $config->{ ERROR } || $config->{ ERRORS };
    $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET }
                            ? $config->{ AUTO_RESET } : 1;
    $self->{ DEBUG      } = ( $config->{ DEBUG } || 0 )
                            & Template::Constants::DEBUG_SERVICE;
    
    $context = $self->{ CONTEXT } = $config->{ CONTEXT }
        || Template::Config->context($config)
        || return $self->error(Template::Config->error);
    
    return $self;
}


#------------------------------------------------------------------------
# _recover(\$exception)
#
# Examines the internal ERROR hash array to find a handler suitable 
# for the exception object passed by reference.  Selecting the handler
# is done by delegation to the exception's select_handler() method, 
# passing the set of handler keys as arguments.  A 'default' handler 
# may also be provided.  The handler value represents the name of a 
# template which should be processed. 
#------------------------------------------------------------------------

sub _recover {
    my ($self, $error) = @_;
    my $context = $self->{ CONTEXT };
    my ($hkey, $handler, $output);

    # there shouldn't ever be a non-exception object received at this
    # point... unless a module like CGI::Carp messes around with the 
    # DIE handler. 
    return undef
        unless blessed($$error) && $$error->isa(EXCEPTION);

    # a 'stop' exception is thrown by [% STOP %] - we return the output
    # buffer stored in the exception object
    return $$error->text()
        if $$error->type() eq 'stop';

    my $handlers = $self->{ ERROR }
        || return undef;                    ## RETURN

    if (ref $handlers eq 'HASH') {
        if ($hkey = $$error->select_handler(keys %$handlers)) {
            $handler = $handlers->{ $hkey };
            $self->debug("using error handler for $hkey") if $self->{ DEBUG };
        }
        elsif ($handler = $handlers->{ default }) {
            # use default handler
            $self->debug("using default error handler") if $self->{ DEBUG };
        }
        else {
            return undef;                   ## RETURN
        }
    }
    else {
        $handler = $handlers;
        $self->debug("using default error handler") if $self->{ DEBUG };
    }
    
    eval { $handler = $context->template($handler) };
    if ($@) {
        $$error = $@;
        return undef;                       ## RETURN
    };
    
    $context->stash->set('error', $$error);
    eval {
        $output .= $context->process($handler);
    };
    if ($@) {
        $$error = $@;
        return undef;                       ## RETURN
    }

    return $output;
}



#------------------------------------------------------------------------
# _dump()
#
# Debug method which return a string representing the internal object
# state. 
#------------------------------------------------------------------------

sub _dump {
    my $self = shift;
    my $context = $self->{ CONTEXT }->_dump();
    $context =~ s/\n/\n    /gm;

    my $error = $self->{ ERROR };
    $error = join('', 
          "{\n",
          (map { "    $_ => $error->{ $_ }\n" }
           keys %$error),
          "}\n")
    if ref $error;
    
    local $" = ', ';
    return <<EOF;
$self
PRE_PROCESS  => [ @{ $self->{ PRE_PROCESS } } ]
POST_PROCESS => [ @{ $self->{ POST_PROCESS } } ]
ERROR        => $error
CONTEXT      => $context
EOF
}


1;

__END__

=head1 NAME

Template::Service - General purpose template processing service

=head1 SYNOPSIS

    use Template::Service;
    
    my $service = Template::Service->new({
        PRE_PROCESS  => [ 'config', 'header' ],
        POST_PROCESS => 'footer',
        ERROR        => {
            user     => 'user/index.html', 
            dbi      => 'error/database',
            default  => 'error/default',
        },
    });
    
    my $output = $service->process($template_name, \%replace)
        || die $service->error(), "\n";

=head1 DESCRIPTION

The C<Template::Service> module implements an object class for providing
a consistent template processing service. 

Standard header (L<PRE_PROCESS|PRE_PROCESS_POST_PROCESS>) and footer
(L<POST_PROCESS|PRE_PROCESS_POST_PROCESS>) templates may be specified which
are prepended and appended to all templates processed by the service (but not
any other templates or blocks C<INCLUDE>d or C<PROCESS>ed from within). An
L<ERROR> hash may be specified which redirects the service to an alternate
template file in the case of uncaught exceptions being thrown. This allows
errors to be automatically handled by the service and a guaranteed valid
response to be generated regardless of any processing problems encountered.

A default C<Template::Service> object is created by the L<Template> module.
Any C<Template::Service> options may be passed to the L<Template>
L<new()|Template#new()> constructor method and will be forwarded to the
L<Template::Service> constructor.

    use Template;
    
    my $template = Template->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
    });

Similarly, the C<Template::Service> constructor will forward all configuration
parameters onto other default objects (e.g. L<Template::Context>) that it may
need to instantiate.

A C<Template::Service> object (or subclass) can be explicitly instantiated and
passed to the L<Template> L<new()|Template#new()> constructor method as the
L<SERVICE> item.

    use Template;
    use Template::Service;
    
    my $service = Template::Service->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
    });
    
    my $template = Template->new({
        SERVICE => $service,
    });

The C<Template::Service> module can be sub-classed to create custom service
handlers.

    use Template;
    use MyOrg::Template::Service;
    
    my $service = MyOrg::Template::Service->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
        COOL_OPTION  => 'enabled in spades',
    });
    
    my $template = Template->new({
        SERVICE => $service,
    });

The L<Template> module uses the L<Template::Config>
L<service()|Template::Config#service()> factory method to create a default
service object when required. The C<$Template::Config::SERVICE> package
variable may be set to specify an alternate service module. This will be
loaded automatically and its L<new()> constructor method called by the
L<service()|Template::Config#service()> factory method when a default service
object is required. Thus the previous example could be written as:

    use Template;
    
    $Template::Config::SERVICE = 'MyOrg::Template::Service';
    
    my $template = Template->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
        COOL_OPTION  => 'enabled in spades',
    });

=head1 METHODS

=head2 new(\%config)

The C<new()> constructor method is called to instantiate a C<Template::Service>
object.  Configuration parameters may be specified as a HASH reference or
as a list of C<name =E<gt> value> pairs.

    my $service1 = Template::Service->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
    });
    
    my $service2 = Template::Service->new( ERROR => 'error.html' );

The C<new()> method returns a C<Template::Service> object or C<undef> on
error. In the latter case, a relevant error message can be retrieved by the
L<error()|Template::Base#error()> class method or directly from the
C<$Template::Service::ERROR> package variable.

    my $service = Template::Service->new(\%config)
        || die Template::Service->error();
        
    my $service = Template::Service->new(\%config)
        || die $Template::Service::ERROR;

=head2 process($input, \%replace)

The C<process()> method is called to process a template specified as the first
parameter, C<$input>. This may be a file name, file handle (e.g. C<GLOB> or
C<IO::Handle>) or a reference to a text string containing the template text. An
additional hash reference may be passed containing template variable
definitions.

The method processes the template, adding any
L<PRE_PROCESS|PRE_PROCESS_POST_PROCESS> or
L<POST_PROCESS|PRE_PROCESS_POST_PROCESS> templates defined, and returns the
output text. An uncaught exception thrown by the template will be handled by a
relevant L<ERROR> handler if defined. Errors that occur in the
L<PRE_PROCESS|PRE_PROCESS_POST_PROCESS> or
L<POST_PROCESS|PRE_PROCESS_POST_PROCESS> templates, or those that occur in the
main input template and aren't handled, cause the method to return C<undef> to
indicate failure. The appropriate error message can be retrieved via the
L<error()|Template::Base#error()> method.

    $service->process('myfile.html', { title => 'My Test File' })
        || die $service->error();

=head2 context()

Returns a reference to the internal context object which is, by default, an
instance of the L<Template::Context> class.

=head1 CONFIGURATION OPTIONS

The following list summarises the configuration options that can be provided
to the C<Template::Service> L<new()> constructor. Please consult
L<Template::Manual::Config> for further details and examples of each
configuration option in use.

=head2 PRE_PROCESS, POST_PROCESS

The L<PRE_PROCESS|Template::Manual::Config#PRE_PROCESS_POST_PROCESS> and
L<POST_PROCESS|Template::Manual::Config#PRE_PROCESS_POST_PROCESS> options may
be set to contain the name(s) of template files which should be processed
immediately before and/or after each template. These do not get added to
templates processed into a document via directives such as C<INCLUDE>
C<PROCESS>, C<WRAPPER>, etc.

    my $service = Template::Service->new({
        PRE_PROCESS  => 'header',
        POST_PROCESS => 'footer',
    };

Multiple templates may be specified as a reference to a list.  Each is 
processed in the order defined.

    my $service = Template::Service->new({
        PRE_PROCESS  => [ 'config', 'header' ],
        POST_PROCESS => 'footer',
    };

=head2 PROCESS

The L<PROCESS|Template::Manual::Config#PROCESS> option may be set to contain
the name(s) of template files which should be processed instead of the main
template passed to the C<Template::Service> L<process()> method. This can be used to
apply consistent wrappers around all templates, similar to the use of
L<PRE_PROCESS|PRE_PROCESS_POST_PROCESS> and 
L<POST_PROCESS|PRE_PROCESS_POST_PROCESS> templates.

    my $service = Template::Service->new({
        PROCESS  => 'content',
    };
    
    # processes 'content' instead of 'foo.html'
    $service->process('foo.html');

A reference to the original template is available in the C<template>
variable.  Metadata items can be inspected and the template can be
processed by specifying it as a variable reference (i.e. prefixed by
'C<$>') to an C<INCLUDE>, C<PROCESS> or C<WRAPPER> directive.

Example C<PROCESS> template:

    <html>
      <head>
        <title>[% template.title %]</title>
      </head>
      <body>
      [% PROCESS $template %]
      </body>
    </html>

=head2 ERROR

The L<ERROR|Template::Manual::Config#ERROR> (or C<ERRORS> if you prefer)
configuration item can be used to name a single template or specify a hash
array mapping exception types to templates which should be used for error
handling. If an uncaught exception is raised from within a template then the
appropriate error template will instead be processed.

If specified as a single value then that template will be processed 
for all uncaught exceptions. 

    my $service = Template::Service->new({
        ERROR => 'error.html'
    });

If the L<ERROR/ERRORS|Template::Manual::Config#ERROR> item is a hash reference
the keys are assumed to be exception types and the relevant template for a
given exception will be selected. A C<default> template may be provided for
the general case.

    my $service = Template::Service->new({
        ERRORS => {
            user     => 'user/index.html',
            dbi      => 'error/database',
            default  => 'error/default',
        },
    });

=head2 AUTO_RESET

The L<AUTO_RESET|Template::Manual::Config#AUTO_RESET> option is set by default
and causes the local C<BLOCKS> cache for the L<Template::Context> object to be
reset on each call to the L<Template> L<process()|Template#process()> method.
This ensures that any C<BLOCK>s defined within a template will only persist until
that template is finished processing. 

=head2 DEBUG

The L<DEBUG|Template::Manual::Config#DEBUG> option can be used to enable
debugging messages from the C<Template::Service> module by setting it to include
the C<DEBUG_SERVICE> value.

    use Template::Constants qw( :debug );
    
    my $template = Template->new({
        DEBUG => DEBUG_SERVICE,
    });

=head1 AUTHOR

Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>

=head1 COPYRIGHT

Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.

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>, L<Template::Context>

=cut

# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: