Skip to content

Commit

Permalink
Now always using :utf8_strict
Browse files Browse the repository at this point in the history
  • Loading branch information
HayoBaan committed Aug 11, 2016
1 parent 6bccafd commit a083fd5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
45 changes: 13 additions & 32 deletions lib/utf8/all.pm
Expand Up @@ -87,21 +87,11 @@ C<STDERR> file handles is always global!
=head2 UTF-8 Errors
By default, C<utf8::all> will handle invalid code points (i.e.,
utf-8 that does not map to a valid unicode "character"), as a fatal
error.
Note: On Perl < v5.24.0 a bug in handling the I/O encoding layers in
combination with threads (and, on Windows, forks) causes a
segmentation fault. To prevent this, C<utf8::all> will use the
non-strict C<:utf8> instead of C<:encoding(UTF-8)> for I/O in case of
a thread enabled Perl < 5.24.0. If threads are not enabled for your
Perl version, or if you are using a version >= v5.24.0, C<utf8::all>
will use the strict (and recommended) C<:encoding(UTF-8)> I/O layer.
Note: For C<glob>, C<readdir>, and C<readlink>, one can decide how
decoding errors are handled by setting the attribute
L</"$utf8::all::UTF8_CHECK">.
C<utf8::all> will handle invalid code points (i.e., utf-8 that does
not map to a valid unicode "character"), as a fatal error.
For C<glob>, C<readdir>, and C<readlink>, one can change this
behaviour by setting the attribute L</"$utf8::all::UTF8_CHECK">.
=head1 COMPATIBILITY
Expand Down Expand Up @@ -153,6 +143,9 @@ C<readdir>, C<readlink>.
=cut

use Encode ();
use PerlIO::utf8_strict;

our $UTF8_CHECK = Encode::FB_CROAK; # Die on encoding errors

# UTF-8 Encoding object
Expand All @@ -164,18 +157,15 @@ sub import {
# Enable features/pragmas in calling package
my $target = caller;

my $utf8_IO_encoding = $class->_choose_utf8_IO_encoding;

'utf8'->import::into($target);
'open'->import::into($target => "IO" => $utf8_IO_encoding);
'open'->import::into($target, 'IO' => ':utf8_strict');

# use open ':std' only works with some encodings.
state $have_encoded_std = 0;
if( !$have_encoded_std ) {
binmode STDERR, $utf8_IO_encoding;
binmode STDOUT, $utf8_IO_encoding;
binmode STDIN, $utf8_IO_encoding;
$have_encoded_std = 1;
if (!$have_encoded_std++) {
binmode STDERR, ':utf8_strict';
binmode STDOUT, ':utf8_strict';
binmode STDIN, ':utf8_strict';
}

'charnames'->import::into($target, qw{:full :short});
Expand Down Expand Up @@ -277,15 +267,6 @@ sub _utf8_glob {
}
}

sub _choose_utf8_IO_encoding {
# Perl >= 5.24.0 or no threads? No problem.
return ':encoding(UTF-8)' if $^V >= v5.24.0 || (!$Config{usethreads} && !$Config{useithreads});

# A safe default.
require PerlIO::utf8_strict;
return ':utf8_strict';
}

=head1 INTERACTION WITH AUTODIE
If you use L<autodie>, which is a great idea, you need to use at least
Expand Down
4 changes: 2 additions & 2 deletions t/utf8.t
Expand Up @@ -37,10 +37,10 @@ use Test::More;
# If we have the Perl Unicode flag set that adds the UTF-8 layer,
# we need to skip this test.
skip 'Perl Unicode flag set that always adds UTF-8 layer to output', 1 if (${^UNICODE} & 16);
ok( !grep(/utf8/, @layers), q{utf8 doesn't appear in perlio layers})
ok(!grep(/utf8/, @layers), q{utf8 does not appear in perlio layers})
or diag explain { $test_fh => \@layers };
}
ok( !grep(m/utf-?8[-_]strict/, @layers), q{utf-?8[-_]strict doesn't appear in the perlio layers})
ok(!grep(m/utf-?8[-_]strict/, @layers), q{utf-?8[-_]strict does not appear in the perlio layers})
or diag explain { $test_fh => \@layers };

}
Expand Down

0 comments on commit a083fd5

Please sign in to comment.