We had to use two backslashes in the replacement because the
replacement section of a substitution is read as a double-quoted
string, and to get one backslash, you need to write two. Here's a
similar example for VMS DCL, where you need to double every quote to
get one through:
Because we're using character classes in the regular expressions, we
can use - to define a range and
^ at the start to negate. This escapes all
characters that aren't in the range A through Z.
$string =~ s/([^A-Z])/\\$1/g;
In practice, you wouldn't want to do that, since it would pick up a
lowercase "a" and turn it into
"\a", for example, which is ASCII BEL character.
(Usually when you mean non-alphabetic characters,
\PL works better.)