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

Re: [FWP] Less fun with meta chars



Edward M. Perry wrote:
> Ok, I know I've made this more fun than it ought to be. I'm trying to escape
> all meta characters in a string. I'm sure there is a module for this or a
> cookbook solution, but that would be no fun. I'd like to maintain a little fun.
> At the risk of being shamed by the simplicity of another solution, heres mine:
> 
>    %meta = map {($_,1)} split('', '\|()[{^$*+?.');
>    $str = join('', map {if ($meta{$_}) {"\\$_"} else {$_}} split('', $str));

how about:

$str =~ s/(.)/quotemeta $1 ne $1 ? quotemeta $1 : $1/eg;

:)
--
Steve Lane <sml@zfx.com>

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe