$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g; $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumlistings%%/$premiumitem/g;
Don't do several substitutions one after another, you could end up replacing what you replaced, again. For example:
$_ = 'taxial'; s/taxi/cab/g; s/cabal/gang/g; print;
prints "gang".

Better would be to replace both at once:

$_ = 'taxial'; my %subst = ( 'taxi' => 'cab', 'cabal' => 'gang' ); local $" = "|"; my @keys = map quotemeta, sort { length $b <=> length $a } keys %sub +st; my $regexp = qr(@keys); s/($regexp)/$subst{$1}/g; print;
which prints "cabal". Once the first substitution went past "taxi", replacing it with "cab", we've moved too far to replace it again in the second substitution. Usually, this is exactly what we want.

I like Regex::PreSuf (abbreviated from prefix/suffix) for this kind of solution. It combines several "words" into one regexp, as alternatives, and takes care of quotemeta as well. The code is shorter, and may likely become faster, too. That's the idea, though it depends on the words in the list.

$_ = 'taxial'; my %subst = ( 'taxi' => 'cab', 'cabal' => 'gang' ); use Regex::PreSuf; my $regexp = presuf(keys %subst); s/($regexp)/$subst{$1}/g; print;

Applied to your code, it becomes:

my %subst = ( %fields, searcresults => $pitem, premiumlistings => $premiumitem ); use Regex::PreSuf; my $regexp = presuf(keys %subst); $main_template =~ s/%%($regexp)%%/$subst{$1}/go;
BTW Using the /o modifier, the regexp is compiled only once, the first time a match is tried. Even though you can later change the values in the substitution hash, adding more keys will not do any good.

In reply to Re: Separating multiple keyword search input by bart
in thread Separating multiple keyword search input by Dente

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.