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

Hello.

What is the syntax to use new() for the following:

The form template is already stored in the variable $template_file which contains the placeholder called '<TMPL_VAR NAME=PERLCODE>' that need to be replaced with e.g. the phrase 'Perl code goes here'.

I'll replace the phrase with the actual code once I have the syntax setup to make that replacement.

Thanks.

Replies are listed 'Best First'.
Re: How? Use new() from HTML::Template
by ahmad (Hermit) on Jul 23, 2010 at 04:38 UTC

    Here's an example:

    #!/usr/bin/perl use strict; use warnings; use HTML::Template; my $template_file = '<TMPL_VAR NAME="PERLCODE">'; my $t = HTML::Template->new( scalarref => \$template_file ); $t->param( PERLCODE => 'Perl code goes here' ); print $t->output;

      Now ahmad, that looks clearer than what I read on the CPAN page. I'll give it a try.

      Thanks!

        Hello again.

        BACKGROUND:
        I'm trying to use that code in ahmad's reply to add reCaptcha to a form in a template file. That template file is a static file, not a Perl executable file like .pl nor .pm.

        ===

        Within the form code section in that template file, I added a placeholder in hopes of adding and executing a single Perl statement once that template file was called by the .pm file. I've renamed that placeholder from <TMPL_VAR NAME="PERLCODE"> to <RECAPTCHA>.

        I tried adding the Perl statements as shown below inside of the .pm file that calls the template file, but with each version I tried, it generated a 'Software Error' pointing to the '$t->param...' statement. There's other code prior to this code I added.

        use HTML::Template; use Captcha::reCAPTCHA; my $c = Captcha::reCAPTCHA->new; my $t = HTML::Template->new( scalarref => \$template_file ); $t->param( <RECAPTCHA> => "print $c->get_html( 'your public key here' +);" ); $Output = $t->output;
        (NOTE: I substituted the key for 'your public key here'.)

        This routine is part of a subroutine where the final $Output var is returned.

        What might be done to get this to work?

        Thanks.
Re: How? Use new() from HTML::Template
by ww (Archbishop) on Jul 23, 2010 at 04:45 UTC

      Thanks for your reply, ww.

      I had previously consulted that CPAN reference in the thread you referenced that I created. I had also searched for other tutorials before and after I posted that previous referenced node. I added 2 replies there that went unanswered which I figured was due to the node's age. The first of those 2 replies was during the time the Perlmonks server was on its way down for a couple of days or so. The next was a week later. Now I may have the answer in this thread that I can give a try.

      Thanks for your assistance, too.