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

Timothy S. Nelson wayland at wayland.id.au
Sun Apr 19 14:20:09 PDT 2009


On Sun, 19 Apr 2009, Alec Clews wrote:

> 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

 	Are you saying it works fine as displayed, but if you leave out the 
PREPEND option, it doesn't work like you want?


---------------------------------------------------------------------
| Name: Tim Nelson                 | Because the Creator is,        |
| E-mail: wayland at wayland.id.au    | I am                           |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V- 
PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+ e++>++++ h! y-
-----END GEEK CODE BLOCK-----



More information about the Melbourne-pm mailing list