#!/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/&#x(....);/\\u$1/g; return $s; } is( escape_with_encode($_->[0]), $_->[1], $_->[2] ) for @tests;