Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I can't figure out what is going on with this snippet. I have a template stored in a __DATA__ section that works fine. When the same code is stored in a module, the template produces a HTML::Template syntax error. As far as I can tell, the __DATA __ sections are the same and seem top be loaded fine. This works fine:
#!/usr/bin/perl use HTML::Template; my @html = <DATA>; my $template = HTML::Template->new( arrayref => \@html); print $template->output; __DATA__ <html> <head> </head> <body> <TMPL_VAR NAME=VALUE> </body> </html>
But this creates an error:
package Foo; use HTML::Template; sub run{ my @html = <DATA>; my $template = HTML::Template->new(arrayref => \@html); print $template->output; } 1; __DATA__ <html> <head> </head> <body> <TMPL_VAR NAME=VALUE> </body> </html>
output:
HTML::Template->new() : Syntax error in <TMPL_*> tag at /fake/path/for +/non/file/template : 5. at /usr/lib/perl5/site_perl/5.8.0/HTML/Templa +te.pm line 2243, <DATA> line 7.

Replies are listed 'Best First'.
Re: HTML::Template in a module's __DATA__ section
by shenme (Priest) on Sep 12, 2004 at 01:29 UTC
    The chief confusion for me is why you don't consistently get the error message. The error message is correctly complaining about your code. You don't have a definition for variable 'VALUE' and so the error message is saying something is wrong with the statement.

    I can't seem to provoke the error message even when using your code. It works in both versions, well, after I put the "Foo->run();" statement into the second version. Otherwise your second version outputs nothing.

    This is the code I was playing with:

    #!/usr/bin/perl use strict; use warnings; Foo->run(); package Foo; use HTML::Template; sub run { my @html = <DATA>; my $template = HTML::Template->new( arrayref => \@html, strict => 1, + die_on_bad_params => 1 ); $template->param( VALUE => 'pick me - pick me!' ); print $template->output; } 1; __DATA__ <html> <head> </head> <body> <TMPL_VAR NAME=VALUE> </body> </html>
    which outputs
    <html> <head> </head> <body> pick me - pick me! </body> </html>
    You might also want to upgrade to version 2.7. It is a couple years newer than the version 2.6 that you are running (but forgot to mention)
      Yay, thanks!

      The version was the problem. I pasted in your posted code, and it gave an error too, so I hit CPAN for an upgrade and viola, it worked. I didn't even think of an error in HTML::Template.

      As to why I don't get an error message in the original, not providing a substitution value is not an error, it just swaps out the value with an empty string.

      Thanks again, you guys rock.

Re: HTML::Template in a module's __DATA__ section
by ikegami (Patriarch) on Sep 12, 2004 at 00:59 UTC

    I think <DATA> always reads from the __DATA__/__END__ in the startup file. (You could test this yourself by copying the template into the .pl file.) You could do the following instead of using <DATA>:

    my @html = <<'__EOD__' =~ m#(.*?$/)#g; <html> <head> </head> <body> <TMPL_VAR NAME=VALUE> </body> </html> __EOD__
      Actually, the __DATA__ section seems to be read in just fine, hence the syntax error on the template tag. I have tried to test this by printing and examining the contents before sending to the HTML::Template constructor, and it looks perfectly normal. I have solved the immediate issue by just creating a method that returns the template as a scalar, but I am still very curious as to why this did not work as I had expected.

      (I also tried using <Foo::DATA> to ensure that the correct package was being read.)

      Thanks for the reply, and any other suggestions are welcome.