| 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.002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition88%8/9
defined $mode and not defined $encoding and $mode ne "strict" and $mode ne "lax"
defined $mode and not defined $encoding and $mode ne "strict"
defined $mode and not defined $encoding
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | $encoding = $mode; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | my @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | if (defined $mode and $mode eq 'lax') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $mode and $mode eq "lax"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | if (!defined $encoding or lc($encoding) eq 'utf-8') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $encoding or lc $encoding eq "utf-8"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $encoding or lc $encoding eq "utf-8"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||