Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Wrap CORE::readdir to provide UTF-8 filenames

readdir returns filenames as bytes, but that is
inconvenient. Now, we wrap readdir and decode the
returned filenames so the user is presented with
the UTF-8 filenames they expected.

Fixes GH #11
  • Loading branch information...
commit e3de34f51ebcd67244a2dbd0728c250600b9b6b0 1 parent 7978a07
@doherty authored
View
1  Changes
@@ -4,6 +4,7 @@ Revision history for Perl module {{$dist->name}}
* Enable unicode_strings (see perldoc feature) [GH #2]
* Enable unicode_eval (see perldoc feature) [GH #2]
* Enable fc (see perldoc fc) [GH #2]
+ * Wrap CORE::readdir to provide UTF-8 filenames [GH #11]
0.007 2012-08-01
* Use version.pm for comparing versions in the test suite
View
0  corpus/みかちゃん
No changes.
View
1  dist.ini
@@ -7,6 +7,7 @@ copyright_year = 2009
[@Author::DOHERTY]
has_version = 0
+disable_tests = Test::Portability
[Conflicts]
autodie = 2.11
View
13 lib/utf8/all.pm
@@ -51,6 +51,12 @@ sub import {
'feature'->import::into($target, qw{unicode_strings}) if $^V >= v5.11.0;
'feature'->import::into($target, qw{unicode_eval fc}) if $^V >= v5.16.0;
+ {
+ no strict qw(refs); ## no critic (TestingAndDebugging::ProhibitNoStrict)
+ no warnings qw(redefine);
+ *{$target . '::readdir'} = \&_utf8_readdir;
+ }
+
# utf8 in @ARGV
state $have_encoded_argv = 0;
_encode_argv() unless $have_encoded_argv++;
@@ -65,6 +71,13 @@ sub _encode_argv {
return;
}
+sub _utf8_readdir(*) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
+ my $handle = shift;
+ my @files = readdir($handle);
+ $_ = Encode::decode('UTF-8', $_) for @files;
+ return @files;
+}
+
=head1 INTERACTION WITH AUTODIE
If you use L<autodie>, which is a great idea, you need to use at least version
View
41 t/autodie.t
@@ -1,10 +1,45 @@
use strict;
use warnings;
use version 0.77;
-use Test::More tests => 1;
+use Test::More 0.96;
+use Test::Fatal;
if ( eval { require autodie; 1 } ) {
- diag "autodie is version $autodie::VERSION, but must be greater than version 2.11"
+ plan skip_all => "autodie is version $autodie::VERSION, but must be greater than version 2.11"
if (version->parse($autodie::VERSION) < version->parse(2.11));
+ plan tests => 2;
}
-pass;
+else {
+ plan skip_all => "autodie is required for these tests";
+}
+no autodie;
+
+subtest 'autodie first' => sub {
+ plan tests => 2;
+ use autodie;
+ use utf8::all;
+
+ opendir my $dh, 'corpus';
+ my @files = grep { !m{^\.} } readdir $dh;
+ closedir $dh;
+
+ is_deeply \@files, ["\x{307f}\x{304b}\x{3061}\x{3083}\x{3093}", "testfile"];
+
+ my $exception = exception { opendir my $no_dh, 'nonexistent' };
+ like $exception => qr/No such file or directory/;
+};
+
+subtest 'autodie last' => sub {
+ plan tests => 2;
+ use utf8::all;
+ use autodie;
+
+ opendir my $dh, 'corpus';
+ my @files = grep { !m{^\.} } readdir $dh;
+ closedir $dh;
+
+ is_deeply \@files, ["\x{307f}\x{304b}\x{3061}\x{3083}\x{3093}", "testfile"];
+
+ my $exception = exception { opendir my $no_dh, 'nonexistent' };
+ like $exception, qr/No such file or directory/;
+};
View
19 t/readdir.t
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+opendir my $dh, 'corpus'
+ or die "Couldn't open directory 'corpus'";
+
+my @files = grep { $_ ne '.' and $_ ne '..' } readdir $dh;
+my @utf8_files;
+{
+ rewinddir $dh;
+ use utf8::all;
+ @utf8_files = grep { $_ ne '.' and $_ ne '..' } readdir $dh;
+}
+closedir $dh;
+
+is_deeply \@utf8_files, ["\x{307f}\x{304b}\x{3061}\x{3083}\x{3093}", "testfile"];
+isnt $files[0] => $utf8_files[0];
+is $files[1] => $utf8_files[1];
Please sign in to comment.
Something went wrong with that request. Please try again.