[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Q on Substitution improvement



Walter Torres wrote:
> # ---------------------------------------------------------
> # We are going to run through each name/value pair sent up and
> # parse it to replace any RETURN (cr/lf) with <br>, the HTML line
> # break code and quotes with '&quot;'.
> for (keys %cgiNameValues)
> {
>    $cgiNameValues{$_} =~ s/\n/<br>/g;      # change RETURNS to <br>
>    $cgiNameValues{$_} =~ s/\"/\&quot;/g;   # Change quotes to HTML code
> }

A common problem...

> Isn't there a Perl-ish method to run down my 50+ hash and do the swap?

Yes, there is:

%Subst = ("\n" => '<br>', 
          '"' => '&quot;',
          # etc...
         );

$SubstRE = join('|', keys %Subst);

for (keys %cgiNameValues) {
  $cgiNameValues{$_} =~ s/($SubstRE)/$Subst{$1}/ego;
}

Or, just take a look at HTML::Entities encode function (I think it's 
part of HTML::Parser).

Hope this helps!

[BTW, where is the `Fun'? IMHO this problem belongs 
 more in clp.moderated than in FWP...]

Roland
--
Roland.Giersig@alcatel.at         Phone: +43-1-27722-3755
ALCATEL Austria, Dept. RTPM       FAX:   +43-1-27722-3955
Scheydgasse 41, A-1210 WIEN, Austria (no Kangaroos here!)

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org