[Melbourne-pm] How to make Text::Template error with undeclared value name

Alec Clews alec.clews at gmail.com
Sat Apr 18 10:29:56 PDT 2009


G'Day Mongers,

I'm using Text::Template, and jolly good it is too, but I seem to have a
problem with error checking.

I want Text::Template to fail if it is given a template that references
a variable which is not supplied. i.e. my template values is incomplete.
>From the docs it appears the correct approach is to make the template
compile with strict.

NB: I am passing template values as a hash.

I figure this code should work
-----------------8<------------------------8<-----------------
use strict;
use warnings;

use Text::Template; # Standard lib

my $template = Text::Template->new(
    TYPE       => 'ARRAY',
    SOURCE     => [    'Text  { $NAME1 }'."\n",
            'Text  { $NAME2 }'."\n"]
) or print qq!Could not construct template $Text::Template::ERROR\n!;

# Set up one value, forget the second one on purpose
my $i = { NAME1 => "VALUE1" };

# Fill in the template
$template->fill_in( OUTPUT => \*STDOUT, HASH => $i, PREPEND => q{use
strict 'vars'}
)
    or print qq!Could not fill in template Error code
$Text::Template::ERROR\n!;
-----------------8<------------------------8<-----------------

Without the PREPEND option everything works fine, except that I am not
told that $NAME2 is missing. I figure I have some scoping problem, but I
can't work out what it is.

Many thanks for any suggestions you might have

-- 
Alec Clews
Personal <alec.clews at gmail.com>			Melbourne, Australia.
Jabber:  alecclews at jabber.org.au		PGPKey ID: 0x9BBBFC7C
Blog  http://alecthegeek.wordpress.com/



More information about the Melbourne-pm mailing list