In Perl, why does variable interpolation fail for a hexadecimal escape sequence?

schulwitz

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.

choroba

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does angularjs interpolation fail in IE 11?

From Dev

Regex: Illegal hexadecimal escape sequence

From Dev

Why does make ignore escape sequence if the output is piped

From Dev

Limit the Length of a Hexadecimal Escape Sequence in a C-String

From Dev

Unrecognized Escape Sequence C# 6 String Interpolation

From Dev

Why does innerjoin fail when table contains a nested table in a variable

From Dev

Why does innerjoin fail when table contains a nested table in a variable

From Dev

Perl regex for 8 digit hexadecimal number (may be missing escape characters)

From Dev

Why does String.Format fail to escape characters when the string is in a resource?

From Dev

Why does String.Format fail to escape characters when the string is in a resource?

From Dev

PHP variable Interpolation, why it works

From Dev

PHP variable Interpolation, why it works

From Dev

Why does this return fail?

From Dev

Why does vectorization fail?

From Dev

Why does this Calendar fail?

From Dev

Why does this macrodef fail?

From Dev

JavaScript "If" - Why does it not fail?

From Dev

why does mmap fail?

From Dev

Why does this fail to compile?

From Dev

why does this script fail?

From Dev

Why does this macrodef fail?

From Dev

JavaScript "If" - Why does it not fail?

From Dev

Why does this parser always fail when the end-of-line sequence is CRLF?

From Dev

Octave's 'system' does not interpret a escape sequence

From Dev

What does this bash escape sequence mean?

From Dev

What does this bash escape sequence mean?

From Dev

Octave's 'system' does not interpret a escape sequence

From Dev

Why does Windows API function createDirectoryW fail when used from a Perl script?

From Dev

Why does this workaround for magic Perl internal variable work?

Related Related

  1. 1

    Why does angularjs interpolation fail in IE 11?

  2. 2

    Regex: Illegal hexadecimal escape sequence

  3. 3

    Why does make ignore escape sequence if the output is piped

  4. 4

    Limit the Length of a Hexadecimal Escape Sequence in a C-String

  5. 5

    Unrecognized Escape Sequence C# 6 String Interpolation

  6. 6

    Why does innerjoin fail when table contains a nested table in a variable

  7. 7

    Why does innerjoin fail when table contains a nested table in a variable

  8. 8

    Perl regex for 8 digit hexadecimal number (may be missing escape characters)

  9. 9

    Why does String.Format fail to escape characters when the string is in a resource?

  10. 10

    Why does String.Format fail to escape characters when the string is in a resource?

  11. 11

    PHP variable Interpolation, why it works

  12. 12

    PHP variable Interpolation, why it works

  13. 13

    Why does this return fail?

  14. 14

    Why does vectorization fail?

  15. 15

    Why does this Calendar fail?

  16. 16

    Why does this macrodef fail?

  17. 17

    JavaScript "If" - Why does it not fail?

  18. 18

    why does mmap fail?

  19. 19

    Why does this fail to compile?

  20. 20

    why does this script fail?

  21. 21

    Why does this macrodef fail?

  22. 22

    JavaScript "If" - Why does it not fail?

  23. 23

    Why does this parser always fail when the end-of-line sequence is CRLF?

  24. 24

    Octave's 'system' does not interpret a escape sequence

  25. 25

    What does this bash escape sequence mean?

  26. 26

    What does this bash escape sequence mean?

  27. 27

    Octave's 'system' does not interpret a escape sequence

  28. 28

    Why does Windows API function createDirectoryW fail when used from a Perl script?

  29. 29

    Why does this workaround for magic Perl internal variable work?

HotTag

Archive