This is a bit tricky because there are a variety of escape
types to handle. In particular, there are the "regular" codes
like '\n' which translate into a single character, but there
are also others.
- '\cX' is the equivalent of CTRL-X, where X is a letter
from a to z, or also a few others in the nearby ASCII space
(such as ESC).
- '\xNN' is the ASCII character hexidecimal 0xNN.
- '\0nnn' is the ASCII character octal nnn.
You could build a regex to handle these, perhaps, such as:
my %escape = map { ( chr($_) => eval '"\\'.chr($_).'"'||undef ) } 0 ..
+ 127;
sub interpolate
{
my ($s) = @_;
$s =~ s/\\c(.)/chr(ord(lc($1))-ord('`'))/ge;
$s =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/ge;
$s =~ s/\\0([0-9]{1,3})/chr(oct($1))/ge;
$s =~ s/\\0/\0/g;
$s =~ s/\\(.)/$escape{$1}/gs;
return $s;
}