| 1 | package usww; | ||
| 2 | use 5.012005; | ||
| 3 | |||
| 4 | our $VERSION = "0.06"; | ||
| 5 | |||
| 6 | use Encode qw(is_utf8 encode_utf8 decode_utf8); | ||
| 7 | use utf8(); | ||
| 8 | use strict(); | ||
| 9 | use warnings(); | ||
| 10 | use List::Util qw(first); | ||
| 11 | |||
| 12 | sub import { | ||
| 13 | warn "it seems this is NOT Windows" unless $^O eq "MSWin32"; | ||
| 14 | utf8->import; | ||
| 15 | strict->import; | ||
| 16 | warnings->import( 'all', FATAL => 'recursion' ); | ||
| 17 | |||
| 18 | my $cp = eval { require Win32; return Win32::GetConsoleCP() } | ||
| 19 | or die "install 'Win32' module before use it"; | ||
| 20 | my $encoding = $@ ? 'UTF-8' : "cp$cp"; | ||
| 21 | $| = 1; # is this irrelevant? | ||
| 22 | #binmode \*STDIN, ":encoding($encoding)"; | ||
| 23 | binmode \*STDOUT, ":encoding($encoding)"; | ||
| 24 | binmode \*STDERR, ":encoding($encoding)"; | ||
| 25 | |||
| 26 | $SIG{__WARN__} = \&_redecode; | ||
| 27 | $SIG{__DIE__} = sub { die _redecode(@_) }; | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _redecode { | ||
| 32 | $_[0] =~ /^(.+) at (.+) line (\d+)\.$/; | ||
| 33 | my @texts = split $2, $_[0]; | ||
| 34 | return is_utf8($1) | ||
| 35 | ? $texts[0] . decode_utf8 $2. $texts[1] | ||
| 36 | : decode_utf8 $_[0]; | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | __END__ |