问题
Is there a way to make Perl's Template
display warnings for all undefined values that I attempt to use the GET directive on (via [% %]
) during Template::process
?
The default behavior is to ignore and move on. I'd like to warn only in the case of undefined values, if possible, and to log messages to STDERR.
回答1:
Yes. If you pass the DEBUG option to Template->new
, TT will warn you about undefined values.
See the docs here: http://search.cpan.org/dist/Template-Toolkit/lib/Template/Manual/Variables.pod
回答2:
You are looking for:
DEBUG_UNDEF
This option causes the Template Toolkit to throw an 'undef' error whenever it encounters an undefined variable value.
use Template::Constants qw( :debug );
my $template = Template->new({
DEBUG => DEBUG_UNDEF,
});
(From http://search.cpan.org/dist/Template-Toolkit/lib/Template/Manual/Config.pod.)
If you want to do some special handling of the exception, you'll need to catch it or replace the __DIE__ signal handler.
Let's put it together:
#!/usr/bin/perl
use strict;
use warnings;
use Template;
use Template::Constants qw( :debug );
my $debug_tt = Template->new({
DEBUG => DEBUG_UNDEF,
});
my $tt = Template->new();
my $vars = {
something => "42",
};
my $template = <<EOF;
First something undefined: [% nothing %].
And now something defined: [% something %].
EOF
my $output = '';
eval {$debug_tt->process(\$template, $vars, \$output)};
warn $debug_tt->error() if $debug_tt->error();
$tt->process(\$template, $vars);
The output is:
undef error - nothing is undefined
First something undefined: .
And now something defined: 42.
My approach was to use two different instances of the Template class:
$debug_tt
which has theDEBUG_UNDEF
flag turned on and hides its output in the$output
variable.$tt
which is a vanilla instance and prints its output toSTDOUT
as is the default.
Both instances use the same template stored in $template
and the same variable hash stored in $vars
. $debug_tt
is wrapped in an eval
to avoid exiting prematurely and a warning is emitted if $debug_tt->error()
is true. $tt
is executed normally. I think this satisfies your main requirements although it probably isn't efficient. We need to parse $template
twice using this method.
Some thoughts I had working on this:
It would have been nice if
Template::Toolkit
had usedCarp
so that we could get a bit more context on the warning.One could probably derive a class from Template that would
warn
instead ofdie
on error. I didn't feel like doing that.Depending on how your templates are set up, it might make sense to feed it in one line at a time so that you could emit line numbers when an undefined value is found.
It should be possible to alter the templates to test for their own errors and emit more sensible text in the face of undefined values.
来源:https://stackoverflow.com/questions/571898/can-perls-template-toolkit-warn-on-undefined-values