Decode/ARGV.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 8 / 8 Conditioncond 83.3% 15 / 18 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 91.1% 62 / 68 CC 11 SCAR 21.8
CC
11
Cov
94%
import
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.9
↑ Summary Encode/Simple.pm »
1 package Decode::ARGV;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode::Simple qw(decode decode_lax decode_utf8 decode_utf8_lax);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.001';
8  
9 sub import {
10 8 ▶ my ($class, $mode, $encoding) = @_;
Subroutine import: called CC=11 SCAR=24.0
11 8 ▶ if (defined $mode and !defined $encoding and $mode ne 'strict' and $mode ne 'lax') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode and not defined $encoding and $mode ne "strict" and $mode ne "lax") 2 6 8
Condition88%8/9
defined $mode and not defined $encoding and $mode ne "strict" and $mode ne "lax"
!ll&&!rl&&r
422
defined $mode and not defined $encoding and $mode ne "strict"
!ll&&!rl&&r
404
defined $mode and not defined $encoding
!ll&&!rl&&r
224
MC/DC75%3/4
defined $mode and not defined $encoding and $mode ne "strict" and $mode ne "lax"
defined $mode not defined $encoding $mode ne "strict" $mode ne "lax"
Truth table80%4/5
A and not(B) and C and D
A= defined $modeB= not defined $encodingC= $mode ne "strict"D= $mode ne "lax"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
12 2 $encoding = $mode;
13 }
14 8 my @args;
15 8 ▶ if (defined $mode and $mode eq 'lax') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode and $mode eq 'lax') { } 4 4 8
Condition100%3/3
defined $mode and $mode eq "lax"
!ll&&!rl&&r
224
MC/DC100%2/2
defined $mode and $mode eq "lax"
defined $mode $mode eq "lax"
Truth table100%3/3
A and B
A= defined $modeB= $mode eq "lax"
ABresult
0X0
100
111
16 4 ▶ if (!defined $encoding or lc($encoding) eq 'utf-8') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $encoding or lc $encoding eq 'utf-8') { } 2 2 4
Condition66%2/3
not defined $encoding or lc $encoding eq "utf-8"
l!l&&r!l&&!r
202
MC/DC50%1/2
not defined $encoding or lc $encoding eq "utf-8"
not defined $encoding lc $encoding eq "utf-8"
Truth table66%2/3
not(A) or B
A= not defined $encodingB= lc $encoding eq "utf-8"
ABresult
1X1
011
000
17 2 @args = map { decode_utf8_lax $_ } @ARGV;
18 } else {
19 2 @args = map { decode_lax $encoding, $_ } @ARGV;
20 }
21 } else {
22 4 ▶ if (!defined $encoding or lc($encoding) eq 'utf-8') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $encoding or lc $encoding eq 'utf-8') { } 2 2 4
Condition66%2/3
not defined $encoding or lc $encoding eq "utf-8"
l!l&&r!l&&!r
202
MC/DC50%1/2
not defined $encoding or lc $encoding eq "utf-8"
not defined $encoding lc $encoding eq "utf-8"
Truth table66%2/3
not(A) or B
A= not defined $encodingB= lc $encoding eq "utf-8"
ABresult
1X1
011
000
23 2 @args = map { decode_utf8 $_ } @ARGV;
24 } else {
25 2 @args = map { decode $encoding, $_ } @ARGV;
26 }
27 }
28 # only munge @ARGV if we got this far
29 6 @ARGV = @args;
30 6 1;
31 }
32  
33 1 1;
34  
35 =encoding UTF-8
36  
37 =head1 NAME
38  
39 Decode::ARGV - Decode the command-line arguments to characters
40  
41 =head1 SYNOPSIS
42  
43 use Decode::ARGV; # decodes from UTF-8
44  
45 use Decode::ARGV 'cp1252';
46  
47 $ perl -MDecode::ARGV -E'say "Argument contains only word characters" unless $ARGV[0] =~ m/\W/' 'слово'
48  
49 =head1 DESCRIPTION
50  
51 This module provides simple in-place decoding of command-line arguments in the global array
52 L<@ARGV|perlvar/"@ARGV">. As with most input and output, command-line arguments are provided to the
53 script in bytes, and must be decoded to characters before performing string operations like
54 C<length> or regex matches.
55  
56 The C<-CA> switch for Perl performs a similar function, but this has some deficiencies. It assumes
57 via the C<:utf8> internal layer that the arguments are valid UTF-8 bytes, ignoring invalid Unicode,
58 and even resulting in malformed strings if the bytes do not happen to be well-formed UTF-8. This
59 switch is also difficult to use in a script and cannot decode the arguments from other encodings.
60  
61 =head1 PARAMETERS
62  
63 use Decode::ARGV;
64 use Decode::ARGV 'lax';
65 use Decode::ARGV 'Shift_JIS';
66 use Decode::ARGV lax => 'Shift_JIS';
67  
68 $ perl -MDecode::ARGV ...
69 $ perl -MDecode::ARGV=lax ...
70 $ perl -MDecode::ARGV=Shift_JIS ...
71 $ perl -MDecode::ARGV=lax,Shift_JIS ...
72  
73 By default, C<Decode::ARGV> will decode C<@ARGV> in-place using L<Encode::Simple/decode_utf8>,
74 which will throw an exception if any argument doesn't contain valid well-formed UTF-8 bytes.
75  
76 C<lax> can be specified as the first (optional) import parameter to instead use
77 L<Encode::Simple/decode_utf8_lax>, leaving replacement characters in the resulting strings instead
78 of throwing an exception.
79  
80 The next optional import parameter specifies an alternate encoding to expect from the command-line,
81 in which case L<Encode::Simple/decode> or L<Encode::Simple/decode_lax> will be used to decode the
82 arguments from that encoding.
83  
84 =head1 BUGS
85  
86 Report any issues on the public bugtracker.
87  
88 =head1 AUTHOR
89  
90 Dan Book <dbook@cpan.org>
91  
92 =head1 COPYRIGHT AND LICENSE
93  
94 This software is Copyright (c) 2021 by Dan Book.
95  
96 This is free software, licensed under:
97  
98 The Artistic License 2.0 (GPL Compatible)
99  
100 =head1 SEE ALSO
101  
102 L<Encode::Simple>
↑ Summary Encode/Simple.pm »