blib/lib/utf8/all.pm

filter: Statementstmt 98.1% 107 / 109 Branchbran 75.0% 39 / 52 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 89.3% 177 / 198 CC 32 SCAR 20.4
CC
32
Cov
90%
import
15.3
_utf8_glob
7.2
_utf8_readdir
7.1
CRAP 7.7
↑ Summary
1 package utf8::all;
2 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use 5.010; # state
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: turn on Unicode - all of it
7 22 our $VERSION = '0.026'; # VERSION
8  
9 #pod =head1 SYNOPSIS
10 #pod
11 #pod use utf8::all; # Turn on UTF-8, all of it.
12 #pod
13 #pod open my $in, '<', 'contains-utf8'; # UTF-8 already turned on here
14 #pod print length 'føø bār'; # 7 UTF-8 characters
15 #pod my $utf8_arg = shift @ARGV; # @ARGV is UTF-8 too (only for main)
16 #pod
17 #pod =head1 DESCRIPTION
18 #pod
19 #pod The C<use utf8> pragma tells the Perl parser to allow UTF-8 in the
20 #pod program text in the current lexical scope. This also means that you
21 #pod can now use literal Unicode characters as part of strings, variable
22 #pod names, and regular expressions.
23 #pod
24 #pod C<utf8::all> goes further:
25 #pod
26 #pod =over 4
27 #pod
28 #pod =item *
29 #pod
30 #pod L<C<charnames>|charnames> are imported so C<\N{...}> sequences can be
31 #pod used to compile Unicode characters based on names.
32 #pod
33 #pod =item *
34 #pod
35 #pod On Perl C<v5.11.0> or higher, the C<use feature 'unicode_strings'> is
36 #pod enabled.
37 #pod
38 #pod =item *
39 #pod
40 #pod C<use feature fc> and C<use feature unicode_eval> are enabled on Perl
41 #pod C<5.16.0> and higher.
42 #pod
43 #pod =item *
44 #pod
45 #pod Filehandles are opened with UTF-8 encoding turned on by default
46 #pod (including C<STDIN>, C<STDOUT>, and C<STDERR> when C<utf8::all> is
47 #pod used from the C<main> package). Meaning that they automatically
48 #pod convert UTF-8 octets to characters and vice versa. If you I<don't>
49 #pod want UTF-8 for a particular filehandle, you'll have to set C<binmode
50 #pod $filehandle>.
51 #pod
52 #pod =item *
53 #pod
54 #pod C<@ARGV> gets converted from UTF-8 octets to Unicode characters (when
55 #pod C<utf8::all> is used from the C<main> package). This is similar to the
56 #pod behaviour of the C<-CA> perl command-line switch (see L<perlrun>).
57 #pod
58 #pod =item *
59 #pod
60 #pod C<readdir>, C<readlink>, C<readpipe> (including the C<qx//> and
61 #pod backtick operators), and L<C<glob>|perlfunc/glob> (including the C<<
62 #pod <> >> operator) now all work with and return Unicode characters
63 #pod instead of (UTF-8) octets (again only when C<utf8::all> is used from
64 #pod the C<main> package).
65 #pod
66 #pod =back
67 #pod
68 #pod =head2 Lexical Scope
69 #pod
70 #pod The pragma is lexically-scoped, so you can do the following if you had
71 #pod some reason to:
72 #pod
73 #pod {
74 #pod use utf8::all;
75 #pod open my $out, '>', 'outfile';
76 #pod my $utf8_str = 'føø bār';
77 #pod print length $utf8_str, "\n"; # 7
78 #pod print $out $utf8_str; # out as utf8
79 #pod }
80 #pod open my $in, '<', 'outfile'; # in as raw
81 #pod my $text = do { local $/; <$in>};
82 #pod print length $text, "\n"; # 10, not 7!
83 #pod
84 #pod Instead of lexical scoping, you can also use C<no utf8::all> to turn
85 #pod off the effects.
86 #pod
87 #pod Note that the effect on C<@ARGV> and the C<STDIN>, C<STDOUT>, and
88 #pod C<STDERR> file handles is always global and can not be undone!
89 #pod
90 #pod =head2 Enabling/Disabling Global Features
91 #pod
92 #pod As described above, the default behaviour of C<utf8::all> is to
93 #pod convert C<@ARGV> and to open the C<STDIN>, C<STDOUT>, and C<STDERR>
94 #pod file handles with UTF-8 encoding, and override the C<readlink> and
95 #pod C<readdir> functions and C<glob> operators when C<utf8::all> is used
96 #pod from the C<main> package.
97 #pod
98 #pod If you want to disable these features even when C<utf8::all> is used
99 #pod from the C<main> package, add the option C<NO-GLOBAL> (or
100 #pod C<LEXICAL-ONLY>) to the use line. E.g.:
101 #pod
102 #pod use utf8::all 'NO-GLOBAL';
103 #pod
104 #pod If on the other hand you want to enable these global effects even when
105 #pod C<utf8::all> was used from another package than C<main>, use the
106 #pod option C<GLOBAL> on the use line:
107 #pod
108 #pod use utf8::all 'GLOBAL';
109 #pod
110 #pod =head2 UTF-8 Errors
111 #pod
112 #pod C<utf8::all> will handle invalid code points (i.e., utf-8 that does
113 #pod not map to a valid unicode "character"), as a fatal error.
114 #pod
115 #pod For C<glob>, C<readdir>, and C<readlink>, one can change this
116 #pod behaviour by setting the attribute L</"$utf8::all::UTF8_CHECK">.
117 #pod
118 #pod =head1 COMPATIBILITY
119 #pod
120 #pod The filesystems of Dos, Windows, and OS/2 do not (fully) support
121 #pod UTF-8. The C<readlink> and C<readdir> functions and C<glob> operators
122 #pod will therefore not be replaced on these systems.
123 #pod
124 #pod =head1 SEE ALSO
125 #pod
126 #pod =over 4
127 #pod
128 #pod =item *
129 #pod
130 #pod L<File::Find::utf8> for fully utf-8 aware File::Find functions.
131 #pod
132 #pod =item *
133 #pod
134 #pod L<Cwd::utf8> for fully utf-8 aware Cwd functions.
135 #pod
136 #pod =back
137 #pod
138 #pod =cut
139  
140 22 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 22 ▶use parent qw(Encode charnames utf8 open warnings feature);
Subroutine BEGIN: called CC=1 SCAR=0.0
142 22 ▶use Symbol qw(qualify_to_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
143 22 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 # Holds the pointers to the original version of redefined functions
146 22 state %_orig_functions;
147  
148 # Current (i.e., this) package
149 22 my $current_package = __PACKAGE__;
150  
151 22 require Carp;
152 22 $Carp::Internal{$current_package}++; # To get warnings reported at correct caller level
153  
154 #pod =attr $utf8::all::UTF8_CHECK
155 #pod
156 #pod By default C<utf8::all> marks decoding errors as fatal (default value
157 #pod for this setting is C<Encode::FB_CROAK>). If you want, you can change this by
158 #pod setting C<$utf8::all::UTF8_CHECK>. The value C<Encode::FB_WARN> reports
159 #pod the encoding errors as warnings, and C<Encode::FB_DEFAULT> will completely
160 #pod ignore them. Please see L<Encode> for details. Note: C<Encode::LEAVE_SRC> is
161 #pod I<always> enforced.
162 #pod
163 #pod Important: Only controls the handling of decoding errors in C<glob>,
164 #pod C<readdir>, and C<readlink>.
165 #pod
166 #pod =cut
167  
168 22 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
169 22 ▶use PerlIO::utf8_strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 22 our $UTF8_CHECK = Encode::FB_CROAK | Encode::LEAVE_SRC; # Die on encoding errors
172  
173 # UTF-8 Encoding object
174 22 my $_UTF8 = Encode::find_encoding('UTF-8');
175  
176 29 ▶sub import {
Subroutine import: called CC=15 SCAR=27.3
177 # Enable features/pragmas in calling package
178 29 my $target = caller;
179  
180 # Enable global effects be default only when imported from main package
181 29 my $no_global = $target ne 'main';
182  
183 # Override global?
184 29 ▶ if (defined $_[1] && $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i) 2 27 29
Condition66%2/3
defined $_[1] and $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i
!ll&&!rl&&r
2702
MC/DC50%1/2
defined $_[1] and $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i
defined $_[1] $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i
Truth table66%2/3
A and B
A= defined $_[1]B= $_[1] =~ /^(?:(NO-)?GLOBAL|LEXICAL-ONLY)$/i
ABresult
0X0
100
111
185 2 $no_global = $_[1] !~ /^GLOBAL$/i;
186 2 splice(@_, 1, 1); # Remove option from import's arguments
187 }
188  
189 29 'utf8'->import::into($target);
190 29 'open'->import::into($target, 'IO' => ':utf8_strict');
191  
192 # use open ':std' only works with some encodings.
193 29 state $have_encoded_std = 0;
194 29 ▶ unless ($no_global || $have_encoded_std++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_global or $have_encoded_std++) 18 11 29
Condition100%3/3
$no_global or $have_encoded_std++
l!l&&r!l&&!r
4718
MC/DC100%2/2
$no_global or $have_encoded_std++
$no_global $have_encoded_std++
Truth table100%3/3
A or B
A= $no_globalB= $have_encoded_std++
ABresult
1X1
011
000
195 18 binmode STDERR, ':utf8_strict';
196 18 binmode STDOUT, ':utf8_strict';
197 18 binmode STDIN, ':utf8_strict';
198 }
199  
200 29 'charnames'->import::into($target, qw{:full :short});
201 29 'warnings'->import::into($target, qw{FATAL utf8});
202 29 ▶ 'feature'->import::into($target, qw{unicode_strings}) if $^V >= v5.11.0;
Branch: 1 branch
BranchTrueFalseTotal
if $^V >= v5.11.0 29 0 29
203 29 ▶ 'feature'->import::into($target, qw{unicode_eval fc}) if $^V >= v5.16.0;
Branch: 1 branch
BranchTrueFalseTotal
if $^V >= v5.16.0 29 0 29
204  
205 29 ▶ unless ($no_global || $^O =~ /MSWin32|cygwin|dos|os2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_global or $^O =~ /MSWin32|cygwin|dos|os2/) 25 4 29
Condition66%2/3
$no_global or $^O =~ /MSWin32|cygwin|dos|os2/
l!l&&r!l&&!r
4025
MC/DC50%1/2
$no_global or $^O =~ /MSWin32|cygwin|dos|os2/
$no_global $^O =~ /MSWin32|cygwin|dos|os2/
Truth table66%2/3
A or B
A= $no_globalB= $^O =~ /MSWin32|cygwin|dos|os2/
ABresult
1X1
011
000
206 22 ▶ no strict qw(refs); ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
207 22 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 # Replace readdir with utf8 aware version
210 25 *{$target . '::readdir'} = \&_utf8_readdir;
211  
212 # Replace readdir with utf8 aware version
213 25 *{$target . '::readlink'} = \&_utf8_readlink;
214  
215 # Replace glob with utf8 aware version
216 25 *{$target . '::glob'} = \&_utf8_glob;
217  
218 # Set compiler hint to encode/decode in the redefined functions
219 25 $^H{'utf8::all'} = 1;
220 }
221  
222 # Make @ARGV utf-8 when, unless perl was launched with the -CA
223 # flag as this already has @ARGV decoded automatically. -CA is
224 # active if the the fifth bit (32) of the ${^UNICODE} variable is
225 # set. (see perlrun on the -C command switch for details about
226 # ${^UNICODE})
227 29 ▶ unless ($no_global || (${^UNICODE} & 32)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_global or ${^UNICODE} & 32) 25 4 29
Condition66%2/3
$no_global or ${^UNICODE} & 32
l!l&&r!l&&!r
4025
MC/DC50%1/2
$no_global or ${^UNICODE} & 32
$no_global ${^UNICODE} & 32
Truth table66%2/3
A or B
A= $no_globalB= ${^UNICODE} & 32
ABresult
1X1
011
000
228 25 state $have_encoded_argv = 0;
229 25 ▶ if (!$have_encoded_argv++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_encoded_argv++) 18 7 25
230 18 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $utf8::all::UTF8_CHECK 18 0 18
231 18 ▶ $_ = ($_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_) for @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 12 0 12
232 }
233 }
234  
235 29 return;
236 }
237  
238 1 ▶sub unimport { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine unimport: called CC=2 SCAR=7.0
239 # Disable features/pragmas in calling package
240 # Note: Does NOT undo the effect on @ARGV,
241 # nor on the STDIN, STDOUT, and STDERR file handles!
242 # These effects are always "global".
243  
244 1 my $target = caller;
245 1 'utf8'->unimport::out_of($target);
246 1 'open'->import::into($target, qw{IO :bytes});
247  
248 1 ▶ unless ($^O =~ /MSWin32|cygwin|dos|os2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /MSWin32|cygwin|dos|os2/) 1 0 1
249 1 $^H{'utf8::all'} = 0; # Reset compiler hint
250 }
251  
252 1 return;
253 }
254  
255 6 ▶sub _utf8_readdir(*) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
Subroutine _utf8_readdir: called CC=7 SCAR=19.6
256 6 my $pre_handle = shift;
257 6 my $hints = (caller 0)[10];
258 6 ▶ my $handle = ref($pre_handle) ? $pre_handle : qualify_to_ref($pre_handle, caller);
Branch: 1 branch
BranchTrueFalseTotal
ref $pre_handle ? : 4 2 6
259 6 ▶ if (not $hints->{'utf8::all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hints->{'utf8::all'}) { } 1 5 6
260 1 return CORE::readdir($handle);
261 } else {
262 5 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $utf8::all::UTF8_CHECK 5 0 5
263 5 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 1 5
264 4 ▶ return map { $_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_ } CORE::readdir($handle);
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 16 0 16
265 } else {
266 1 my $r = CORE::readdir($handle);
267 1 ▶ return $r ? $_UTF8->decode($r, $UTF8_CHECK) : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 1 0 1
268 }
269 }
270 }
271  
272 4 ▶sub _utf8_readlink(_) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
Subroutine _utf8_readlink: called CC=5 SCAR=16.4
273 4 my $arg = shift;
274 4 my $hints = (caller 0)[10];
275 4 ▶ if (not $hints->{'utf8::all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hints->{'utf8::all'}) { } 0 4 4
276 0 return CORE::readlink($arg);
277 } else {
278 4 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $utf8::all::UTF8_CHECK 3 1 4
279 4 ▶ $arg = $arg ? $_UTF8->encode($arg, $UTF8_CHECK) : $arg;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 4 0 4
280 3 my $r = CORE::readlink($arg);
281 3 ▶ return $r ? $_UTF8->decode($r, $UTF8_CHECK) : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 1 2 3
282 }
283 }
284  
285 9 ▶sub _utf8_glob {
Subroutine _utf8_glob: called CC=7 SCAR=19.7
286 9 my $arg = $_[0]; # Making this a lexical somehow is important!
287 9 my $hints = (caller 0)[10];
288 9 ▶ if (not $hints->{'utf8::all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hints->{'utf8::all'}) { } 0 9 9
289 0 ▶ return CORE::glob($arg);
Subroutine BEGIN: called CC=1 SCAR=0.2
290 } else {
291 9 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $utf8::all::UTF8_CHECK 8 1 9
292 9 ▶ $arg = $arg ? $_UTF8->encode($arg, $UTF8_CHECK) : $arg;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 9 0 9
293 8 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 5 8
294 3 ▶ return map { $_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_ } CORE::glob($arg);
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 6 0 6
295 } else {
296 5 my $r = CORE::glob($arg);
297 5 ▶ return $r ? $_UTF8->decode($r, $UTF8_CHECK) : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 3 2 5
298 }
299 }
300 }
301  
302 #pod =head1 INTERACTION WITH AUTODIE
303 #pod
304 #pod If you use L<autodie>, which is a great idea, you need to use at least
305 #pod version B<2.12>, released on L<June 26,
306 #pod 2012|https://metacpan.org/source/PJF/autodie-2.12/Changes#L3>.
307 #pod Otherwise, autodie obliterates the IO layers set by the L<open>
308 #pod pragma. See L<RT
309 #pod #54777|https://rt.cpan.org/Ticket/Display.html?id=54777> and L<GH
310 #pod #7|https://github.com/doherty/utf8-all/issues/7>.
311 #pod
312 #pod =cut
313  
314 22 1;
315  
316 __END__
↑ Summary