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

Hi Anybody Can tell me how to use perl in HTML Template. I Also tried to use it by using following code but it was not running.

[% PERL %] [ print "Hello!!";] [% END %] <html> <title> This is Example of using perl with HTML </title> </html>

Replies are listed 'Best First'.
Re: How to use Perl in HTML Template??
by moritz (Cardinal) on Nov 03, 2009 at 07:52 UTC

    It's no surprise that it's not working, what you write is not valid perl after all:

    $ perl -e '[ print "Hello!!";]' syntax error at -e line 1, near ""Hello!!";" Execution of -e aborted due to compilation errors.

    Trying without the [...] might help.

    And in general it is helpful to describe exactly what happens, not just "it was not running".

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: How to use Perl in HTML Template?
by Anonymous Monk on Nov 03, 2009 at 07:53 UTC
    please read How (Not) To Ask A Question.
    $ cat temp.tt [% PERL %] [ print "Hello!!";] [% END %] <html> <title> This is Example of using perl with HTML </title> </html> $ tpage temp.tt perl error - EVAL_PERL not set $ tpage --eval_perl temp.tt undef error - syntax error at (eval 11) line 3, at EOF $ cat temp.tt [% PERL %] print "Hello!!"; [% END %] <html> <title> This is Example of using perl with HTML </title> </html> $ tpage --eval_perl temp.tt Hello!! <html> <title> This is Example of using perl with HTML </title> </html> $
    Template::Tools::tpage, Template::Manual::Directives#PERL

      Thanks Monks for reply and it's working. Now i change the file a little more and got message "Error:EVAL_PERL not set". Could you tell me how to set EVAL_PERL in template . Another issue is that when i run program B it's showing the value of random but in Program A it it not showing the Value of random A.

      Program A

      [% TRY %] [% PERL %] use WebService::CaptchasDotNet; my $o = WebService::CaptchasDotNet->new(secret => 'secret', username => 'demo'); my $random = $o->random; print "random=", $random; my $url = $o->url($random); print $url; [% END %] [% CATCH %] Error:[% error.info %] [% END %] <html> <title> This is Example of using perl with HTML </title> </html>

      Program B

      #!/usr/bin/perl use strict; use warnings; use WebService::CaptchasDotNet; my $o = WebService::CaptchasDotNet->new(secret => 'secret', username = +> 'demo'); my $random = $o->random(); print "Random value = ". $random; my $url = $o->url($random);
        EVAL_PERL needs to be set (or not - I'd advise against putting non-display logic into your templates) in your call to Template->new. e.g.,
        my $tt = Template->new( INCLUDE_PATH => 'tpl/', TRIM => 1, WRAPPER => 'wrapper.tt', EVAL_PERL => 1, );

        Also, you might want to consider changing your question's title... You're doing this with Template::Toolkit, but there's a different (and unrelated) Perl templating module called HTML::Template. I originally read your question expecting to answer that HTML::Template doesn't support Perl in templates at all.

        Could you tell me how to set EVAL_PERL in template .

        Please don't do that. Create a Template::Plugin instead