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

Timothy S. Nelson wayland at wayland.id.au
Mon Apr 20 22:35:41 PDT 2009


On Mon, 20 Apr 2009, Alec Clews wrote:

> Sorry, not explicit enought
>
> Without the PREPEND option Text::Template works fine, BUT does not
> report that $NAME2 is missing. With PREPEND I get an error message
> about $NAME1

 	So isn't the solution to use PREPEND?  Either on the ->new or as you 
have?  How are the results that you get with PREPEND different than what you 
want?

 	:)

>
> On Mon, Apr 20, 2009 at 7:20 AM, Timothy S. Nelson
> <wayland at wayland.id.au> wrote:
>> 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-----
>>
>>
>
>
>
> -- 
> Alec Clews
> Personal <alec.clews at gmail.com>             Melbourne, Australia.
> Jabber:  alecclews at jabber.org.au             PGPKey ID: 0x9BBBFC7C
> blog:http://alecthegeek.wordpress.com/
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm

---------------------------------------------------------------------
| 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