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

I am a first time poster so please be gentle.
Basically I am writing a lightweight templating application.
I want to be able to let html coders link templates together by adding a tag like: <% link_name faq_template %> and have this replaced with the appropriate a href based on a lookup of the link name "faq_template" in the database.
The output will look something like:
<a href="http://www.somedomain.com/?q_act=view&q_srn=1719">
Where 1719 is the template_id that corresponds with the faq_template -- After which mod_perl handles the rest :-)
Anyway, not to get too heavily involved in the architecture, I'm slurping up the template that contains
the <%link_name tag into $$file. I take advantage of code evaluation in my regexp
(which also makes sure all the tags are formatted the same way) and do something like the following:

========================

##BEGIN preparse #makes use of experimental code evaluation regexp features in +perl my @link_names; $$file =~ s/<%[ ]*link_name[ ]*([^ ]*)[ ]*%>(?{push(@link_name +s,$1);})/<% link_name $1%>/g; #printStruct(\@link_names); #debugging #now map link names to actual variables my $sql; foreach my $link_name (@link_names){ $link_name=$dbh->quote($link_name); $sql="SELECT template_id FROM template_table where template_ +name=$link_name"; #print "SQL is $sql \n"; #if success do another regexp find and replace with real url + #otherwise replace template tag with HTML code comment indic +ating error }#end foreach ##END preparse

===========================

This works fine. The @link_names array is populated
correctly. My only concern is that code evaluation is
indicated in the docs as a very experimental thing.
Will this be deprecated in the future or should I not worry about it?

Rodney A. Hampton

Replies are listed 'Best First'.
Re: Is it safe to use code evaluation regexp or will it be deprecated
by broquaint (Abbot) on Oct 22, 2002 at 16:20 UTC
    My only concern is that code evaluation is indicated in the docs as a very experimental thing. Will this be deprecated in the future or should I not worry about it?
    It is very unlikely that these regexp extensions are to be dropped, let alone without notice.

    BTW: You may want to look at HTML::Template for a lightweight templating system.
    HTH

    _________
    broquaint

Re: Is it safe to use code evaluation regexp or will it be deprecated
by fruiture (Curate) on Oct 22, 2002 at 16:40 UTC

    Well, you don't need it in that situation:

    $$file =~ s/<%[ ]*link_name[ ]*([^ ]*)[ ]*%>(?{push(@link_names,$1);})/<% link_n +ame $1%>/g; #can be rewritten as (changed single space to \s, might # be more usefull $$file =~ s{ <% \s* link_name \s* (\S*) \s* %> }{ push @link_names,$1; "<% link_name $1 %>" }xg; # but in fact you don't even need s/// while($$file =~ m{ <% \s* link_name \s* (\S*) \s* %> }xg ){ push @link_names,$1 }

    It looks like you first look for each link in the template, then you fetch _each_ number from the database and replace each number again. I'd recommand you either use s///e and put a function-call into the replacement, where the function fetches the database information and returns the link or HTML comment for errors, or you do your search first, but keep in mind the positions of your links, so you'll find'em much faster. The second method has the great advantage that you can fetch all link-information at once.

    while($$file =~ m{( <% \s* link_name \s* (\S*) \s* %> )}xg ){ push @link_names,[pos($$file)-length($1),length($1),$2] } #now fetch stuff from database at once if(@link_names){ my $sql = 'SELECT template_id from template_table where '. join(' OR ',(('template_name = ?')x @link_names)); #... you're hopefully using DBI $sth->execute( @link_names ); #... #replacement can be done easily and _fast_ because # you know the positions of your links and their # length, so use substr(!) here }
    --
    http://fruiture.de