Take a look at the quotemeta operator, which escapes lots of things includeing $ and @. You might also try the regex s/([\$\@])/\\$1/g which will only escape $ and @, as in:
#!/usr/bin/perl -wT
use strict;
my $txt = '$a @b $c %d';
my $out1 = quotemeta($txt);
(my $out2 = $txt) =~ s/([\$\@])/\\$1/g;
print "txt = $txt\n";
print "out1 = $out1\n";
print "out2 = $out2\n";
=OUTPUT
txt = $a @b $c %d
out1 = \$a\ \@b\ \$c\ \%d
out2 = \$a \@b \$c %d
-Blake | [reply] [d/l] [select] |
You need to post code, too. In most circumstances, once $ and @ are safely tucked away in a string variable you don't need to worry about escaping them. If, on the other hand, you
- use them in a regex,
- eval them, or
- pass them to the shell through exec, system, or backticks,
you may need to escape them. What you need to escape depends on what is to evaluate them and what your intentions are.
Update: Clarified language introducing the list of exceptions.
After Compline, Zaxo
| [reply] [d/l] [select] |
It's pretty rare that you'd need that level of escaping. What's using the result such that $ and @ are dangerous characters? | [reply] |