perl-unicode

Re: \x (backslash x) weirdness in perl 5.6.0

2001-01-15 09:07:22
On Mon, 15 Jan 2001, James wrote:

Folks:

Just experimenting with the \x notation and found
some surprises.

1) Seems like print "\x{0041}"; works as expected (gives 'A'), but 
"\x{$s}", where $s is the string hex value '0041', first
gives the warning below then a blank value.

Any ideas what I'm doing wrong?

You're expecting \x to interpolate variables. That's not what it does.
Neither did \123 (octal) or \x12 (hex) formats. If you want variables, you
probably want pack "U".

2) Looks like "\x{0041}" is understood by the parser, but
not the bare \x{0041} (error) or '\x{0041}' (not
interpolated into a unicode value. Comments?

Why does this surprise you? This is exactly the same behaviour as with the
other backslash escapes. For example, you can't use \n in the middle of
your source as a newline (it's a reference to the bareword 'n'), nor will
'\n' represent a newline. You have to use double quotes: "\n" (or qq|\n|,
etc.). That's that double quotes do. \x{} is no exception.

RTFM. In this case, for example, `perldoc perlop` is a good place to
start, specifically the section on "Quote and Quote-like Operators" and
later on on "Interpolation".

Cheers,
Philip
-- 
Philip Newton <newton(_at_)newton(_dot_)digitalspace(_dot_)net>

<Prev in Thread] Current Thread [Next in Thread>