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

Please go through following code.

#! /usr/bin/env perl use strict; use warnings; use 5.0080; my $html =<<'HTML'; <div> <p>Code</p> <?perl print "<b>Hello World</b>"; ?> </div> HTML # trial 1 $html =~ s/\<\?perl(.*?)\?\>/$1/gisee; # trial 2 $html =~ s/\<\?perl(.*?)\?\>/(sub{$1})->()/gisee; print $html;

This prints the output as follows:(trial #1 or #2)

<b>Hello World</b><div> <p>Code</p> 1 </div>

But I want as follows:

<div> <p>Code</p> <b>Hello World</b> </div>

Replies are listed 'Best First'.
Re: Eval in RegEx ...
by hdb (Monsignor) on Dec 06, 2013 at 15:22 UTC

    The reason is that your anonymous sub first prints the string and returns the return value of print (usually 1 for successful printing). Then you print the content in the $html variable.

    I would suggest that you re-define your embedded perl in such a way that it returns a string (just remove the print command in the embedded perl. Then the embedded perl section will be replaced by the resulting string which is what you want (I assume). Code could look like this:

    use strict; use warnings; use 5.0080; my $html =<<'HTML'; <div> <p>Code</p> <?perl my $string = "Hello World"; "<b>$string</b>"; ?> </div> HTML $html =~ s/\<\?perl(.*?)\?\>/(sub{$1})->()/gisee; print $html;
      I tried this before but it only works for single output. Actually I read the content ($html) from the template file which has many lines of Perl code to eval embeded in multiple <?perl ?> tags. For every string I had to define it first otherwise it says "Useless use of a constant (...) in void context at (eval ...) line ...". Anyway thanks for reply.

        Actually, hdb's suggestion works as long as EACH <?perl ... /> is embedded in ITS OWN <div> ... </div>.

Re: Eval in RegEx ...
by kennethk (Abbot) on Dec 06, 2013 at 15:24 UTC
    Your issue is that print returns true if the print succeeded. So rather than print "<b>Hello World</b>";, you probably just want "<b>Hello World</b>"; (or return "<b>Hello World</b>";), since subroutines return the last thing evaluated.

    As a side note, this sounds like you are doing something imprudent, either from a security or reinventing the wheel perspective. This type of templating is natural in Template::Toolkit, and might be worth a perusal.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Though Template::Plugin is useful but not suit for my job.

      And secondly I'm very much fond of "reinventing the wheel"!!!

      Thanks anyway.

      P.S> I don't require too much templating, I've a specific job and that too without any additional modules, as on a very ligh weight and small system which has only core modules available (perl 5.8.8)

        Then you might consider running HTML::Template with a filter. Super-lightweight, no dependencies, pure Perl and compatible going way back.

        Was the issue resolved?


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Eval in RegEx ...
by taint (Chaplain) on Dec 06, 2013 at 13:54 UTC
    Greetings.

    Well, that's simple enough. Simply change

    my $html =<<'HTML'; <div> <p>Code</p> <?perl print "<b>Hello World</b>"; ?> </div> HTML
    into
    my $html =<<'HTML'; <div> <p>Code</p> <?perl print " <b>Hello World</b>"; ?> </div> HTML
    Done. :)

    Best wishes.

    --Chris

    Hey. I'm not completely useless. I can be used as a bad example.
    
      Nope... thanks anyway...
        Odd. I'ts simple heredoc notation/quoting. I use it all the time. I tried it here, before posting the reply, and it worked. Must be more elements involved, than you are providing, that process the format of the output.

        Sorry.

        --Chris

        Hey. I'm not completely useless. I can be used as a bad example.