blib/lib/Encode.pm

filter: Statementstmt 76.0% 114 / 150 Branchbran 55.3% 31 / 56 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 80.7% 21 / 26 Podpod 30.0% 3 / 10 totaltotal 69.6% 181 / 260 CC 32 SCAR 19.4
CC
32
Cov
71%
getEncoding
13.2
decode
12.0
encode
12.0
CRAP 7.0
« blib/lib/Encode/Unicode/UTF7.pm ↑ Summary blib/lib/Encode/JP/H2Z.pm »
1 #
2 # $Id: Encode.pm,v 3.23 2026/04/27 02:52:33 dankogai Exp $
3 #
4 package Encode;
5 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 53 ▶use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 53 our $VERSION;
9 BEGIN {
10 53 ▶ $VERSION = sprintf "%d.%02d", q$Revision: 3.23 $ =~ /(\d+)/g;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 53 require XSLoader;
12 53 XSLoader::load( __PACKAGE__, $VERSION );
13 }
14  
15 53 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 53 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 53 our @CARP_NOT = qw(Encode::Encoder);
19  
20 # Public, encouraged API is exported by default
21  
22 53 our @EXPORT = qw(
23 decode decode_utf8 encode encode_utf8 str2bytes bytes2str
24 encodings find_encoding find_mime_encoding clone_encoding
25 );
26 53 our @FB_FLAGS = qw(
27 DIE_ON_ERR WARN_ON_ERR RETURN_ON_ERR LEAVE_SRC
28 PERLQQ HTMLCREF XMLCREF STOP_AT_PARTIAL
29 );
30 53 our @FB_CONSTS = qw(
31 FB_DEFAULT FB_CROAK FB_QUIET FB_WARN
32 FB_PERLQQ FB_HTMLCREF FB_XMLCREF
33 );
34 53 our @EXPORT_OK = (
35 qw(
36 _utf8_off _utf8_on define_encoding from_to is_16bit is_8bit
37 is_utf8 perlio_ok resolve_alias utf8_downgrade utf8_upgrade
38 ),
39 @FB_FLAGS, @FB_CONSTS,
40 );
41  
42 53 our %EXPORT_TAGS = (
43 all => [ @EXPORT, @EXPORT_OK ],
44 default => [ @EXPORT ],
45 fallbacks => [ @FB_CONSTS ],
46 fallback_all => [ @FB_CONSTS, @FB_FLAGS ],
47 );
48  
49 # Documentation moved after __END__ for speed - NI-S
50  
51 53 our $ON_EBCDIC = ( ord("A") == 193 );
52  
53 53 ▶use Encode::Alias ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 53 ▶use Encode::MIME::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 53 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # Make a %Encoding package variable to allow a certain amount of cheating
59 53 our %Encoding;
60 53 our %ExtModule;
61 53 require Encode::Config;
62 # See
63 # https://bugzilla.redhat.com/show_bug.cgi?id=435505#c2
64 # to find why sig handlers inside eval{} are disabled.
65 53 eval {
66 53 local $SIG{__DIE__};
67 53 local $SIG{__WARN__};
68 53 local @INC = @INC;
69 53 ▶ pop @INC if @INC && $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if @INC and $INC[-1] eq "." 45 8 53
Condition66%2/3
@INC and $INC[-1] eq "."
!ll&&!rl&&r
0845
MC/DC50%1/2
@INC and $INC[-1] eq "."
@INC $INC[-1] eq "."
Truth table66%2/3
A and B
A= @INCB= $INC[-1] eq "."
ABresult
0X0
100
111
70 53 require Encode::ConfigLocal;
71 };
72  
73 sub encodings {
74 4 ▶ my %enc;
Subroutine encodings: called CC=7 SCAR=22.7
Pod documented
75 4 ▶ my $arg = $_[1] || '';
Condition100%2/2
$_[1] || ''
l!l
31
MC/DC100%1/1
$_[1] || ''
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
76 4 ▶ if ( $arg eq ":all" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq ':all') { } 3 1 4
77 3 %enc = ( %Encoding, %ExtModule );
78 }
79 else {
80 1 %enc = %Encoding;
81 1 ▶ for my $mod ( map { m/::/ ? $_ : "Encode::$_" } @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
/::/ ? : 0 0 0
82 0 DEBUG and warn $mod;
83 0 for my $enc ( keys %ExtModule ) {
84 0 ▶ $ExtModule{$enc} eq $mod and $enc{$enc} = $mod;
Branch: 1 branch
BranchTrueFalseTotal
$Encode::ExtModule{$enc} eq $mod and $enc{$enc} = $mod 0 0 0
85 }
86 }
87 }
88 2152 return sort { lc $a cmp lc $b }
89 4 grep { !/^(?:Internal|Unicode|Guess)$/o } keys %enc;
90 }
91  
92 sub perlio_ok {
93 16 ▶ my $obj = ref( $_[0] ) ? $_[0] : find_encoding( $_[0] );
Subroutine perlio_ok: called CC=3 SCAR=13.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 16 16
94 16 ▶ $obj->can("perlio_ok") and return $obj->perlio_ok();
Branch: 1 branch
BranchTrueFalseTotal
$obj->can("perlio_ok") and return $obj->perlio_ok 16 0 16
95 0 return 0; # safety net
96 }
97  
98 sub define_encoding {
99 1313 ▶ my $obj = shift;
Subroutine define_encoding: called CC=5 SCAR=16.2
Pod undocumented
100 1313 my $name = shift;
101 1313 $Encoding{$name} = $obj;
102 1313 my $lc = lc($name);
103 1313 ▶ define_alias( $lc => $obj ) unless $lc eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $lc eq $name 305 1008 1313
104 1313 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1313 1313
105 0 my $alias = shift;
106 0 define_alias( $alias, $obj );
107 }
108 1313 my $class = ref($obj);
109 1313 ▶ push @Encode::CARP_NOT, $class unless grep { $_ eq $class } @Encode::CARP_NOT;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $class;} @Encode::CARP_NOT 166 1147 1313
110 1313 ▶ push @Encode::Encoding::CARP_NOT, $class unless grep { $_ eq $class } @Encode::Encoding::CARP_NOT;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $class;} @Encode::Encoding::CARP_NOT 166 1147 1313
111 1313 return $obj;
112 }
113  
114 sub getEncoding {
115 62665 ▶ my ( $class, $name, $skip_external ) = @_;
Subroutine getEncoding: called CC=13 SCAR=25.8
Pod undocumented
116  
117 62665 ▶ defined($name) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $name or return 19 62646 62665
118  
119 62646 $name =~ s/\s+//g; # https://rt.cpan.org/Ticket/Display.html?id=65796
120  
121 62646 ▶ ref($name) && $name->can('renew') and return $name;
Branch: 1 branch
BranchTrueFalseTotal
ref $name and $name->can("renew") and return $name 12 62634 62646
Condition66%2/3
ref $name and $name->can("renew")
!ll&&!rl&&r
62634012
MC/DC50%1/2
ref $name and $name->can("renew")
ref $name $name->can("renew")
Truth table66%2/3
A and B
A= ref $nameB= $name->can("renew")
ABresult
0X0
100
111
122 62634 ▶ exists $Encoding{$name} and return $Encoding{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Encoding{$name} and return $Encode::Encoding{$name} 58952 3682 62634
123 3682 my $lc = lc $name;
124 3682 ▶ exists $Encoding{$lc} and return $Encoding{$lc};
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Encoding{$lc} and return $Encode::Encoding{$lc} 54 3628 3682
125  
126 3628 my $oc = $class->find_alias($name);
127 3628 ▶ defined($oc) and return $oc;
Branch: 1 branch
BranchTrueFalseTotal
defined $oc and return $oc 3188 440 3628
128 440 ▶ $lc ne $name and $oc = $class->find_alias($lc);
Branch: 1 branch
BranchTrueFalseTotal
$lc ne $name and $oc = $class->find_alias($lc) 274 166 440
129 440 ▶ defined($oc) and return $oc;
Branch: 1 branch
BranchTrueFalseTotal
defined $oc and return $oc 0 440 440
130  
131 440 ▶ unless ($skip_external) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_external) 440 0 440
132 440 ▶ if ( my $mod = $ExtModule{$name} || $ExtModule{$lc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mod = $Encode::ExtModule{$name} || $Encode::ExtModule{$lc}) 77 363 440
Condition66%2/3
$Encode::ExtModule{$name} || $Encode::ExtModule{$lc}
l!l&&r!l&&!r
770363
MC/DC50%1/2
$Encode::ExtModule{$name} || $Encode::ExtModule{$lc}
$Encode::ExtModule{$name} $Encode::ExtModule{$lc}
Truth table66%2/3
A || B
A= $Encode::ExtModule{$name}B= $Encode::ExtModule{$lc}
ABresult
1X1
011
000
133 77 $mod =~ s,::,/,g;
134 77 $mod .= '.pm';
135 77 eval { require $mod; };
136 77 ▶ exists $Encoding{$name} and return $Encoding{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $Encode::Encoding{$name} and return $Encode::Encoding{$name} 77 0 77
137 }
138 }
139 363 return;
140 }
141  
142 # HACK: These two functions must be defined in Encode and because of
143 # cyclic dependency between Encode and Encode::Alias, Exporter does not work
144 sub find_alias {
145 3902 ▶ goto &Encode::Alias::find_alias;
Subroutine find_alias: called CC=1 SCAR=0.0
Pod undocumented
146 }
147 sub define_alias {
148 305 ▶ goto &Encode::Alias::define_alias;
Subroutine define_alias: called CC=1 SCAR=0.0
Pod undocumented
149 }
150  
151 sub find_encoding($;$) {
152 62656 ▶ my ( $name, $skip_external ) = @_;
Subroutine find_encoding: called CC=1 SCAR=0.0
Pod documented
153 62656 return __PACKAGE__->getEncoding( $name, $skip_external );
154 }
155  
156 sub find_mime_encoding($;$) {
157 50358 ▶ my ( $mime_name, $skip_external ) = @_;
Subroutine find_mime_encoding: called CC=1 SCAR=0.0
Pod documented
158 50358 my $name = Encode::MIME::Name::get_encode_name( $mime_name );
159 50358 return find_encoding( $name, $skip_external );
160 }
161  
162 sub resolve_alias($) {
163 0 ▶ my $obj = find_encoding(shift);
Subroutine resolve_alias: not called CC=2 SCAR=17.9
Pod undocumented
164 0 ▶ defined $obj and return $obj->name;
Branch: 1 branch
BranchTrueFalseTotal
defined $obj and return $obj->name 0 0 0
165 0 return;
166 }
167  
168 sub clone_encoding($) {
169 0 ▶ my $obj = find_encoding(shift);
Subroutine clone_encoding: not called CC=2 SCAR=17.9
Pod undocumented
170 0 ▶ ref $obj or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj or return 0 0 0
171 0 return Storable::dclone($obj);
172 }
173  
174 53 onBOOT;
175  
176 53 ▶if ($ON_EBCDIC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Encode::ON_EBCDIC) 0 53 53
177 package Encode::UTF_EBCDIC;
178 53 ▶ use parent 'Encode::Encoding';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 0 my $obj = bless { Name => "UTF_EBCDIC" } => "Encode::UTF_EBCDIC";
180 0 Encode::define_encoding($obj, 'Unicode');
181 sub decode {
182 0 ▶ my ( undef, $str, $chk ) = @_;
Subroutine decode: not called CC=3 SCAR=24.8
183 0 my $res = '';
184 0 ▶ for ( my $i = 0 ; $i < length($str) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
185 0 $res .=
186 chr(
187 utf8::unicode_to_native( ord( substr( $str, $i, 1 ) ) )
188 );
189 }
190 0 ▶ $_[1] = '' if $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $chk 0 0 0
191 0 return $res;
192 }
193 sub encode {
194 0 ▶ my ( undef, $str, $chk ) = @_;
Subroutine encode: not called CC=3 SCAR=24.8
195 0 my $res = '';
196 0 ▶ for ( my $i = 0 ; $i < length($str) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
197 0 $res .=
198 chr(
199 utf8::native_to_unicode( ord( substr( $str, $i, 1 ) ) )
200 );
201 }
202 0 ▶ $_[1] = '' if $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $chk 0 0 0
203 0 return $res;
204 }
205 }
206  
207 {
208 # https://rt.cpan.org/Public/Bug/Display.html?id=103253
209 package Encode::XS;
210 53 ▶ use parent 'Encode::Encoding';
Subroutine BEGIN: called CC=1 SCAR=0.0
211 }
212  
213 {
214 53 package Encode::utf8;
215 53 ▶ use parent 'Encode::Encoding';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 53 my %obj = (
217 'utf8' => { Name => 'utf8' },
218 'utf-8-strict' => { Name => 'utf-8-strict', strict_utf8 => 1 }
219 );
220 53 for ( keys %obj ) {
221 106 bless $obj{$_} => __PACKAGE__;
222 106 Encode::define_encoding( $obj{$_} => $_ );
223 }
224 sub cat_decode {
225 # ($obj, $dst, $src, $pos, $trm, $chk)
226 # currently ignores $chk
227 0 ▶ my ( undef, undef, undef, $pos, $trm ) = @_;
Subroutine cat_decode: not called CC=2 SCAR=13.7
228 0 my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ];
229 53 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 0 ▶ if ( ( my $npos = index( $$rsrc, $trm, $pos ) ) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $npos = index($$rsrc, $trm, $pos)) >= 0) 0 0 0
231 0 $$rdst .=
232 substr( $$rsrc, $pos, $npos - $pos + length($trm) );
233 0 $$rpos = $npos + length($trm);
234 0 return 1;
235 }
236 0 $$rdst .= substr( $$rsrc, $pos );
237 0 $$rpos = length($$rsrc);
238 0 return '';
239 }
240 }
241  
242 53 1;
243  
244 __END__
« blib/lib/Encode/Unicode/UTF7.pm ↑ Summary blib/lib/Encode/JP/H2Z.pm »