package I18N::Japanese; use Encode 'encode'; use Symbol 'qualify_to_ref'; # eventually, determine this dynamically my $encoding = "cp932"; # this is meant to take effect for whoever uses us require encoding; encoding->import($encoding); # override/wrap Perl built-ins that take or return filenames # (... snippage of all but open()-wrapper) *CORE::GLOBAL::open = sub (*@) { my $fhref = \$_[0]; my $autov = !defined $_[0]; my $fh = qualify_to_ref(shift, scalar caller); # pass filehandle up to caller when "open my $f, ..." $$fhref = $fh if $autov; my ($arg2, $arg3, @args) = convert_encoding(@_); # need to handle the different prototypes seperately if (@_ >= 3) { CORE::open $fh, $arg2, $arg3, @args; } elsif (@_ == 2) { if (defined $arg3) { CORE::open $fh, $arg2, $arg3; } else { # must be undef _syntactically_ CORE::open $fh, $arg2, undef; } } elsif (@_ == 1) { CORE::open $fh, $arg2; } else { CORE::open $fh; } }; sub convert_encoding { return ( map ref(\$_) eq 'SCALAR' ? encode($encoding, $_) : $_, @_ ); } 1;