Is there a difference between \x{..} and \N{U+..}

Viewed 138

Is there a difference in using either \x{..} or \N{U+..} in a print statement? To me they look like 2 ways of doing the same thing. So what is the difference - if there is any?

1 Answers

"\x{...}" and "\N{U+...}" always produce exactly the same string, meaning "\x{...}" eq "\N{U+...}" will always be true (for the same number).

However, since 5.12, the scalar created by "\N{U+...}" is guaranteed to use the UTF8=1 internal storage format[1], while the scalar created by "\x{...}" is not subject to any guarantee (and in fact uses the UTF8=0 format when possible). This means that utf8::upgrade needs not be called on a scalar produced by "\N{U+...}" before passing it to (builtin or XS) functions that suffer from The Unicode Bug.[2]

In short, "\N{U+...}" is more likely to work correctly when dealing with Unicode Code Points, and "\x{...}" is more likely to work correctly when dealing with bytes.


  1. The UTF8=0 storage format support strings of 8-bit characters, while the UTF8=1 storage format support strings of 32- or 64-bit characters (depending on the build).

  2. This refers to code that considers two identical strings to be different because one is stored using UTF8=0 format, and one is stored using the UTF8=1 format). Code that assigns semantics to the storage format is buggy because Perl can and does freely convert strings between the two formats.

Related