1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
package Path::Class::Unicode;
use strict;
use 5.008_001;
our $VERSION = '0.08';
use Exporter::Lite;
our @EXPORT = qw( ufile udir ufile_from_uri udir_from_uri );
use Encode ();
use Path::Class;
use URI::file;
use Scalar::Util qw(blessed);
sub ufile {
__PACKAGE__->new(file(@_));
}
sub udir {
__PACKAGE__->new(dir(@_));
}
sub Path::Class::File::ufile {
my $file = shift;
ufile(_decode_filename($file), @_);
}
sub Path::Class::Dir::udir {
my $dir = shift;
udir(_decode_filename($dir), @_);
}
sub ufile_from_uri {
my $uri = shift;
$uri = URI->new($uri) unless blessed $uri;
if ($^O eq "MSWin32") {
ufile(Encode::decode_utf8($uri->file('win32')));
} else {
ufile(Encode::decode_utf8($uri->file('unix')));
}
}
sub udir_from_uri {
my $uri = shift;
$uri = URI->new($uri) unless blessed $uri;
if ($^O eq "MSWin32") {
udir(Encode::decode_utf8($uri->file('win32')));
} else {
udir(Encode::decode_utf8($uri->file('unix')));
}
}
sub new {
my($class, $path) = @_;
bless { path => $path }, $class;
}
sub uri {
my $self = shift;
my $path = Encode::encode_utf8($self->{path}->stringify);
$path =~ tr!\\!/! if $^O eq "MSWin32";
if ($self->is_absolute) {
return URI->new("file://$path");
} else {
return URI->new("file:$path");
}
}
our $encoding;
sub init_encoding {
unless ($encoding) {
$encoding = 'utf-8';
if ($^O eq 'MSWin32') {
eval {
require Win32::API;
Win32::API->Import('kernel32', 'UINT GetACP()');
$encoding = 'cp'.GetACP();
};
}
}
}
sub stringify {
my $self = shift;
init_encoding();
Encode::encode($encoding, $self->{path}->stringify);
}
sub _decode_filename {
init_encoding();
my $filename = shift;
Encode::decode($encoding, "$filename");
}
sub open {
my $self = shift;
my $class = $self->is_dir ? "IO::Dir" : "IO::File";
$class->new($self->stringify, @_);
}
sub next {
my $self = shift;
$self->{path}->{dh} = $self->open unless $self->{path}->{dh};
local *IO::Dir::read = sub { Encode::decode $encoding, readdir shift };
ufile($self->{path}->next);
}
sub children {
my ($self, %opts) = @_;
$self->{path}->{dh} = $self->open unless $self->{path}->{dh};
my @out;
while (my $entry = $self->{path}->{dh}->read) {
# XXX What's the right cross-platform way to do this?
next if (!$opts{all} && ($entry eq '.' || $entry eq '..'));
push @out, $self->file($entry);
$out[-1] = $self->subdir($entry) if -d $out[-1];
}
return @out;
}
use overload (
q[""] => 'stringify',
fallback => 1,
);
our $AUTOLOAD;
sub AUTOLOAD {
my $self = shift;
(my $method = $AUTOLOAD) =~ s/.*:://;
$self->{path}->$method(@_);
}
sub DESTROY { }
1;
__END__
=encoding utf-8
=for stopwords TODO UTF-8 filenames cp932 mattn
=head1 NAME
Path::Class::Unicode - Maps Unicode filenames to local encoding and code pages
=head1 SYNOPSIS
use Path::Class::Unicode;
# Use ufile() to create Unicode objects
my $fn = "\x{55ed}.txt";
my $file = ufile("path", $fn);
my $fh = $file->open;
my $fn = "\x{55ed}.txt";
my $file = ufile("/path", $fn);
my $uri = $file->uri; # file:///path/%E5%97%AD.txt (always utf-8)
my $fh = ufile_from_uri($uri)->open;
=head1 DESCRIPTION
Path::Class::Unicode is a Path::Class extension to handle Unicode
file names by mapping them to local encodings when stringified. It maps
to UTF-8 for all UNIX systems including Mac OS X and uses Windows code
page (like cp932 for Japanese) in Win32 systems.
It's very useful if you store file paths using URI representation like
L<file://> and uses URI escaped UTF-8 characters for non-ASCII
characters. See L<Path::Class::URI> for details.
=head1 TODO
It would be nice if we could proxy filehandles using Win32API::File.
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@cpan.orgE<gt>
mattn
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
=cut
|