in reply to Faster way to regex this
I don't have time to test, but generally shoving as much work as possible into "internals" is the way to go. The existing solution has to start up the regex engine six times. It would be better to start it once, and let it look for six things. (This isn't a universal win, but in your case, I think it will be.)
$dir =~ s/([[\]{~*?])/\\$1/g;
However, it's possible you would be just as happy from a functionality standpoint, and even happier from a performance standpoint with quotemeta, or \Q...\E:
my $dir = "[]{~*?"; my $dir2 = "[]{~*?"; $dir =~ s/([[\]{~*?])/\\$1/g; print "$dir\n"; print quotemeta($dir2), "\n";
As you can see, for these characters, they are functionally equivalent. However, quotemeta will also escape other "special" characters, so you should first familiarize yourself with how it behaves for your use case. quotemeta's advantage is that it doesn't invoke the regexp engine, which is a substantial apparatus.
Dave
|
|---|