blib/lib/Cwd/utf8.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 91.6% 88 / 96 CC 13 SCAR 16.1
CC
13
Cov
91%
_utf8_cwd
8.2
import
7.0
BEGIN
1.0
CRAP 5.0
↑ Summary
1 package Cwd::utf8;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use 5.010; # state
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Fully UTF-8 aware Cwd
7 4 our $VERSION = '0.014'; # VERSION
8  
9 #pod =begin :prelude
10 #pod
11 #pod =for test_synopsis
12 #pod my $file;
13 #pod
14 #pod =end :prelude
15 #pod
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod # Using the utf-8 versions of cwd, getcwd, fastcwd, fastgetcwd
19 #pod use Cwd::utf8;
20 #pod my $dir = getcwd;
21 #pod
22 #pod # Using the utf-8 versions of abs_path
23 #pod use Cwd::utf8 qw(abs_path);
24 #pod my $abs_path = abs_path($file);
25 #pod
26 #pod # Exporting no functions
27 #pod use Cwd::utf8 qw(:none); # NOT "use Cwd::utf8 qw();"!
28 #pod my $real_path = Cwd::real_path($file);
29 #pod
30 #pod =head1 DESCRIPTION
31 #pod
32 #pod While the original L<Cwd> functions are capable of handling UTF-8
33 #pod quite well, they expects and return all data as bytes, not as
34 #pod characters.
35 #pod
36 #pod This module replaces all the L<Cwd> functions with fully UTF-8 aware
37 #pod versions, both expecting and returning characters.
38 #pod
39 #pod B<Note:> Replacement of functions is not done on DOS, Windows, and OS/2
40 #pod as these systems do not have full UTF-8 file system support.
41 #pod
42 #pod =head2 Behaviour
43 #pod
44 #pod The module behaves as a pragma so you can use both C<use
45 #pod Cwd::utf8> and C<no Cwd::utf8> to turn utf-8 support on
46 #pod or off.
47 #pod
48 #pod By default, cwd(), getcwd(), fastcwd(), and fastgetcwd() (and, on
49 #pod Win32, getdcwd()) are exported (as with the original L<Cwd>). If you
50 #pod want to prevent this, use C<use Cwd::utf8 qw(:none)>. (As all the
51 #pod magic happens in the module's import function, you can not simply use
52 #pod C<use Cwd::utf8 qw()>)
53 #pod
54 #pod =head1 COMPATIBILITY
55 #pod
56 #pod The filesystems of Dos, Windows, and OS/2 do not (fully) support
57 #pod UTF-8. The L<Cwd> function will therefore not be replaced on these
58 #pod systems.
59 #pod
60 #pod =head1 SEE ALSO
61 #pod
62 #pod =for :list
63 #pod * L<Cwd> -- The original module
64 #pod * L<File::Find::utf8> -- Fully utf-8 aware versions of the L<File::Find>
65 #pod functions.
66 #pod * L<utf8::all> -- Turn on utf-8, all of it.
67 #pod This was also the module I first added the utf-8 aware versions of
68 #pod L<Cwd> and L<File::Find> to before moving them to their own package.
69 #pod
70 #pod =cut
71  
72 4 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 # Holds the pointers to the original version of redefined functions
76 4 state %_orig_functions;
77  
78 # Current (i.e., this) package
79 4 my $current_package = __PACKAGE__;
80  
81 # Original package (i.e., the one for which this module is replacing the functions)
82 4 my $original_package = $current_package;
83 4 $original_package =~ s/::utf8$//;
84  
85 4 require Carp;
86 4 $Carp::Internal{$current_package}++; # To get warnings reported at correct caller level
87  
88 #pod =attr $Cwd::utf8::UTF8_CHECK
89 #pod
90 #pod By default C<Cwd:::utf8> marks decoding errors as fatal (default value
91 #pod for this setting is C<Encode::FB_CROAK>). If you want, you can change this by
92 #pod setting C<Cwd::utf8::UTF8_CHECK>. The value C<Encode::FB_WARN> reports
93 #pod the encoding errors as warnings, and C<Encode::FB_DEFAULT> will completely
94 #pod ignore them. Please see L<Encode> for details. Note: C<Encode::LEAVE_SRC> is
95 #pod I<always> enforced.
96 #pod
97 #pod =cut
98  
99 4 our $UTF8_CHECK = Encode::FB_CROAK | Encode::LEAVE_SRC; # Die on encoding errors
100  
101 # UTF-8 Encoding object
102 4 my $_UTF8 = Encode::find_encoding('UTF-8');
103  
104 8 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
105 # Target package (i.e., the one loading this module)
106 8 my $target_package = caller;
107  
108 # If run on the dos/os2/windows platform, ignore overriding functions silently.
109 # These platforms do not have (proper) utf-8 file system suppport...
110 8 ▶ unless ($^O =~ /MSWin32|cygwin|dos|os2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /MSWin32|cygwin|dos|os2/) 8 0 8
111 4 ▶ no strict qw(refs); ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
112 4 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 # Redefine each of the functions to their UTF-8 equivalent
115 8 for my $f (@{$original_package . '::EXPORT'}, @{$original_package . '::EXPORT_OK'}) {
116 # If we already have the _orig_function, we have redefined the function
117 # in an earlier load of this module, so we need not do it again
118 72 ▶ unless ($_orig_functions{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_orig_functions{$f}) 27 45 72
119 27 $_orig_functions{$f} = \&{$original_package . '::' . $f};
120 27 ▶ *{$original_package . '::' . $f} = sub { return _utf8_cwd($f, @_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
121 }
122 }
123 8 $^H{$current_package} = 1; # Set compiler hint that we should use the utf-8 version
124 }
125  
126 # Determine symbols to export
127 8 shift; # First argument contains the package (that's us)
128 8 ▶ @_ = (':DEFAULT') if !@_; # If nothing provided, use default
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 6 8
129 8 ▶ @_ = map { $_ eq ':none' ? () : $_ } @_; # Replace :none tag with empty list
Branch: 1 branch
BranchTrueFalseTotal
$_ eq ':none' ? : 1 10 11
130  
131 # Use exporter to export
132 8 require Exporter;
133 8 ▶ Exporter::export_to_level($original_package, 1, $target_package, @_) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 1 8
134  
135 7 return;
136 }
137  
138 1 ▶sub unimport { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine unimport: called CC=1 SCAR=0.0
139 1 $^H{$current_package} = 0; # Set compiler hint that we should not use the utf-8 version
140 1 return;
141 }
142  
143 40 ▶sub _utf8_cwd {
Subroutine _utf8_cwd: called CC=7 SCAR=21.1
144 40 my $func = shift;
145  
146 40 my $hints = (caller 1)[10]; # Use caller level 1 because of the added anonymous sub around call
147 40 ▶ if (! $hints->{$current_package}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hints->{$current_package}) { } 23 17 40
148 # Use original function if we're not using Cwd::utf8 in calling package
149 23 return $_orig_functions{$func}->(@_);
150 } else {
151 17 ▶ $UTF8_CHECK |= Encode::LEAVE_SRC if $UTF8_CHECK; # Enforce LEAVE_SRC
Branch: 1 branch
BranchTrueFalseTotal
if $Cwd::utf8::UTF8_CHECK 16 1 17
152 17 ▶ my @args = map { $_ ? $_UTF8->encode($_, $UTF8_CHECK) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 11 0 11
153 16 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 16 16
154 0 ▶ return map { $_ ? $_UTF8->decode($_, $UTF8_CHECK) : $_ } $_orig_functions{$func}->(@args);
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
155 } else {
156 16 my $r = $_orig_functions{$func}->(@args);
157 16 ▶ return $r ? $_UTF8->decode($r, $UTF8_CHECK) : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 16 0 16
158 }
159 }
160 }
161  
162 4 1;
163  
164 __END__
↑ Summary