http://qs321.pair.com?node_id=871090


in reply to Unicode: Perl5 equivalent to Perl6's @string.graphemes?

The one thing I did that produced different results was to change the open statement to use '<:encoding(UTF-8)' for the mode instead of '<', but that transforms everything (not just the chars, but also the words) into stuff like "\x{ff11}", which does not seem useful to me.

That is the correct fix. Dumper produces Perl code, primarily for debugging purposes. When it comes to characters where encoding is likely to matter, it uses escapes to avoid mixups. As a debugging tool, it rather produce some harder to read output then producing output that looks wrong because the caller didn't properly encode the output.

If you hadn't used Dumper (just printed the string) and if you encoded your output (use open ':std', ':locale';), then you would get the actual characters.

(Each of the graphemes you posted were represented by a single character, so I didn't bother using \P{M}.)

use strict; use warnings; use open ':std', ':locale'; use Data::Dumper qw( Dumper ); my $file = do { open(my $fh, '<:encoding(UTF-8)', 'jap') or die $!; local $/; <$fh> }; print(Dumper($file)); print("[$_]") for $file =~ /(.)/sg; print("\n");
$VAR1 = "\x{6301}\x{3063}\x{3066}\x{884c}\x{304f}
";
[持][っ][て][行][く][
]