blib/lib/encoding.pm

filter: Statementstmt 27.5% 38 / 138 Branchbran 4.5% 3 / 66 Conditioncond 11.7% 2 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 68.7% 11 / 16 Podpod 0.0% 0 / 1 totaltotal 21.6% 54 / 249 CC 41 SCAR 50.6
CC
41
Cov
19%
import
362.4
_get_locale_encoding
88.6
_exception
20.0
CRAP 157.3
↑ Summary blib/lib/Encode/MIME/Header.pm »
1 # $Id: encoding.pm,v 3.01 2026/10/26 01:42:18 dankogai Exp $
2 package encoding;
3 2 our $VERSION = '3.01';
4  
5 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use constant {
Subroutine BEGIN: called CC=2 SCAR=7.2
11 DEBUG => !!$ENV{PERL_ENCODE_DEBUG},
12 2 ▶ HAS_PERLIO => eval { require PerlIO::encoding; PerlIO::encoding->VERSION(0.02) },
Condition33%1/3
$^V && $^V ge v5.21.7
!ll&&!rl&&r
002
MC/DC0%0/2
$^V && $^V ge v5.21.7
$^V $^V ge v5.21.7
Truth table33%1/3
A && B
A= $^VB= $^V ge v5.21.7
ABresult
0X0
100
111
13 PERL_5_21_7 => $^V && $^V ge v5.21.7, # lexically scoped
14 2 };
15  
16 0 ▶sub _exception {
Subroutine _exception: not called CC=4 SCAR=30.0
17 0 my $name = shift;
18 0 ▶ $] > 5.008 and return 0; # 5.8.1 or higher then no
Branch: 1 branch
BranchTrueFalseTotal
$] > "5.008" and return 0 0 0 0
19 0 my %utfs = map { $_ => 1 }
20 qw(utf8 UCS-2BE UCS-2LE UTF-16 UTF-16BE UTF-16LE
21 UTF-32 UTF-32BE UTF-32LE);
22 0 ▶ $utfs{$name} or return 0; # UTFs or no
Branch: 1 branch
BranchTrueFalseTotal
$utfs{$name} or return 0 0 0 0
23 0 require Config;
24 0 Config->import();
25 0 our %Config;
26 0 ▶ return $Config{perl_patchlevel} ? 0 : 1 # maintperl then no
Branch: 1 branch
BranchTrueFalseTotal
$encoding::Config{'perl_patchlevel'} ? : 0 0 0
27 }
28  
29 0 ▶sub in_locale { $^H & ( $locale::hint_bits || 0 ) }
Subroutine in_locale: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$locale::hint_bits || 0
l!l
00
MC/DC0%0/1
$locale::hint_bits || 0
$locale::hint_bits
Truth table0%0/2
A
A= $locale::hint_bits
Aresult
11
00
30  
31 2 ▶sub _get_locale_encoding {
Subroutine _get_locale_encoding: called CC=12 SCAR=44.8
32 2 my $locale_encoding;
33  
34 2 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2 2
35 0 my @tries = (
36 # First try to get the OutputCP. This will work only if we
37 # are attached to a console
38 'Win32.pm' => 'Win32::GetConsoleOutputCP',
39 'Win32/Console.pm' => 'Win32::Console::OutputCP',
40 # If above failed, this means that we are a GUI app
41 # Let's assume that the ANSI codepage is what matters
42 'Win32.pm' => 'Win32::GetACP',
43 );
44 0 ▶ while (@tries) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tries) 0 0 0
45 0 my $cp = eval {
46 0 require $tries[0];
47 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 0 &{$tries[1]}()
49 };
50 0 ▶ if ($cp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp) 0 0 0
51 0 ▶ if ($cp == 65001) { # Code page for UTF-8
Branch: 1 branch
BranchTrueFalseTotal
if ($cp == 65001) { } 0 0 0
52 0 $locale_encoding = 'UTF-8';
53 } else {
54 0 $locale_encoding = 'cp' . $cp;
55 }
56 0 return $locale_encoding;
57 }
58 0 splice(@tries, 0, 2)
59 }
60 }
61  
62 # I18N::Langinfo isn't available everywhere
63 2 $locale_encoding = eval {
64 2 require I18N::Langinfo;
65 2 find_encoding(
66 I18N::Langinfo::langinfo( I18N::Langinfo::CODESET() )
67 )->name
68 };
69 2 ▶ return $locale_encoding if defined $locale_encoding;
Branch: 1 branch
BranchTrueFalseTotal
if defined $locale_encoding 2 0 2
70  
71 0 eval {
72 0 require POSIX;
73 # Get the current locale
74 # Remember that MSVCRT impl is quite different from Unixes
75 0 my $locale = POSIX::setlocale(POSIX::LC_CTYPE());
76 0 ▶ if ( $locale =~ /^([^.]+)\.([^.@]+)(?:@.*)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale =~ /^([^.]+)\.([^.\@]+)(?:\@.*)?$/) 0 0 0
77 0 my $country_language;
78 0 ( $country_language, $locale_encoding ) = ( $1, $2 );
79  
80 # Could do more heuristics based on the country and language
81 # since we have Locale::Country and Locale::Language available.
82 # TODO: get a database of Language -> Encoding mappings
83 # (the Estonian database at http://www.eki.ee/letter/
84 # would be excellent!) --jhi
85 0 ▶ if (lc($locale_encoding) eq 'euc') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $locale_encoding eq "euc") 0 0 0
86 0 ▶ if ( $country_language =~ /^ja_JP|japan(?:ese)?$/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($country_language =~ /^ja_JP|japan(?:ese)?$/i) { } 0 0 0
elsif ($country_language =~ /^ko_KR|korean?$/i) { } 0 0 0
elsif ($country_language =~ /^zh_CN|chin(?:a|ese)$/i) { } 0 0 0
elsif ($country_language =~ /^zh_TW|taiwan(?:ese)?$/i) { } 0 0 0
87 0 $locale_encoding = 'euc-jp';
88 }
89 elsif ( $country_language =~ /^ko_KR|korean?$/i ) {
90 0 $locale_encoding = 'euc-kr';
91 }
92 elsif ( $country_language =~ /^zh_CN|chin(?:a|ese)$/i ) {
93 0 $locale_encoding = 'euc-cn';
94 }
95 elsif ( $country_language =~ /^zh_TW|taiwan(?:ese)?$/i ) {
96 0 $locale_encoding = 'euc-tw';
97 }
98 else {
99 0 require Carp;
100 0 Carp::croak(
101 "encoding: Locale encoding '$locale_encoding' too ambiguous"
102 );
103 }
104 }
105 }
106 };
107  
108 0 return $locale_encoding;
109 }
110  
111 0 ▶sub import {
Subroutine import: not called CC=21 SCAR=58.9
112  
113 0 if ( ord("A") == 193 ) {
114 require Carp;
115 Carp::croak("encoding: pragma does not support EBCDIC platforms");
116 }
117  
118 my $deprecate =
119 ($] >= 5.017 and !$Config{usecperl})
120 0 ▶ ? "Use of the encoding pragma is deprecated" : 0;
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.017 && !$encoding::Config{'usecperl'} ? : 0 0 0
Condition0%0/3
$] >= "5.017" and not $encoding::Config{"usecperl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$] >= "5.017" and not $encoding::Config{"usecperl"}
$] >= "5.017" not $encoding::Config{"usecperl"}
Truth table0%0/3
A and not(B)
A= $] >= "5.017"B= not $encoding::Config{"usecperl"}
ABresult
0X0
100
111
121  
122 0 my $class = shift;
123 0 my $name = shift;
124 0 ▶ if (!$name){
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
125 0 require Carp;
126 0 Carp::croak("encoding: no encoding specified.");
127 }
128 0 ▶ if ( $name eq ':_get_locale_encoding' ) { # used by lib/open.pm
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq ":_get_locale_encoding") 0 0 0
129 0 my $caller = caller();
130 {
131 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
132 0 *{"${caller}::_get_locale_encoding"} = \&_get_locale_encoding;
133 }
134 0 return;
135 }
136 0 ▶ $name = _get_locale_encoding() if $name eq ':locale';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq ":locale" 0 0 0
137 2 ▶ BEGIN { strict->unimport('hashpairs') if $] >= 5.027 and $^V =~ /c$/; }
Subroutine BEGIN: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.027" and $^V =~ /c$/ 0 2 2
Condition33%1/3
$] >= "5.027" and $^V =~ /c$/
!ll&&!rl&&r
020
MC/DC0%0/2
$] >= "5.027" and $^V =~ /c$/
$] >= "5.027" $^V =~ /c$/
Truth table33%1/3
A and B
A= $] >= "5.027"B= $^V =~ /c$/
ABresult
0X0
100
111
138 0 my %arg = @_;
139 0 ▶ $name = $ENV{PERL_ENCODING} unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
140 0 my $enc = find_encoding($name);
141 0 ▶ unless ( defined $enc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $enc) 0 0 0
142 0 require Carp;
143 0 Carp::croak("encoding: Unknown encoding '$name'");
144 }
145 0 $name = $enc->name; # canonize
146 0 ▶ unless ( $arg{Filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'Filter'}) { } 0 0 0
147 0 ▶ if ($] >= 5.025003 and !$Config{usecperl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.025003" and not $encoding::Config{"usecperl"}) 0 0 0
Condition0%0/3
$] >= "5.025003" and not $encoding::Config{"usecperl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$] >= "5.025003" and not $encoding::Config{"usecperl"}
$] >= "5.025003" not $encoding::Config{"usecperl"}
Truth table0%0/3
A and not(B)
A= $] >= "5.025003"B= not $encoding::Config{"usecperl"}
ABresult
0X0
100
111
148 0 require Carp;
149 0 Carp::croak("The encoding pragma is no longer supported. Check cperl");
150 }
151 0 ▶ warnings::warnif("deprecated",$deprecate) if $deprecate;
Branch: 1 branch
BranchTrueFalseTotal
if $deprecate 0 0 0
152  
153 0 DEBUG and warn "_exception($name) = ", _exception($name);
154 0 ▶ if (! _exception($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_exception($name)) 0 0 0
155 0 if (!PERL_5_21_7) {
156 ${^ENCODING} = $enc;
157 }
158 else {
159 # Starting with 5.21.7, this pragma uses a shadow variable
160 # designed explicitly for it, ${^E_NCODING}, to enforce
161 # lexical scope; instead of ${^ENCODING}.
162 0 $^H{'encoding'} = 1;
163 0 ${^E_NCODING} = $enc;
164 }
165 }
166 0 if (! HAS_PERLIO ) {
167 return 1;
168 }
169 }
170 else {
171 0 ▶ warnings::warnif("deprecated",$deprecate) if $deprecate;
Branch: 1 branch
BranchTrueFalseTotal
if $deprecate 0 0 0
172  
173 0 ▶ defined( ${^ENCODING} ) and undef ${^ENCODING};
Branch: 1 branch
BranchTrueFalseTotal
defined ${^ENCODING} and undef ${^ENCODING} 0 0 0
174 0 undef ${^E_NCODING} if PERL_5_21_7;
175  
176 # implicitly 'use utf8'
177 0 require utf8; # to fetch $utf8::hint_bits;
178 0 $^H |= $utf8::hint_bits;
179  
180 0 require Filter::Util::Call;
181 0 Filter::Util::Call->import;
182 filter_add(
183 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
184 0 my $status = filter_read();
185 0 ▶ if ( $status > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 0 0 0
186 0 $_ = $enc->decode( $_, 1 );
187 0 DEBUG and warn $_;
188 }
189 0 $status;
190 }
191 0 );
192 }
193 0 ▶ defined ${^UNICODE} and ${^UNICODE} != 0 and return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined ${^UNICODE} and ${^UNICODE} != 0 and return 1 0 0 0
Condition0%0/3
defined ${^UNICODE} and ${^UNICODE} != 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined ${^UNICODE} and ${^UNICODE} != 0
defined ${^UNICODE} ${^UNICODE} != 0
Truth table0%0/3
A and B
A= defined ${^UNICODE}B= ${^UNICODE} != 0
ABresult
0X0
100
111
194 0 for my $h (qw(STDIN STDOUT)) {
195 0 ▶ if ( $arg{$h} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{$h}) { } 0 0 0
196 0 ▶ unless ( defined find_encoding( $arg{$h} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &find_encoding($arg{$h})) 0 0 0
197 0 require Carp;
198 0 Carp::croak(
199 "encoding: Unknown encoding for $h, '$arg{$h}'");
200 }
201 0 binmode( $h, ":raw :encoding($arg{$h})" );
202 }
203 else {
204 0 ▶ unless ( exists $arg{$h} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $arg{$h}) 0 0 0
205 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
206 0 binmode( $h, ":raw :encoding($name)" );
207 }
208 }
209 }
210 0 return 1; # I doubt if we need it, though
211 }
212  
213 0 ▶sub unimport {
Subroutine unimport: not called CC=2 SCAR=13.4
214 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 0 undef ${^ENCODING};
216 0 undef ${^E_NCODING} if PERL_5_21_7;
217 0 if (HAS_PERLIO) {
218 0 binmode( STDIN, ":raw" );
219 0 binmode( STDOUT, ":raw" );
220 }
221 else {
222 binmode(STDIN);
223 binmode(STDOUT);
224 }
225 0 ▶ if ( $INC{"Filter/Util/Call.pm"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Filter/Util/Call.pm"}) 0 0 0
226 0 eval { filter_del() };
227 }
228 }
229  
230 2 1;
231 __END__
↑ Summary blib/lib/Encode/MIME/Header.pm »