$ perl -MEncode=encode,from_to -wle'$s = encode("UTF-8", chr(0x2660)); from_to($s, "UTF-8", "ascii", Encode::FB_XMLCREF); print $s' | od -c
0000000 342 231 240 \n
0000004
####
$ perl -MEncode=encode,from_to -wle'$s = encode("UTF-8", chr(0x2660)); from_to($s, "UTF-8", "ascii", Encode::FB_XMLCREF); print $s' | od -c
0000000 & # x 2 6 6 0 ; \n
0000011
####
1..1
not ok 1 - With XML/HTML 4 digit hex entity
# Failed test 'With XML/HTML 4 digit hex entity'
# at a.pl line 24.
# got: '\u2660\u2660'
# expected: '♠\u2660'
# Looks like you failed 1 test of 1.
####
#!/usr/bin/perl -T
use strict;
use warnings;
# 2.13 has a necessary bug fix in from_to
use Encode 2.13 qw( encode );
my @tests;
BEGIN {
@tests = (
[ encode('UTF-8', "♠\x{2660}"), "♠\\u2660", 'With XML/HTML 4 digit hex entity' ],
);
}
use Test::More tests => 0+@tests;
sub escape_with_encode {
return $_[0] unless $_[0] =~ /[\x80-\xff]/;
my $s = shift;
Encode::from_to($s, "utf-8", "ascii", Encode::FB_XMLCREF);
$s =~ s/(....);/\\u$1/g;
return $s;
}
is( escape_with_encode($_->[0]), $_->[1], $_->[2] ) for @tests;