In Perl, if I run the code:
print "Literal Hex: \x{50} \n";
I get this: "Literal Hex: P
"
However, if I run the code:
my $hex_num = 50;
print "Interpolated Hex: \x{$hex_num}";
The variable does not interpolate properly and I get this: "Interpolated Hex:
"
Similar failure results when I attempt to use variable interpolation in unicode and octal escape sequences.
Is it possible to use escape sequences (e.g. \x, \N) with interpolated string variables? I was under the impression that a $variable contained within double quotes is always interpolated, but is this the exception?
Note: Thanks to this question, I am aware of the workaround: chr(hex($hex_num))
, but my above questions regarding variable interpolation for escape sequences still stand.
Interpolation is not recursive, everything is interpolated just once, from left to right. Therefore, when \x{$hex}
is being processed, the following applies (cited from perlop):
If there are no valid digits between the braces, the generated character is the NULL character ("\x{00}").
Zero is really there:
perl -MO=Deparse -e '$h=50;print "<\x{$h}>"'
$h = 50;
print "<\000>";
-e syntax OK
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments