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

There is a bug in Template -- ok, supposidly this is actually a bug in perl, but I am sure that the author of Template could have worked around the issue. Basically, you cannot use the 'Interpolate' feature of the module when loading a template larger than ~30k; if you do, perl will dump core, segfault, or do something of the sort.

Here's the example that I have in which Template fails:

my $TMPL = Template->new( { DELIMITER => ';', INCLUDE_PATH => 'C:/Apache2/sites/foobar/template', INTERPOLATE => 1 } ); $TMPL->process( \*DATA, { screen => qw(screen1 screen2)[rand 2] } ); __END__ [% INCLUDE primary/html_head.tmpl %] [% INCLUDE "$screen" %] [% INCLUDE primary/html_foot.tmpl %] [% BLOCK screen1 %] This is screen #1. [% END %] [% BLOCK screen2 %] This is screen #2. [% END %]

Of course, it is possible to work around this in this fashion:

__END__ [% INCLUDE primary/html_head.tmpl %] [% IF screen == 'screen1' %] [% INCLUDE screen1 %] [% ELSIF screen == 'screen2' %] [% INCLUDE screen2 %] [% END %] [% INCLUDE primary/html_foot.tmpl %]

But if I have several screens, this can get very annoying to code and is definitely ugly. Can anybody think of a way to get around this alternative of mine?

Replies are listed 'Best First'.
Re: Interpolate within Template
by edoc (Chaplain) on Jan 13, 2005 at 01:36 UTC

    If I'm understanding the problem correctly, there is no problem.

    Are you saying that you need INTERPOLATE => 1 to get [% INCLUDE "$screen" % ] to work?

    This works ok for me:

    use Template; my $TMPL = Template->new(); my $res; $TMPL->process( \*DATA, { screen => qw(screen1 screen2)[rand 2] }, \$res ); print "$res\n"; __END__ [% INCLUDE "$screen" %] [% BLOCK screen1 %] This is screen #1. [% END %] [% BLOCK screen2 %] This is screen #2. [% END %]

    From the docs:

    The INTERPOLATE flag, when set to any true value will cause variable references in plain text (i.e. not surrounded by START_TAG and END_TAG) to be recognised and interpolated accordingly.

    and you don't need to do that. In fact you really don't want to do that..

    cheers,

    J

      OMG, I feel stupid. Thank you ever so much.

Re: Interpolate within Template
by Tanktalus (Canon) on Jan 12, 2005 at 18:05 UTC

    To be honest, I've hit a number of memory "bugs" in perl - upgrading from 5.6 to 5.8 has solved almost every single one of them (upgrading to 5.8.1 solving almost all of the rest). If you are using 5.6, and you can upgrade to 5.8, that would be my suggestion.