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

RE: [FWP] Q on Substitution improvement



The "reverse" will put longer keys before shorter ones with the same root.
Otherwise, if you had a substitute for both "can" and "candy", Perl would
find the "can" first and make the substitution before seeing that "candy"
matches.  The "reverse" puts the "candy" before "can" in the search string
so "can" will only match if "candy" failed.  It may not be strictly
necessary in the HTML case, but it might be.  And it's also good to keep it
for the general solution, especially since Perl code tends to get copied
frequently.

 - Andy Jacobs

-----Original Message-----

Randal L. Schwartz writes:
|    $map{'"'} = """;
|    $map{"\n"} = "<br>";
|    ...
|    my $pat = "(" . join("|", map quotemeta $_, reverse sort keys %map),
")";
|    eval q{$pat = qr/$pat/} if $] >= 5.005;
|    s/$pat/$map{$1}/g;

Maybe it's way too early in the morning, but why "reverse sort keys %map"
and not just "keys %map"?

Also, does the qr/$pat/ do anything useful in this case? s/// would have to
do it anyway, and being there's only one substitution, it seems it would
still happen only once without the eval. If there were multiple s/// with
$pat as the expression to match I can see a distinct advantage, but not
here.

But then, it's really early...
Brian

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


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