blib/lib/winja.pm

filter: Statementstmt 57.1% 24 / 42 Branchbran 30.0% 6 / 20 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 42.7% 38 / 89 CC 19 SCAR 28.5
CC
19
Cov
43%
__ANON__
34.6
__ANON__
34.6
BEGIN
9.2
CRAP 17.2
↑ Summary
1 package winja;
2 7 ▶use 5.018000;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our $VERSION = '1.0.4';
5  
6 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ## define custom warn()
9 my $warn;
10  
11 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=5.0
12 0 ▶ $warn = sub {
Subroutine __ANON__: not called CC=6 SCAR=35.4
13 0 my $message = shift;
14 0 ▶ if ( $message && $message =~ m!\n\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message and $message =~ /\n\z/u) { } 0 0 0
Condition16%1/6
$message and $message =~ /\n\z/u
!ll&&!rl&&r
000
$message and $message =~ /\n\z/u
!ll&&!rl&&r
010
MC/DC0%0/2
$message and $message =~ /\n\z/u
$message $message =~ /\n\z/u
MC/DC0%0/2
$message and $message =~ /\n\z/u
$message $message =~ /\n\z/u
Truth table0%0/3
A and B
A= $messageB= $message =~ /\n\z/u
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $messageB= $message =~ /\n\z/u
ABresult
0X0
100
111
15 0 CORE::warn $message;
16 }
17 else {
18 0 my ( $pkg, $file, $line ) = caller();
19 0 my $where = '';
20 0 ▶ ( $pkg, $file, $line ) = caller(2)
Branch: 1 branch
BranchTrueFalseTotal
if $file eq __FILE__ 0 0 0
21 if $file eq __FILE__;
22 0 ▶ if ( defined $file && defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and defined $line) 0 0 0
Condition16%1/6
defined $file and defined $line
!ll&&!rl&&r
000
defined $file and defined $line
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and defined $line
defined $file defined $line
MC/DC0%0/2
defined $file and defined $line
defined $file defined $line
Truth table0%0/3
A and B
A= defined $fileB= defined $line
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $fileB= defined $line
ABresult
0X0
100
111
23 0 $where = qq{ at $file line $line.$/};
24 }
25 0 print STDERR $message, $where;
26 }
27 7 };
28 }
29  
30 ## check OS & Locale
31 7 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=22.2
32 #check OS
33 7 ▶ if ( $^O ne 'MSWin32' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O ne "MSWin32") 6 1 7
if ($^O ne "MSWin32") 0 1 1
34 6 die __PACKAGE__ . qq{ supports "MSWin32 JApanese edition" only};
35 }
36  
37 #check Locale if possible
38 1 eval { require Win32::API; };
39 1 ▶ if ( !$@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 0 1 1
if (not $@) { } 1 0 1
40 0 my $ALLOWED_LOCALE_ID = 1041; # e.g. Japanese_Japan.932
41 0 my $got;
42 0 eval {
43 0 $got
44 = Win32::API::More->new( 'kernel32',
45 'unsigned short GetSystemDefaultLangID()' )->Call();
46 };
47 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 0 0 0
if ($@) 1 0 1
48 0 die $@;
49 }
50 0 ▶ if ( $got != $ALLOWED_LOCALE_ID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got != $ALLOWED_LOCALE_ID) 0 0 0
51 0 die __PACKAGE__
52 . qq{ supports "MSWin32 JApanese edition" only\n}
53 . qq{ Your LOCALE ID is $got, not $ALLOWED_LOCALE_ID.};
54 }
55 }
56 else {
57 1 $warn->(qq{Cannot decide your locale is really Japanese_Japan.932});
58 1 die $@;
59 }
60 }
61  
62 use warnings;
63 use strict;
64  
65 require Cwd;
66 require File::Spec;
67 require File::Basename;
68 use Encode;
69  
70 BEGIN {
71  
72 {
73 my $package = 'File::Spec::Functions';
74 eval <<EOM;
75 package ${package};
76 winja->import(qw/:FileSpec/);
77 1;
78 EOM
79 }
80 }
81  
82 my %tag;
83  
84 my %override;
85  
86 ##[[[ Begining of Defining Override.
87  
88 # Constants
89 my $pre_x5c_pat
90 = qr/[\x81\x83\x84\x87 \x89-\x9F \xE0-\xEA \xED\xEE \xFA\xFB]/x;
91 my $x5c_pat = qr/${pre_x5c_pat} \x5c/x;
92 my $x5c_end_pat = qr/${x5c_pat} \z/x;
93 my $abs_path_pat = qr!\A (?:[a-zA-Z]:) [/\\]?!x;
94  
95 # Declare 'normalize_if' class
96 my $normalize_class = qr/\A File::Path \z | \A Path::Class:: /x;
97  
98 my $encobj = find_encoding('cp932')
99 || die __PACKAGE__, ": encoding 'cp932' not found";
100  
101 # decoder
102 my $dec = sub {
103 return if !@_ + 0;
104 my @args = map { utf8::is_utf8($_) ? $_ : $encobj->decode($_) } @_;
105 return wantarray ? @args : $args[0];
106 };
107  
108 # encoder
109 my $enc = sub {
110 return if !@_ + 0;
111 my @args = map { utf8::is_utf8($_) ? $encobj->encode($_) : $_ } @_;
112 return wantarray ? @args : $args[0];
113 };
114  
115 my $normalize = sub {
116 return unless @_ + 0;
117 my ($arg) = @_;
118 return if !defined $arg;
119 debugs("NORMALIZE: arg='$arg'");
120 $arg = $dec->($arg);
121 my $delim = $arg =~ /\\/?"\\":"/";
122 $arg =~ s!\\!/!g;
123 $arg = $enc->($arg);
124 my @path = map { $_ =~ $x5c_end_pat ? $_ . q[.] : $_ }
125 split '/', $arg, -1;
126 my $result = join $delim, @path;
127 debugs("NORMALIZE: result='$result'");
128 return $result;
129 };
130  
131 my $normalize_if = sub {
132 return unless @_ + 0;
133 my ($arg) = @_;
134 debugs("NORMALIZE_IF: arg='$arg' ");
135 my ($caller) = caller(2);
136 return $enc->($arg) if !$caller || $caller !~ $normalize_class;
137 my $normalized = $normalize->($arg);
138 debugs("NORMALIZE_IF: $normalized: ");
139 return $normalized;
140 };
141  
142 ## Original methods pool
143  
144 my %org_methods;
145  
146 ## Override 'File::Basename'
147 {
148 no strict 'refs';
149 no warnings 'redefine';
150 $org_methods{'File::Basename::fileparse'}
151 = *{'File::Basename::fileparse'}{CODE};
152 *{'File::Basename::fileparse'} = sub {
153 my @args = $dec->(@_);
154 my @resp = map { $normalize->($_) }
155 $enc->( $org_methods{'File::Basename::fileparse'}->(@args) );
156 debugs( qq{BASENAME:}, $resp[0], qq{, DIRNAME:}, $resp[1] );
157 return wantarray ? @resp : $resp[0];
158 };
159 }
160  
161 ## Override 'FileSpec'
162  
163 require File::Spec;
164 require File::Spec::Win32;
165 my @ovr_methods = map { 'File::Spec::Win32::' . $_ }
166 qw/_canon_cat splitpath splitdir catdir catfile canonpath/;
167 push @{ $tag{':FileSpec'} }, @ovr_methods;
168 push @{ $tag{':FileSpec'} }, 'Cwd::getcwd'; # for 'rel2abs'
169 {
170 no strict 'refs';
171 @org_methods{@ovr_methods} = map { *{$_}{CODE} } @ovr_methods;
172 }
173  
174 # Override 'File::Spec::Win32' only when found valid encoding.
175  
176 for my $method (@ovr_methods) {
177 no strict 'refs';
178 $override{$method} = eval qq`sub {
179 my \$is_utf8 = utf8::is_utf8(join('',\@_));
180 my \$mthd=\$org_methods{'$method'};
181 goto \&{\$mthd} if \$is_utf8;
182 goto \&{\$mthd} if qq{\@_} !~ \$x5c_pat;
183 my \@arg = \$dec->(\@_);
184 my \@resp = \$enc->(\&{\$mthd}(\@arg));
185 return wantarray ? \@resp : \$resp[0];
186 };`;
187 }
188  
189 if ( ( eval { File::Spec::Win32->version } || 0 ) < 3.3 ) {
190 push @{ $tag{':File::Spec'} }, 'File::Spec::canonpath';
191 $override{'File::Spec::canonpath'} = sub {
192 # Legacy / compatibility support
193 return $_[1] if !defined( $_[1] ) or $_[1] eq '';
194 return _canon_cat( $_[1] );
195 };
196 }
197  
198 ## Override 'Cwd';
199 # NOTE: fast*() is the same as *(), not "fast" anymore
200 push @{ $tag{':Cwd'} },
201 map { 'Cwd::' . $_ }
202 qw/cwd getcwd fastcwd fastgetcwd getdcwd abs_path realpath fast_abs_path/;
203 $override{'Cwd::cwd'} = $override{'Cwd::getcwd'}
204 = $override{'Cwd::fastcwd'} = $override{'Cwd::fastgetcwd'} = sub {
205 my $r = join '/', split "\\\\",
206 decode( "cp932", File::Spec->rel2abs(".") );
207 return encode( "cp932", $r );
208 };
209 $override{'Cwd::getdcwd'} = sub {
210 File::Spec->rel2abs(".");
211 };
212 $override{'Cwd::abs_path'} = $override{'Cwd::realpath'}
213 = $override{'Cwd::fast_abs_path'} = sub {
214 return unless @_ + 0;
215 my $a = File::Spec->rel2abs(shift);
216 return unless -e $a;
217 $a = encode( "cp932", join '/', split "\\\\", decode( "cp932", $a ) )
218 if -d $a;
219 return $a;
220 };
221 push @{ $tag{':Cwd'} }, 'Cwd::chdir';
222 $override{'Cwd::chdir'} = sub {
223 my $res = CORE::chdir(shift);
224 $ENV{'PWD'} = $override{'Cwd::getdcwd'}->();
225 return $res;
226 };
227  
228 ##]]] End of Defining Override
229  
230 ## import/unimport
231  
232 my $MGR = {};
233  
234 my %init_done;
235 my %bitmask;
236 my $bits = 0;
237 @bitmask{ keys %override } = map { $bits <<= 1; $bits ||= 1; } keys %override;
238  
239 my $init = sub {
240 my ($key) = @_;
241 return if !defined $key;
242 return if !exists $override{$key};
243 my @class = split '::', $key;
244 my $name = pop @class;
245 if ( $key =~ /\A CORE::GLOBAL::/x ) {
246 $MGR->{$key}->{self} = $override{$key};
247 }
248 else {
249 $MGR->{$key}->{self} = sub {
250 my $H;
251 my $i = 0;
252 $i++ while caller($i);
253 while ( --$i >= 0 ) {
254 my $h = ( caller($i) )[10];
255 next if !$h;
256 next if !exists $$h{ __PACKAGE__ . '' };
257 $H = $$h{ __PACKAGE__ . '' };
258 last;
259 }
260 goto &{ $override{$key} } if $H && $H & $bitmask{$key};
261 goto &{ $MGR->{$key}->{saved} };
262 };
263 }
264 $MGR->{$key}->{name} = $name;
265 $MGR->{$key}->{class} = [@class];
266 $MGR->{$key}->{saved} = ( $org_methods{$key} || undef );
267 $MGR->{$key}->{overridden} = 0;
268 $init_done{$key}++;
269 };
270  
271 my $make_classref = sub {
272 return if !@_;
273 no strict 'refs';
274 my $self = *main::;
275 my @class = @_;
276 for my $key (@class) {
277 $self = ${$self}{ $key . '::' };
278 }
279 $self;
280 };
281  
282 my $delete_sub_from = sub {
283 return unless @_;
284 my $sym = shift;
285 no strict 'refs';
286 return if !*{$sym}{CODE};
287 my $self = *{$sym}{CODE};
288 my $classref = $make_classref->( @{ $MGR->{$sym}->{class} } );
289 my $name = $MGR->{$sym}->{name};
290 my @refs = grep {$_} map { *{$sym}{$_} } qw/SCALAR ARRAY HASH IO/;
291 delete ${$classref}{$name};
292 *{$sym} = $_ for @refs;
293 return $self;
294 };
295  
296 my $tags2keys = sub {
297 my @invalid = grep { !exists $tag{$_} } @_;
298 if (@invalid) {
299 require Carp;
300 Carp::croak( "Invalid tag '", join( "', '", @invalid ), "'" );
301 }
302 my (@tags) = @_;
303 @tags = keys %tag if !@tags;
304 my %seen;
305 my @keys = grep { !$seen{$_}++ } map { @{ $tag{$_} } } @tags;
306 no strict 'refs';
307 no warnings 'once';
308 @keys = grep { /\A CORE::GLOBAL::/x || *{$_}{CODE} } @keys;
309 return @keys;
310 };
311  
312 my $load = sub {
313 my ($key) = @_;
314 $init->($key) if !$MGR->{$key};
315 die "Undefined flag of '$key'" if !exists $bitmask{$key};
316 return if ( $^H{ __PACKAGE__ . '' } ||= 0 ) & $bitmask{$key};
317 $^H{ __PACKAGE__ . '' } |= $bitmask{$key};
318 no strict 'refs';
319 no warnings 'redefine';
320 no warnings 'prototype';
321 my $coderef = *{$key}{CODE};
322  
323 if ( !$coderef || $MGR->{$key}->{self} ne $coderef ) {
324 $MGR->{$key}->{saved} = $coderef
325 if !$MGR->{$key}->{saved}
326 || $coderef && $$MGR{$key}{saved} ne $coderef;
327 $delete_sub_from->($key);
328 *{$key} = $MGR->{$key}->{self};
329 }
330 };
331  
332 sub import {
333 shift;
334 my @keys = $tags2keys->(@_);
335 return if !@keys;
336 $load->($_) for (@keys);
337 }
338  
339 sub debugs {
340 return unless $ENV{'DEBUG_VERBOSE'};
341 my ( $_pkg, $_file, $_line ) = caller(0);
342 my ( $pkg, $file, $line ) = caller(1);
343 print STDERR @_, ' <= ', qq{$_file line $_line}, ' <= ',
344 qq{$file line $line}, $/;
345 }
346  
347 no warnings 'void';
348 'End of winja
349 this module works for WIN32-JApanese only';
350 __END__
↑ Summary