A small patch to Text::Template.pm that tells you exactly where template warnings/errors are coming from.
If you use the Text::Template module regularly, you know that correlating a a warning/error to its location in your template code can be quite a daunting task. This is, of course, because eval'd warnings and errors calculate line numbers based from the origin of the eval'd block, rather than from the beginning of the template. I added a feature that permits Text::Template to spew such messages with their actual line number info intact, as well as their originating template file name (if applicable). This has made debugging complex templates much faster for me. To use the feature, set the parameter LINENUMS => 1 in your calls to Text::Template->new().

To apply this patch to your Template.pm file, save the patch text below to a file and run patch Template.pm patchfile.

56a57 > my $linenums = _param('linenums', %a); 68c69,70 < (defined $alt_delim ? (DELIM => $alt_delim) : ()), --- > (defined $linenums ? (LINENUMS => $linenums) : ()), > (defined $alt_delim ? (DELIM => $alt_delim) : ()), 85a88 > $self->{FILENAME} = $self->{SOURCE}; 261a265 > my $fi_filename = $fi_self->{FILENAME} || 'text_template'; 272c276,277 < my $fi_progtext = "package $fi_eval_package; $fi_prepend;\n#li +ne 1\n$fi_text" --- > my $fi_lncomment = ($fi_self->{LINENUMS} ? "#line $fi_lineno $ +fi_filename" : "#line 1"); > my $fi_progtext = "package $fi_eval_package; $fi_prepend;\n$fi +_lncomment\n$fi_text"

Replies are listed 'Best First'.
Re: Text::Template debugging made easier
by mirod (Canon) on Dec 11, 2000 at 14:11 UTC