Created
August 5, 2011 18:01
-
-
Save mix3/1128123 to your computer and use it in GitHub Desktop.
show_replace_hash.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Encode; | |
my $sample = { | |
test1 => { | |
test2 => { | |
test3 => encode_utf8('あいうえお'), | |
test4 => encode_utf8('かきくけこ'), | |
}, | |
}, | |
test5 => { | |
test6 => { | |
test7 => encode_utf8('さしすせそ'), | |
test8 => encode_utf8('たちつてと'), | |
}, | |
}, | |
test9 => [ | |
encode_utf8('なにぬねの'), | |
encode_utf8('はひふへほ'), | |
], | |
test10 => [ | |
{ | |
test11 => encode_utf8('まみむめも'), | |
}, | |
{ | |
test12 => encode_utf8('やゆよ'), | |
test13 => encode_utf8('らりるれろ'), | |
}, | |
], | |
}; | |
print "#--------------------------------\n"; | |
print "# utf8\n"; | |
print "#\n"; | |
&ushow(\$sample); | |
&u2s(\$sample); | |
print "#--------------------------------\n"; | |
print "# cp932\n"; | |
print "#\n"; | |
&sshow(\$sample); | |
&s2u(\$sample); | |
print "#--------------------------------\n"; | |
print "# utf8\n"; | |
print "#\n"; | |
&ushow(\$sample); | |
sub base { | |
my $arg = shift; | |
my $fun = shift; | |
my $dep = shift || 0; | |
if (!$dep) { | |
$arg = \{var => $$arg}; | |
} | |
if (ref($$arg) eq 'HASH') { | |
for (keys %$$arg) { | |
if (ref($$arg->{$_}) eq 'HASH') { | |
&base(\$$arg->{$_}, $fun, $dep + 1); | |
} elsif (ref($$arg->{$_}) eq 'ARRAY') { | |
&base(\$$arg->{$_}, $fun, $dep + 1); | |
} elsif (ref($$arg->{$_}) eq '') { | |
&$fun(\$$arg->{$_}); | |
} | |
} | |
} elsif (ref($$arg) eq 'ARRAY') { | |
for (@$$arg) { | |
if (ref($_) eq 'HASH') { | |
&base(\$_, $fun, $dep + 1); | |
} elsif (ref($_) eq 'ARRAY') { | |
&base(\$_, $fun, $dep + 1); | |
} elsif (ref($_) eq '') { | |
&$fun(\$_); | |
} | |
} | |
} else { | |
&$fun($arg); | |
} | |
} | |
sub u2s { | |
my $arg = shift; | |
&base($arg, sub{ | |
my $arg = shift; | |
$$arg = encode('cp932', decode_utf8($$arg)); | |
}); | |
} | |
sub s2u { | |
my $arg = shift; | |
&base($arg, sub{ | |
my $arg = shift; | |
$$arg = encode_utf8(decode('cp932', $$arg)); | |
}); | |
} | |
sub sshow { | |
my $arg = shift; | |
&base($arg, sub{ | |
my $arg = shift; | |
print encode_utf8(decode('cp932', $$arg))."\n"; | |
}); | |
} | |
sub ushow { | |
my $arg = shift; | |
&base($arg, sub{ | |
my $arg = shift; | |
print $$arg."\n"; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ perl show_replace_hash.pl