File Coverage

blib/lib/Encode/Locale.pm
Criterion Covered Total %
statement 56 83 67.4
branch 15 48 31.2
condition 8 25 32.0
subroutine 12 12 100.0
pod 3 3 100.0
total 94 171 54.9


line stmt bran cond sub pod time code
1             package Encode::Locale;
2              
3 2     2   13982 use strict;
  2         5  
  2         197  
4             our $VERSION = "1.03";
5              
6 2     2   16 use base 'Exporter';
  2         4  
  2         313  
7             our @EXPORT_OK = qw(
8             decode_argv env
9             $ENCODING_LOCALE $ENCODING_LOCALE_FS
10             $ENCODING_CONSOLE_IN $ENCODING_CONSOLE_OUT
11             );
12              
13 2     2   611529 use Encode ();
  2         212724  
  2         283  
14 2     2   23 use Encode::Alias ();
  2         3  
  2         2670  
15              
16             our $ENCODING_LOCALE;
17             our $ENCODING_LOCALE_FS;
18             our $ENCODING_CONSOLE_IN;
19             our $ENCODING_CONSOLE_OUT;
20              
21             sub DEBUG () { 0 }
22              
23             sub _init {
24 3 50   3   23 if ($^O eq "MSWin32") {
25 0 0       0 unless ($ENCODING_LOCALE) {
26             # Try to obtain what the Windows ANSI code page is
27 0         0 eval {
28 0 0       0 unless (defined &GetACP) {
29 0         0 require Win32::API;
30 0         0 Win32::API->Import('kernel32', 'int GetACP()');
31             };
32 0 0       0 if (defined &GetACP) {
33 0         0 my $cp = GetACP();
34 0 0       0 $ENCODING_LOCALE = "cp$cp" if $cp;
35             }
36             };
37             }
38              
39 0 0       0 unless ($ENCODING_CONSOLE_IN) {
40             # If we have the Win32::Console module installed we can ask
41             # it for the code set to use
42 0         0 eval {
43 0         0 require Win32::Console;
44 0         0 my $cp = Win32::Console::InputCP();
45 0 0       0 $ENCODING_CONSOLE_IN = "cp$cp" if $cp;
46 0         0 $cp = Win32::Console::OutputCP();
47 0 0       0 $ENCODING_CONSOLE_OUT = "cp$cp" if $cp;
48             };
49             # Invoking the 'chcp' program might also work
50 0 0 0     0 if (!$ENCODING_CONSOLE_IN && (qx(chcp) || '') =~ /^Active code page: (\d+)/) {
      0        
51 0         0 $ENCODING_CONSOLE_IN = "cp$1";
52             }
53             }
54             }
55              
56 3 100       12 unless ($ENCODING_LOCALE) {
57 2         3 eval {
58 2         3498 require I18N::Langinfo;
59 2         2347 $ENCODING_LOCALE = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET());
60              
61             # Workaround of Encode < v2.25. The "646" encoding alias was
62             # introduced in Encode-2.25, but we don't want to require that version
63             # quite yet. Should avoid the CPAN testers failure reported from
64             # openbsd-4.7/perl-5.10.0 combo.
65 2 50       13 $ENCODING_LOCALE = "ascii" if $ENCODING_LOCALE eq "646";
66              
67             # https://rt.cpan.org/Ticket/Display.html?id=66373
68 2 50 33     14 $ENCODING_LOCALE = "hp-roman8" if $^O eq "hpux" && $ENCODING_LOCALE eq "roman8";
69             };
70 2   33     10 $ENCODING_LOCALE ||= $ENCODING_CONSOLE_IN;
71             }
72              
73 3 50       12 if ($^O eq "darwin") {
74 0   0     0 $ENCODING_LOCALE_FS ||= "UTF-8";
75             }
76              
77             # final fallback
78 3 0 33     11 $ENCODING_LOCALE ||= $^O eq "MSWin32" ? "cp1252" : "UTF-8";
79 3   33     21 $ENCODING_LOCALE_FS ||= $ENCODING_LOCALE;
80 3   66     28 $ENCODING_CONSOLE_IN ||= $ENCODING_LOCALE;
81 3   66     16 $ENCODING_CONSOLE_OUT ||= $ENCODING_CONSOLE_IN;
82              
83 3 50       15 unless (Encode::find_encoding($ENCODING_LOCALE)) {
84 0         0 my $foundit;
85 0 0       0 if (lc($ENCODING_LOCALE) eq "gb18030") {
86 0         0 eval {
87 0         0 require Encode::HanExtra;
88             };
89 0 0       0 if ($@) {
90 0         0 die "Need Encode::HanExtra to be installed to support locale codeset ($ENCODING_LOCALE), stopped";
91             }
92 0 0       0 $foundit++ if Encode::find_encoding($ENCODING_LOCALE);
93             }
94 0 0       0 die "The locale codeset ($ENCODING_LOCALE) isn't one that perl can decode, stopped"
95             unless $foundit;
96              
97             }
98              
99             # use Data::Dump; ddx $ENCODING_LOCALE, $ENCODING_LOCALE_FS, $ENCODING_CONSOLE_IN, $ENCODING_CONSOLE_OUT;
100             }
101              
102             _init();
103             Encode::Alias::define_alias(sub {
104 2     2   17 no strict 'refs';
  2         3  
  2         82  
105 2     2   12 no warnings 'once';
  2         4  
  2         185  
106             return ${"ENCODING_" . uc(shift)};
107             }, "locale");
108              
109             sub _flush_aliases {
110 2     2   10 no strict 'refs';
  2         5  
  2         1448  
111 1     1   6 for my $a (keys %Encode::Alias::Alias) {
112 4 100       5 if (defined ${"ENCODING_" . uc($a)}) {
  4         21  
113 1         3 delete $Encode::Alias::Alias{$a};
114 1         2 warn "Flushed alias cache for $a" if DEBUG;
115             }
116             }
117             }
118              
119             sub reinit {
120 1     1 1 343 $ENCODING_LOCALE = shift;
121 1         2 $ENCODING_LOCALE_FS = shift;
122 1         3 $ENCODING_CONSOLE_IN = $ENCODING_LOCALE;
123 1         2 $ENCODING_CONSOLE_OUT = $ENCODING_LOCALE;
124 1         31 _init();
125 1         4041 _flush_aliases();
126             }
127              
128             sub decode_argv {
129 1 50   1 1 398 die if defined wantarray;
130 1         4 for (@ARGV) {
131 0         0 $_ = Encode::decode(locale => $_, @_);
132             }
133             }
134              
135             sub env {
136 9     9 1 3545 my $k = Encode::encode(locale => shift);
137 9         355 my $old = $ENV{$k};
138 9 100       24 if (@_) {
139 4         7 my $v = shift;
140 4 100       12 if (defined $v) {
141 3         9 $ENV{$k} = Encode::encode(locale => $v);
142             }
143             else {
144 1         5 delete $ENV{$k};
145             }
146             }
147 9 50       141 return Encode::decode(locale => $old) if defined wantarray;
148             }
149              
150             1;
151              
152             __END__