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


in reply to Re: Convert JSON to Perl and back with unicode
in thread Convert JSON to Perl and back with unicode

Nice and easy! But, it's prone to Unicode injection:
#! /usr/bin/perl use warnings; use strict; use Data::Dumper; my %mydatatoshow = ( "\N{GREEK SMALL LETTER ALPHA}" => "\\\N{GREEK SMALL LETTER BETA}", '\\x{3b1}' => '\\\\x{3b2}', ); binmode STDERR, ':encoding(UTF-8)'; print STDERR Dumper(\%mydatatoshow); print STDERR Dumper(\%mydatatoshow) =~ s/\\x\{([0-9a-f]{2,})\}/chr(hex +($1))/ger;

Update: Possibly fixable by

s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 : $1/ger;

Verified by

#! /usr/bin/perl use warnings; use strict; use Data::Dumper; my %datatoshow = ( "\N{GREEK SMALL LETTER ALPHA}" => "\\\N{GREEK SMALL LETTER BETA}", '\\x{3b1}' => '\\\\x{3b2}', ); binmode STDERR, ':encoding(UTF-8)'; print STDERR Dumper(\%datatoshow); print STDERR Dumper(\%datatoshow) =~ s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 : $1/ger; use Test::More tests => 3; my $dumper_orig = Dumper(\%datatoshow); my $dumper_leszekdubiel = $dumper_orig =~ s/\\x\{([0-9a-f]{2,})\}/chr(hex($1))/ger; my $dumper_choroba = $dumper_orig =~ s/((\\+)x\{([0-9a-f]{2,})\})/ (length($2) % 2) ? substr($2, 1) . chr hex $3 +: $1/ger; my $VAR1; eval $dumper_orig; is_deeply $VAR1, \%datatoshow; eval $dumper_leszekdubiel; is_deeply $VAR1, \%datatoshow; eval $dumper_choroba; is_deeply $VAR1, \%datatoshow;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]