$text =~ s![#$%&~_}{^]!\\$&!g;
But only one character is not escaped: "%"
The sequence $% is interpreted as the Perl special variable $% (see perlvar) and its current value is interpolated. Try [#\$%&~_}{^] (escaping the $ character) instead.
Update: E.g.:
c:\@Work\Perl\monks\marek1703>perl -wMstrict -le "my $sans_esc = qr/[#$%&~_}{^]/; print $sans_esc; ;; my $with_esc = qr/[#\$%&~_}{^]/; print $with_esc; " (?^:[#0&~_}{^]) (?^:[#\$%&~_}{^])
In reply to Re^4: Escape special characters for a LaTeX file
by AnomalousMonk
in thread Escape special characters for a LaTeX file
by marek1703
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |