3

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.

2
  • Consider: my $n = 'n'; print "\$n"; It prints "$n", not a newline. The string is only going to be interpolated once. Commented Aug 2, 2013 at 6:45
  • Had to read the whole thing just to get to the workaround of... print(chr(hex($hex_num))); Commented Apr 12, 2016 at 21:41

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you, answered perfectly! I don't know if I ever would have found this on my own.
1

You should put in your variable the complete scape sequence:

my $hex_num = "\x50"; 
print "Interpolated Hex: $hex_num", "\n";

3 Comments

I appreciate your answer but it doesn't get at the fundamental question I'm trying to answer here, which is why does "\x{$hex_num}" fail? Your answer relies on using a string literal, which works in one instance, but is not very practical for cycling $hex_num through every hex character from "\x001" to "\x501".
short answer: because there is not a double (2-phases) interpolation. There is only a single step
Interesting alternative, did not think of this. Wouldn't fit my case, as I wanted a for-loop of integers and their sequences, but may be useful for someone else.
1

The issue I had was adding an escaped var into another variable such as:

$MYVAR = "20";
$myQuery = "\x02\x12\x10\x$MYVAR\x10";

Tried a number of \\x, \Q\x and various other escape sequences to no avail!!!

My workaround was not a direct escape but converting the var prior to adding to the string.

$MYVAR = chr(hex(20));

Did quite a bit of searching for a direct regex solution but had to run with this in the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.