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

Hello.

Is it possible to execute the following code versus display it as text?

>>> Scenario: <<<

In the .pm file,

Template code is stored in the '$template_file' var which contains a place holder called {PERL_CODE}

my $c = ... my $perlcode = 'print $c->get_html( 'some text' );'; $template_file =~ s/{PERL_CODE}/$perlcode/;

I tried this, but then had to escape the $c in $perlcode otherwise the value of $c is replaced:

my $perlcode = 'print \$c->get_html( 'some text' );';

The issue is, instead of the print function being executed, the entire statement was printed literally as a text string (which was somewhat expected).

Is there a way to execute the code instead?

Thanks.

Replies are listed 'Best First'.
Re: Possible? String Search, Replace & Execute Code
by Anonymous Monk on Jul 09, 2010 at 06:27 UTC

      Thanks for your reply.

      Q: Is there a way to use the variable '$template_file' instead of filename => 'test.tmpl' in the statement below since the file content has been retrieved already? I'm using existing code and it would be a bit more entailed to try to fit that statement 'as is' to that code.

      my $template = HTML::Template->new(filename => 'test.tmpl')

      Note: That statement is from CPAN's HTML::Template page.

      Thanks.
        Do you mean:
        my $template = HTML::Template->new(filename => $template_file);
        ?