Encode/Simple.pm

filter: Statementstmt 98.7% 161 / 163 Branchbran 64.8% 35 / 54 Conditioncond 34.8% 15 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 100% 26 / 26 Podpod 100% 8 / 8 totaltotal 76.5% 245 / 320 CC 41 SCAR 15.1
CC
41
Cov
81%
encode_lax
7.0
decode_lax
7.0
decode
6.1
CRAP 4.5
« Decode/ARGV.pm ↑ Summary
1 package Encode::Simple;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '1.001';
10  
11 3 our @EXPORT = qw(encode encode_utf8 decode decode_utf8);
12 3 our @EXPORT_OK = qw(encode_lax encode_utf8_lax decode_lax decode_utf8_lax);
13 3 our %EXPORT_TAGS = (
14 all => [@EXPORT, @EXPORT_OK],
15 strict => [qw(encode encode_utf8 decode decode_utf8)],
16 lax => [qw(encode_lax encode_utf8_lax decode_lax decode_utf8_lax)],
17 utf8 => [qw(encode_utf8 encode_utf8_lax decode_utf8 decode_utf8_lax)],
18 );
19  
20 3 ▶use constant HAS_UNICODE_UTF8 => do { local $@; !!eval { require Unicode::UTF8; 1 } };
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant MASK_STRICT => Encode::FB_CROAK | Encode::LEAVE_SRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant MASK_LAX => Encode::FB_DEFAULT | Encode::LEAVE_SRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 my %ENCODINGS;
25  
26 sub encode {
27 11 ▶ my ($encoding, $input) = @_;
Subroutine encode: called CC=6 SCAR=18.0
Pod documented
28 11 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 11 11
29 11 ▶ my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding);
Condition66%2/3
$ENCODINGS{$encoding} || _find_encoding($encoding)
l!l&&r!l&&!r
740
MC/DC0%0/2
$ENCODINGS{$encoding} || _find_encoding($encoding)
$ENCODINGS{$encoding} _find_encoding($encoding)
Truth table66%2/3
A || B
A= $ENCODINGS{$encoding}B= _find_encoding($encoding)
ABresult
1X1
011
000
30 11 my ($output, $error);
31 3 ▶ { local $@; use warnings FATAL => 'utf8'; # Encode::Unicode throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶ unless (eval { $output = $obj->encode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->encode("$input", 9); 1 } }) 4 7 11
Condition50%1/2
$@ || 'Error'
l!l
40
MC/DC0%0/1
$@ || 'Error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
33 }
34 11 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 4 7 11
35 7 return $output;
36 }
37  
38 sub encode_lax {
39 4 ▶ my ($encoding, $input) = @_;
Subroutine encode_lax: called CC=6 SCAR=19.5
Pod documented
40 4 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 4 4
41 4 ▶ my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding);
Condition33%1/3
$ENCODINGS{$encoding} || _find_encoding($encoding)
l!l&&r!l&&!r
400
MC/DC0%0/2
$ENCODINGS{$encoding} || _find_encoding($encoding)
$ENCODINGS{$encoding} _find_encoding($encoding)
Truth table33%1/3
A || B
A= $ENCODINGS{$encoding}B= _find_encoding($encoding)
ABresult
1X1
011
000
42 4 my ($output, $error);
43 3 ▶ { local $@; no warnings 'utf8'; # Encode::Unicode throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶ unless (eval { $output = $obj->encode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->encode("$input", 8); 1 } }) 0 4 4
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
45 }
46 4 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 4 4
47 4 return $output;
48 }
49  
50 sub encode_utf8 {
51 10 ▶ my ($input) = @_;
Subroutine encode_utf8: called CC=5 SCAR=16.2
Pod documented
52 10 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 10 10
53 10 my ($output, $error);
54 10 if (HAS_UNICODE_UTF8) {
55 3 ▶ local $@; use warnings FATAL => 'utf8'; # Unicode::UTF8 throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
56 unless (eval { $output = Unicode::UTF8::encode_utf8($input); 1 }) { $error = $@ || 'Error' }
57 } else {
58 10 ▶ my $obj = $ENCODINGS{'UTF-8'} || _find_encoding('UTF-8');
Condition66%2/3
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
l!l&&r!l&&!r
810
MC/DC0%0/2
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
$ENCODINGS{'UTF-8'} _find_encoding('UTF-8')
Truth table66%2/3
A || B
A= $ENCODINGS{'UTF-8'}B= _find_encoding('UTF-8')
ABresult
1X1
011
000
59 10 local $@;
60 10 ▶ unless (eval { $output = $obj->encode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->encode("$input", 9); 1 } }) 2 8 10
Condition50%1/2
$@ || 'Error'
l!l
10
MC/DC0%0/1
$@ || 'Error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
61 }
62 9 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 8 10
63 7 return $output;
64 }
65  
66 sub encode_utf8_lax {
67 4 ▶ my ($input) = @_;
Subroutine encode_utf8_lax: called CC=5 SCAR=17.0
Pod documented
68 4 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 4 4
69 4 my ($output, $error);
70 4 if (HAS_UNICODE_UTF8) {
71 3 ▶ local $@; no warnings 'utf8'; # Unicode::UTF8 throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
72 unless (eval { $output = Unicode::UTF8::encode_utf8($input); 1 }) { $error = $@ || 'Error' }
73 } else {
74 4 ▶ my $obj = $ENCODINGS{'UTF-8'} || _find_encoding('UTF-8');
Condition33%1/3
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
l!l&&r!l&&!r
200
MC/DC0%0/2
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
$ENCODINGS{'UTF-8'} _find_encoding('UTF-8')
Truth table33%1/3
A || B
A= $ENCODINGS{'UTF-8'}B= _find_encoding('UTF-8')
ABresult
1X1
011
000
75 4 local $@;
76 4 ▶ unless (eval { $output = $obj->encode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->encode("$input", 8); 1 } }) 0 4 4
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
77 }
78 4 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 4 4
79 2 return $output;
80 }
81  
82 sub decode {
83 6 ▶ my ($encoding, $input) = @_;
Subroutine decode: called CC=6 SCAR=18.1
Pod documented
84 6 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 6 6
85 6 ▶ my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding);
Condition33%1/3
$ENCODINGS{$encoding} || _find_encoding($encoding)
l!l&&r!l&&!r
600
MC/DC0%0/2
$ENCODINGS{$encoding} || _find_encoding($encoding)
$ENCODINGS{$encoding} _find_encoding($encoding)
Truth table33%1/3
A || B
A= $ENCODINGS{$encoding}B= _find_encoding($encoding)
ABresult
1X1
011
000
86 6 my ($output, $error);
87 3 ▶ { local $@; use warnings FATAL => 'utf8'; # Encode::Unicode throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
88 6 ▶ unless (eval { $output = $obj->decode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->decode("$input", 9); 1 } }) 2 4 6
Condition50%1/2
$@ || 'Error'
l!l
20
MC/DC0%0/1
$@ || 'Error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
89 }
90 6 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 4 6
91 4 return $output;
92 }
93  
94 sub decode_lax {
95 6 ▶ my ($encoding, $input) = @_;
Subroutine decode_lax: called CC=6 SCAR=19.5
Pod documented
96 6 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 6 6
97 6 ▶ my $obj = $ENCODINGS{$encoding} || _find_encoding($encoding);
Condition33%1/3
$ENCODINGS{$encoding} || _find_encoding($encoding)
l!l&&r!l&&!r
600
MC/DC0%0/2
$ENCODINGS{$encoding} || _find_encoding($encoding)
$ENCODINGS{$encoding} _find_encoding($encoding)
Truth table33%1/3
A || B
A= $ENCODINGS{$encoding}B= _find_encoding($encoding)
ABresult
1X1
011
000
98 6 my ($output, $error);
99 3 ▶ { local $@; no warnings 'utf8'; # Encode::Unicode throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
100 6 ▶ unless (eval { $output = $obj->decode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->decode("$input", 8); 1 } }) 0 6 6
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
101 }
102 6 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 6 6
103 6 return $output;
104 }
105  
106 sub decode_utf8 {
107 8 ▶ my ($input) = @_;
Subroutine decode_utf8: called CC=5 SCAR=16.3
Pod documented
108 8 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 8 8
109 8 my ($output, $error);
110 8 if (HAS_UNICODE_UTF8) {
111 3 ▶ local $@; use warnings FATAL => 'utf8'; # Unicode::UTF8 throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
112 unless (eval { $output = Unicode::UTF8::decode_utf8($input); 1 }) { $error = $@ || 'Error' }
113 } else {
114 8 ▶ my $obj = $ENCODINGS{'UTF-8'} || _find_encoding('UTF-8');
Condition33%1/3
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
l!l&&r!l&&!r
700
MC/DC0%0/2
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
$ENCODINGS{'UTF-8'} _find_encoding('UTF-8')
Truth table33%1/3
A || B
A= $ENCODINGS{'UTF-8'}B= _find_encoding('UTF-8')
ABresult
1X1
011
000
115 8 local $@;
116 8 ▶ unless (eval { $output = $obj->decode("$input", MASK_STRICT); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->decode("$input", 9); 1 } }) 3 5 8
Condition50%1/2
$@ || 'Error'
l!l
20
MC/DC0%0/1
$@ || 'Error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
117 }
118 7 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 3 5 8
119 4 return $output;
120 }
121  
122 sub decode_utf8_lax {
123 8 ▶ my ($input) = @_;
Subroutine decode_utf8_lax: called CC=5 SCAR=17.0
Pod documented
124 8 ▶ return undef unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 8 8
125 8 my ($output, $error);
126 8 if (HAS_UNICODE_UTF8) {
127 3 ▶ local $@; no warnings 'utf8'; # Unicode::UTF8 throws warnings in this category
Subroutine BEGIN: called CC=1 SCAR=0.0
128 unless (eval { $output = Unicode::UTF8::decode_utf8($input); 1 }) { $error = $@ || 'Error' }
129 } else {
130 8 ▶ my $obj = $ENCODINGS{'UTF-8'} || _find_encoding('UTF-8');
Condition33%1/3
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
l!l&&r!l&&!r
600
MC/DC0%0/2
$ENCODINGS{'UTF-8'} || _find_encoding('UTF-8')
$ENCODINGS{'UTF-8'} _find_encoding('UTF-8')
Truth table33%1/3
A || B
A= $ENCODINGS{'UTF-8'}B= _find_encoding('UTF-8')
ABresult
1X1
011
000
131 8 local $@;
132 8 ▶ unless (eval { $output = $obj->decode("$input", MASK_LAX); 1 }) { $error = $@ || 'Error' }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $output = $obj->decode("$input", 8); 1 } }) 0 8 8
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
133 }
134 8 ▶ _rethrow($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 8 8
135 6 return $output;
136 }
137  
138 sub _find_encoding {
139 5 ▶ my ($encoding) = @_;
Subroutine _find_encoding: called CC=3 SCAR=11.3
140 5 ▶ Carp::croak('Encoding name should not be undef') unless defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoding 0 5 5
141 5 my $obj = Encode::find_encoding($encoding);
142 5 ▶ Carp::croak("Unknown encoding '$encoding'") unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 5 5
143 5 return $ENCODINGS{$encoding} = $obj;
144 }
145  
146 sub _rethrow {
147 11 ▶ my ($error) = @_;
Subroutine _rethrow: called CC=3 SCAR=12.0
148 11 ▶ die $error if ref $error or $error =~ m/\n(?!\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $error or $error =~ /\n(?!\z)/ 0 11 11
Condition33%1/3
ref $error or $error =~ /\n(?!\z)/
l!l&&r!l&&!r
0011
MC/DC0%0/2
ref $error or $error =~ /\n(?!\z)/
ref $error $error =~ /\n(?!\z)/
Truth table33%1/3
A or B
A= ref $errorB= $error =~ /\n(?!\z)/
ABresult
1X1
011
000
149 11 $error =~ s/ at .+? line [0-9]+\.\n\z//;
150 11 Carp::croak($error);
151 }
152  
153 3 1;
154  
155 =head1 NAME
156  
157 Encode::Simple - Encode and decode text, simply
158  
159 =head1 SYNOPSIS
160  
161 use Encode::Simple qw(encode encode_lax encode_utf8 decode decode_lax decode_utf8);
162 my $bytes = encode 'Shift_JIS', $characters;
163 my $bytes = encode_lax 'ASCII', $characters;
164 my $bytes = encode_utf8 $characters;
165 my $characters = decode 'cp1252', $bytes;
166 my $characters = decode_lax 'UTF-8', $bytes;
167 my $characters = decode_utf8 $bytes;
168  
169 =head1 DESCRIPTION
170  
171 This module is a simple wrapper around L<Encode> that presents L</"encode"> and
172 L</"decode"> functions with straightforward behavior and error handling. See
173 L<Encode::Supported> for a list of supported encodings.
174  
175 =head1 FUNCTIONS
176  
177 All functions are exported by name, as well as via the tags C<:all>,
178 C<:strict>, C<:lax>, and C<:utf8>. By default, L</"encode">, L</"encode_utf8">,
179 L</"decode">, and L</"decode_utf8"> are exported as in L<Encode>.
180  
181 =head2 encode
182  
183 my $bytes = encode $encoding, $characters;
184  
185 Encodes the input string of characters into a byte string using C<$encoding>.
186 Throws an exception if the input string contains characters that are not valid
187 or possible to represent in C<$encoding>.
188  
189 =head2 encode_lax
190  
191 my $bytes = encode_lax $encoding, $characters;
192  
193 Encodes the input string of characters into a byte string using C<$encoding>,
194 encoding any invalid characters as a substitution character (the substitution
195 character used depends on the encoding). Note that some encoders do not respect
196 this option and may throw an exception anyway, this notably includes
197 L<Encode::Unicode> (but not UTF-8).
198  
199 =head2 encode_utf8
200  
201 my $bytes = encode_utf8 $characters;
202  
203 I<Since version 1.000.>
204  
205 Encodes the input string of characters into a UTF-8 byte string. Throws an
206 exception if the input string contains characters that are not valid or
207 possible to represent in UTF-8.
208  
209 This function will use the more consistent and efficient
210 L<Unicode::UTF8/"encode_utf8"> if installed, and is otherwise equivalent to
211 L</"encode"> with an encoding of C<UTF-8>. It is B<not> equivalent to
212 L<Encode/"encode_utf8">, which should be avoided.
213  
214 =head2 encode_utf8_lax
215  
216 my $bytes = encode_utf8_lax $characters;
217  
218 I<Since version 1.000.>
219  
220 Encodes the input string of characters into a UTF-8 byte string, encoding any
221 invalid characters as the Unicode replacement character C<U+FFFD>, represented
222 in UTF-8 as the three bytes C<0xEFBFBD>.
223  
224 This function will use the more consistent and efficient
225 L<Unicode::UTF8/"encode_utf8"> if installed, and is otherwise equivalent to
226 L</"encode_lax"> with an encoding of C<UTF-8>. It is B<not> equivalent to
227 L<Encode/"encode_utf8">, which should be avoided.
228  
229 =head2 decode
230  
231 my $characters = decode $encoding, $bytes;
232  
233 Decodes the input byte string into a string of characters using C<$encoding>.
234 Throws an exception if the input bytes are not valid for C<$encoding>.
235  
236 =head2 decode_lax
237  
238 my $characters = decode_lax $encoding, $bytes;
239  
240 Decodes the input byte string into a string of characters using C<$encoding>,
241 decoding any malformed bytes to the Unicode replacement character (U+FFFD).
242 Note that some encoders do not respect this option and may throw an exception
243 anyway, this notably includes L<Encode::Unicode> (but not UTF-8).
244  
245 =head2 decode_utf8
246  
247 my $characters = decode_utf8 $bytes;
248  
249 I<Since version 1.000.>
250  
251 Decodes the input UTF-8 byte string into a string of characters. Throws an
252 exception if the input bytes are not valid for UTF-8.
253  
254 This function will use the more consistent and efficient
255 L<Unicode::UTF8/"decode_utf8"> if installed, and is otherwise equivalent to
256 L</"decode"> with an encoding of C<UTF-8>. It is B<not> equivalent to
257 L<Encode/"decode_utf8">, which should be avoided.
258  
259 =head2 decode_utf8_lax
260  
261 my $characters = decode_utf8_lax $bytes;
262  
263 I<Since version 1.000.>
264  
265 Decodes the input UTF-8 byte string into a string of characters, decoding any
266 malformed bytes to the Unicode replacement character C<U+FFFD>.
267  
268 This function will use the more consistent and efficient
269 L<Unicode::UTF8/"decode_utf8"> if installed, and is otherwise equivalent to
270 L</"decode_lax"> with an encoding of C<UTF-8>. It is B<not> equivalent to
271 L<Encode/"decode_utf8">, which should be avoided.
272  
273 =head1 BUGS
274  
275 Report any issues on the public bugtracker.
276  
277 =head1 AUTHOR
278  
279 Dan Book <dbook@cpan.org>
280  
281 =head1 COPYRIGHT AND LICENSE
282  
283 This software is Copyright (c) 2018 by Dan Book.
284  
285 This is free software, licensed under:
286  
287 The Artistic License 2.0 (GPL Compatible)
288  
289 =head1 SEE ALSO
290  
291 L<Unicode::UTF8>
« Decode/ARGV.pm ↑ Summary