blib/lib/File/Find/utf8.pm

filter: Statementstmt 98.0% 98 / 100 Branchbran 64.8% 35 / 54 Conditioncond 39.4% 15 / 38 MC/DCmcdc 8.7% 2 / 23 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 71.8% 166 / 231 CC 44 SCAR 32.7
CC
44
Cov
77%
__ANON__
51.0
_utf8_find
32.1
import
6.0
CRAP 26.3
↑ Summary
1 package File::Find::utf8;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use 5.010; # state
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Fully UTF-8 aware File::Find
7 5 our $VERSION = '0.014'; # VERSION
8  
9 #pod =begin :prelude
10 #pod
11 #pod =for test_synopsis
12 #pod my @directories_to_search;
13 #pod
14 #pod =end :prelude
15 #pod
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod # Use the utf-8 versions of find and finddepth
19 #pod use File::Find::utf8;
20 #pod find(\&wanted, @directories_to_search);
21 #pod
22 #pod # Revert back to non-utf-8 versions
23 #pod no File::Find::utf8;
24 #pod finddepth(\&wanted, @directories_to_search);
25 #pod
26 #pod # Export only the find function
27 #pod use File::Find::utf8 qw(find);
28 #pod find(\&wanted, @directories_to_search);
29 #pod
30 #pod # Export no functions
31 #pod use File::Find::utf8 qw(:none); # NOT "use File::Find::utf8 qw();"!
32 #pod File::Find::find(\&wanted, @directories_to_search);
33 #pod
34 #pod =head1 DESCRIPTION
35 #pod
36 #pod While the original L<File::Find> functions are capable of handling
37 #pod UTF-8 quite well, they expect and return all data as bytes, not as
38 #pod characters.
39 #pod
40 #pod This module replaces the L<File::Find> functions with fully UTF-8
41 #pod aware versions, both expecting and returning characters.
42 #pod
43 #pod B<Note:> Replacement of functions is not done on DOS, Windows, and OS/2
44 #pod as these systems do not have full UTF-8 file system support.
45 #pod
46 #pod =head2 Behaviour
47 #pod
48 #pod The module behaves as a pragma so you can use both C<use
49 #pod File::Find::utf8> and C<no File::Find::utf8> to turn utf-8 support on
50 #pod or off.
51 #pod
52 #pod By default, both find() and finddepth() are exported (as with the original
53 #pod L<File::Find>), if you want to prevent this, use C<use File::Find::utf8
54 #pod qw(:none)>. (As all the magic happens in the module's import function,
55 #pod you can not simply use C<use File::Find::utf8 qw()>)
56 #pod
57 #pod L<File::Find> warning levels are properly propagated. Note though that
58 #pod for propagation of fatal L<File::Find> warnings, Perl 5.12 or higher
59 #pod is required (or the appropriate version of L<warnings>).
60 #pod
61 #pod =head1 COMPATIBILITY
62 #pod
63 #pod The filesystems of Dos, Windows, and OS/2 do not (fully) support
64 #pod UTF-8. The L<File::Find> function will therefore not be replaced on these
65 #pod systems.
66 #pod
67 #pod =head1 SEE ALSO
68 #pod
69 #pod =for :list
70 #pod * L<File::Find> -- The original module.
71 #pod * L<Cwd::utf8> -- Fully utf-8 aware version of the L<Cwd> functions.
72 #pod * L<utf8::all> -- Turn on utf-8, all of it.
73 #pod This was also the module I first added the utf-8 aware versions of
74 #pod L<Cwd> and L<File::Find> to before moving them to their own package.
75 #pod
76 #pod =cut
77  
78 5 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
79 5 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 # Holds the pointers to the original version of redefined functions
82 5 state %_orig_functions;
83  
84 # Current (i.e., this) package
85 5 my $current_package = __PACKAGE__;
86  
87 # Original package (i.e., the one for which this module is replacing the functions)
88 5 my $original_package = $current_package;
89 5 $original_package =~ s/::utf8$//;
90  
91 5 require Carp;
92 5 $Carp::Internal{$current_package}++; # To get warnings reported at correct caller level
93  
94 #pod =attr $File::Find::utf8::SPECIALVARS
95 #pod
96 #pod By default C<File::Find::utf8> only decodes the I<normal>
97 #pod L<File::Find> variables C<$_>, C<$File::Find::name>,
98 #pod C<$File::Find::dir>, and (when C<follow> or C<follow_fast> is in
99 #pod effect) C<$File::Find::fullname> for use in the C<preprocess>,
100 #pod C<wanted>, and C<postporcess> functions.
101 #pod
102 #pod If for any reason (e.g., compatibility with find.pl or find2perl) you
103 #pod also need the I<special> variables C<$File::Find::topdir>,
104 #pod C<$File::Find::topdev>, C<$File::Find::topino>,
105 #pod C<$File::Find::topmode>, and C<$File::Find::topnlink> to be decoded,
106 #pod specify C<local $File::Find::utf8::COMPATILBILITY = 1;> in your
107 #pod code. The extra decoding that needs to happen will impact performance
108 #pod though, so use only when absolutely necessary.
109 #pod
110 #pod =cut
111  
112 5 our $SPECIALVARS = 0;
113  
114 #pod =attr $File::Find::utf8::UTF8_CHECK
115 #pod
116 #pod By default C<File::Find::utf8> marks decoding errors as fatal (default value
117 #pod for this setting is C<Encode::FB_CROAK>). If you want, you can change this by
118 #pod setting C<File::Find::utf8::UTF8_CHECK>. The value C<Encode::FB_WARN> reports
119 #pod the encoding errors as warnings, and C<Encode::FB_DEFAULT> will completely
120 #pod ignore them. Please see L<Encode> for details. Note: C<Encode::LEAVE_SRC> is
121 #pod I<always> enforced.
122 #pod =cut
123  
124 5 our $UTF8_CHECK = Encode::FB_CROAK | Encode::LEAVE_SRC; # Die on encoding errors
125  
126 # UTF-8 Encoding object
127 5 my $_UTF8 = Encode::find_encoding('UTF-8');
128  
129 sub import {
130 # If run on the dos/os2/windows platform, ignore overriding functions silently.
131 # These platforms do not have (proper) utf-8 file system suppport...
132 8 ▶ unless ($^O =~ /MSWin32|cygwin|dos|os2/) {
Subroutine import: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /MSWin32|cygwin|dos|os2/) 8 0 8
133 5 ▶ no strict qw(refs); ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
134 5 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 # Redefine each of the functions to their UTF-8 equivalent
137 8 for my $f (@{$original_package . '::EXPORT'}, @{$original_package . '::EXPORT_OK'}) {
138 # If we already have the _orig_function, we have redefined the function
139 # in an earlier load of this module, so we need not do it again
140 16 ▶ unless ($_orig_functions{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_orig_functions{$f}) 8 8 16
141 8 $_orig_functions{$f} = \&{$original_package . '::' . $f};
142 8 *{$original_package . '::' . $f} = \&{"_utf8_$f"};
143 }
144 }
145 8 $^H{$current_package} = 1; # Set compiler hint that we should use the utf-8 version
146 }
147  
148 # Determine symbols to export
149 8 shift; # First argument contains the package (that's us)
150 8 ▶ @_ = (':DEFAULT') if !@_; # If nothing provided, use default
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 4 8
151 8 @_ = grep { $_ ne ':none' } @_; # Strip :none tag
152  
153 # Use exporter to export
154 8 require Exporter;
155 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 1 8
156 7 @_ = ($original_package, @_);
157 7 goto &Exporter::import;
158 }
159  
160 1 return;
161 }
162  
163 sub unimport { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
164 1 ▶ $^H{$current_package} = 0; # Set compiler hint that we should not use the utf-8 version
Subroutine unimport: called CC=1 SCAR=0.0
165 1 return;
166 }
167  
168 sub _utf8_find {
169 15 ▶ my $ref = shift; # This can be the wanted function or a find options hash
Subroutine _utf8_find: called CC=19 SCAR=34.7
170 # Make argument always into the find's options hash
171 15 ▶ my %find_options_hash = ref($ref) eq "HASH" ? %$ref : (wanted => $ref);
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'HASH' ? : 12 3 15
172  
173 # Holds the (possibly encoded) arguments
174 15 my @args = @_;
175  
176 # Get the hint from the caller (one level deeper if called from finddepth)
177 15 ▶ my $hints = ((caller 1)[3]//'') ne 'File::Find::utf8::_utf8_finddepth' ? (caller 0)[10] : (caller 1)[10];
Branch: 1 branch
BranchTrueFalseTotal
((caller 1)[3] // '') ne 'File::Find::utf8::_utf8_finddepth' ? : 11 4 15
Condition50%1/2
(caller 1)[3] // ""
l!l
150
MC/DC0%0/1
(caller 1)[3] // ""
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
178 15 ▶ if ($hints->{$current_package}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hints->{$current_package}) 12 3 15
179 12 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::UTF8_CHECK 11 1 12
180  
181 # Save original processors
182 12 my %org_proc;
183 12 for my $proc ("wanted", "preprocess", "postprocess") { $org_proc{$proc} = $find_options_hash{$proc}; }
184 my $follow_option = (exists $find_options_hash{follow} && $find_options_hash{follow})
185 12 ▶ || (exists $find_options_hash{follow_fast} && $find_options_hash{follow_fast});
Condition33%3/9
exists $find_options_hash{'follow'} && $find_options_hash{'follow'} || exists $find_options_hash{'follow_fast'} && $find_options_hash{'follow_fast'}
l!l&&r!l&&!r
0012
exists $find_options_hash{"follow"} and $find_options_hash{"follow"}
!ll&&!rl&&r
1200
exists $find_options_hash{"follow_fast"} and $find_options_hash{"follow_fast"}
!ll&&!rl&&r
1200
MC/DC0%0/4
exists $find_options_hash{'follow'} && $find_options_hash{'follow'} || exists $find_options_hash{'follow_fast'} && $find_options_hash{'follow_fast'}
exists $find_options_hash{"follow"} $find_options_hash{"follow"} exists $find_options_hash{"follow_fast"} $find_options_hash{"follow_fast"}
Truth table14%1/7
A and B || C and D
A= exists $find_options_hash{"follow"}B= $find_options_hash{"follow"}C= exists $find_options_hash{"follow_fast"}D= $find_options_hash{"follow_fast"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
186  
187 # Wrap processors to become utf8-aware
188 12 ▶ if (defined $org_proc{wanted} && ref $org_proc{wanted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org_proc{"wanted"} and ref $org_proc{"wanted"}) 12 0 12
Condition33%1/3
defined $org_proc{"wanted"} and ref $org_proc{"wanted"}
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $org_proc{"wanted"} and ref $org_proc{"wanted"}
defined $org_proc{"wanted"} ref $org_proc{"wanted"}
Truth table33%1/3
A and B
A= defined $org_proc{"wanted"}B= ref $org_proc{"wanted"}
ABresult
0X0
100
111
189 $find_options_hash{wanted} = sub {
190 # Decode the file variables so they become characters
191 15 ▶ local $_ = $_UTF8->decode($_, $UTF8_CHECK) if $_;
Subroutine __ANON__: called CC=17 SCAR=39.3
Branch: 1 branch
BranchTrueFalseTotal
if $_ 15 0 15
192 15 ▶ local $File::Find::name = $_UTF8->decode($File::Find::name, $UTF8_CHECK) if $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name 15 0 15
193 15 ▶ local $File::Find::dir = $_UTF8->decode($File::Find::dir, $UTF8_CHECK) if $File::Find::dir;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir 15 0 15
194 15 ▶ local $File::Find::fullname = $_UTF8->decode($File::Find::fullname, $UTF8_CHECK) if $follow_option && $File::Find::fullname;
Branch: 1 branch
BranchTrueFalseTotal
if $follow_option and $File::Find::fullname 0 15 15
Condition33%1/3
$follow_option and $File::Find::fullname
!ll&&!rl&&r
1500
MC/DC0%0/2
$follow_option and $File::Find::fullname
$follow_option $File::Find::fullname
Truth table33%1/3
A and B
A= $follow_optionB= $File::Find::fullname
ABresult
0X0
100
111
195 # These are only necessary for compatibility reasons (find.pl, find2perl).
196 # If you need them, set $File::Find::utf8::SPECIALVARS
197 15 ▶ local $File::Find::topdir = $_UTF8->decode($File::Find::topdir, $UTF8_CHECK) if $SPECIALVARS && $File::Find::topdir;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::SPECIALVARS and $File::Find::topdir 0 15 15
Condition33%1/3
$File::Find::utf8::SPECIALVARS and $File::Find::topdir
!ll&&!rl&&r
1500
MC/DC0%0/2
$File::Find::utf8::SPECIALVARS and $File::Find::topdir
$File::Find::utf8::SPECIALVARS $File::Find::topdir
Truth table33%1/3
A and B
A= $File::Find::utf8::SPECIALVARSB= $File::Find::topdir
ABresult
0X0
100
111
198 15 ▶ local $File::Find::topdev = $_UTF8->decode($File::Find::topdev, $UTF8_CHECK) if $SPECIALVARS && $File::Find::topdev;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::SPECIALVARS and $File::Find::topdev 0 15 15
Condition33%1/3
$File::Find::utf8::SPECIALVARS and $File::Find::topdev
!ll&&!rl&&r
1500
MC/DC0%0/2
$File::Find::utf8::SPECIALVARS and $File::Find::topdev
$File::Find::utf8::SPECIALVARS $File::Find::topdev
Truth table33%1/3
A and B
A= $File::Find::utf8::SPECIALVARSB= $File::Find::topdev
ABresult
0X0
100
111
199 15 ▶ local $File::Find::topino = $_UTF8->decode($File::Find::topino, $UTF8_CHECK) if $SPECIALVARS && $File::Find::topino;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::SPECIALVARS and $File::Find::topino 0 15 15
Condition33%1/3
$File::Find::utf8::SPECIALVARS and $File::Find::topino
!ll&&!rl&&r
1500
MC/DC0%0/2
$File::Find::utf8::SPECIALVARS and $File::Find::topino
$File::Find::utf8::SPECIALVARS $File::Find::topino
Truth table33%1/3
A and B
A= $File::Find::utf8::SPECIALVARSB= $File::Find::topino
ABresult
0X0
100
111
200 15 ▶ local $File::Find::topmode = $_UTF8->decode($File::Find::topmode, $UTF8_CHECK) if $SPECIALVARS && $File::Find::topmode;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::SPECIALVARS and $File::Find::topmode 0 15 15
Condition33%1/3
$File::Find::utf8::SPECIALVARS and $File::Find::topmode
!ll&&!rl&&r
1500
MC/DC0%0/2
$File::Find::utf8::SPECIALVARS and $File::Find::topmode
$File::Find::utf8::SPECIALVARS $File::Find::topmode
Truth table33%1/3
A and B
A= $File::Find::utf8::SPECIALVARSB= $File::Find::topmode
ABresult
0X0
100
111
201 15 ▶ local $File::Find::topnlink = $_UTF8->decode($File::Find::topnlink, $UTF8_CHECK) if $SPECIALVARS && $File::Find::topnlink;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::utf8::SPECIALVARS and $File::Find::topnlink 0 15 15
Condition33%1/3
$File::Find::utf8::SPECIALVARS and $File::Find::topnlink
!ll&&!rl&&r
1500
MC/DC0%0/2
$File::Find::utf8::SPECIALVARS and $File::Find::topnlink
$File::Find::utf8::SPECIALVARS $File::Find::topnlink
Truth table33%1/3
A and B
A= $File::Find::utf8::SPECIALVARSB= $File::Find::topnlink
ABresult
0X0
100
111
202 15 ▶ return $org_proc{wanted}->(map { $_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
203 12 };
204 }
205 12 for my $proc ("preprocess", "postprocess") {
206 24 ▶ if (defined $org_proc{$proc} && ref $org_proc{$proc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org_proc{$proc} and ref $org_proc{$proc}) 3 21 24
Condition66%2/3
defined $org_proc{$proc} and ref $org_proc{$proc}
!ll&&!rl&&r
2103
MC/DC50%1/2
defined $org_proc{$proc} and ref $org_proc{$proc}
defined $org_proc{$proc} ref $org_proc{$proc}
Truth table66%2/3
A and B
A= defined $org_proc{$proc}B= ref $org_proc{$proc}
ABresult
0X0
100
111
207 $find_options_hash{$proc} = sub {
208 # Decode the file variables so they become characters
209 3 ▶ local $File::Find::dir = $_UTF8->decode($File::Find::dir, $UTF8_CHECK) if $File::Find::dir;
Subroutine __ANON__: called CC=4 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir 3 0 3
210 # Decode the arguments and encode the results
211 3 ▶ return map { $_ ? $_UTF8->encode($_, $UTF8_CHECK) : $_ } $org_proc{$proc}->(map { $_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_ } @_);
Branch: 2 branches
BranchTrueFalseTotal
$_ ? : 0 0 0
$_ ? : 69 0 69
212 3 };
213 }
214 }
215 # Encode arguments as utf-8 so that the original File::Find receives bytes
216 12 ▶ @args = map { $_ ? $_UTF8->encode($_, $UTF8_CHECK) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 12 0 12
217 }
218  
219 # Make sure warning level propagates to File::Find
220 # Note: on perl prior to v5.12 warnings_fatal_enabled does not exist
221 # so we can not use it.
222 14 ▶ if (!warnings::enabled('File::Find')) {
Branch: 2 branches
BranchTrueFalseTotal
if (not warnings::enabled('File::Find')) { } 3 11 14
elsif (not exists &warnings::fatal_enabled && warnings::fatal_enabled('File::Find')) { } 10 1 11
223 5 ▶ no warnings 'File::Find';
Subroutine BEGIN: called CC=1 SCAR=0.0
224 3 return $_orig_functions{find}->(\%find_options_hash, @args);
225 ▶ } elsif (!exists &warnings::fatal_enabled or !warnings::fatal_enabled('File::Find')) {
Condition66%2/3
exists &warnings::fatal_enabled && warnings::fatal_enabled('File::Find')
!ll&&!rl&&r
0101
MC/DC50%1/2
exists &warnings::fatal_enabled && warnings::fatal_enabled('File::Find')
exists &warnings::fatal_enabled warnings::fatal_enabled('File::Find')
Truth table66%2/3
A && B
A= exists &warnings::fatal_enabledB= warnings::fatal_enabled('File::Find')
ABresult
0X0
100
111
226 5 ▶ use warnings 'File::Find';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 10 return $_orig_functions{find}->(\%find_options_hash, @args);
228 } else {
229 5 ▶ use warnings FATAL => qw(File::Find);
Subroutine BEGIN: called CC=1 SCAR=0.0
230 1 return $_orig_functions{find}->(\%find_options_hash, @args);
231 }
232 }
233  
234 sub _utf8_finddepth {
235 4 ▶ my $ref = shift; # This can be the wanted function or a find options hash
Subroutine _utf8_finddepth: called CC=2 SCAR=7.2
236 4 ▶ return _utf8_find( { bydepth => 1, ref($ref) eq "HASH" ? %$ref : (wanted => $ref) }, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'HASH' ? : 4 0 4
237 }
238  
239 5 1;
240  
241 __END__
↑ Summary