blib/lib/Template.pm untested

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