karlberry has asked for the wisdom of the Perl Monks concerning the following question:

These lines:

use Encode;
$str = decode_utf8("ab\x{cc}");
$str =~ s/(\P{PosixPrint})/sprintf("U+%04X", $1)/eg;
print "$str\n";

output abU+0000. Why does the Unicode character comes out as zero in the back reference? I get the same zero result with other Posix* properties, with \p as well as \P (mutatis mutandis), etc.

I also tried without the decode_utf8; no difference. (Sorry, I've never understood when it's needed and when it's not, so I just tried both ways.)

Also no difference with use feature 'unicode_strings'; (which I don't want in my real program, anyway).

I know there are other ways to accomplish the job being done here, of converting binary Unicode characters into ASCII representations, e.g., encode("ascii", decode_utf8($str), Encode::FB_XMLCREF), the nice_string function from perluniintro, etc. I'm wondering here about the regexp usage in particular.

This is with perl 5.30.2 on CentOS Linux; I built the binary from the original source, no distro packaging involved.

I feel sure I'm missing something obvious, but I can't find the answer in either the myriad perluni* docs or on the net. Apologies and thanks in advance.

Replies are listed 'Best First'.
Re: \P{PosixPrint} back reference?
by hippo (Archbishop) on Dec 21, 2020 at 18:49 UTC

    I guess you haven't used warnings. If you do, then a helpful warning is displayed. viz:

    $ cat posix.pl #!/usr/bin/env perl use strict; use warnings; my $str = "ab\x{cc}"; $str =~ s/(\P{PosixPrint})/sprintf("U+%04X", $1)/eg; print "$str\n"; $ ./posix.pl Argument "Ì" isn't numeric in sprintf at ./posix.pl line 6. abU+0000 $

    So the warning indicates that a numeric argument is expected but that you are providing a non-numeric value. We can fix this with ord:

    #!/usr/bin/env perl use strict; use warnings; my $str = "ab\x{cc}"; $str =~ s/(\P{PosixPrint})/sprintf("U+%04X", ord $1)/eg; print "$str\n";

    The moral: Always use strict and warnings.


    🦛