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

Re: [FWP] Less fun with meta chars



[Fri, Aug 27, 1999 at 01:37:02PM -0500] Edward M. Perry:
> 
> 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));

well, quotemeta does come to mind... ;-)

#!/usr/bin/perl -w

%meta = map {($_,1)} split('', '\|()[{^$*+?.');

while ( <DATA> ) {
    chomp;

    my $ed = join('', map {if ($meta{$_}) {"\\$_"} else {$_}} split(''));
    my $mb = quotemeta;

    print "$ed:$mb\n";
}
__DATA__
Matthew Bafford
The 1st of June
#!/usr/bin/perl
f@b.de;rm -rf /
^D
Matthew Bafford:Matthew\ Bafford
The 1st of June:The\ 1st\ of\ June
#!/usr/bin/perl:\#\!\/usr\/bin\/perl
f@b\.de;rm -rf /:f\@b\.de\;rm\ \-rf\ \/

> Ed Perry

--Matthew

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