File Coverage

blib/lib/Decode/ARGV.pm
Criterion Covered Total %
statement 26 26 100.0
branch 8 8 100.0
condition 15 18 83.3
subroutine 4 4 100.0
pod n/a
total 53 56 94.6


line stmt bran cond sub pod time code
1             package Decode::ARGV;
2              
3 1     1   136075 use strict;
  1         3  
  1         42  
4 1     1   13 use warnings;
  1         3  
  1         69  
5 1     1   13 use Encode::Simple qw(decode decode_lax decode_utf8 decode_utf8_lax);
  1         3  
  1         388  
6              
7             our $VERSION = '1.003';
8              
9             sub import {
10 8     8   8205 my ($class, $mode, $encoding) = @_;
11 8 100 100     98 if (defined $mode and !defined $encoding and $mode ne 'strict' and $mode ne 'lax') {
      66        
      100        
12 2         5 $encoding = $mode;
13             }
14 8         16 my @args;
15 8 100 100     45 if (defined $mode and $mode eq 'lax') {
16 4 100 66     19 if (!defined $encoding or lc($encoding) eq 'utf-8') {
17 2         6 @args = map { decode_utf8_lax $_ } @ARGV;
  4         15  
18             } else {
19 2         7 @args = map { decode_lax $encoding, $_ } @ARGV;
  3         13  
20             }
21             } else {
22 4 100 66     16 if (!defined $encoding or lc($encoding) eq 'utf-8') {
23 2         6 @args = map { decode_utf8 $_ } @ARGV;
  4         13  
24             } else {
25 2         7 @args = map { decode $encoding, $_ } @ARGV;
  4         14  
26             }
27             }
28             # only munge @ARGV if we got this far
29 6         21 @ARGV = @args;
30 6         20 1;
31             }
32              
33             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 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 will decode C<@ARGV> in-place using L,
74             which will throw an exception if any argument doesn't contain valid well-formed UTF-8 bytes.
75              
76             C can be specified as the first (optional) import parameter to instead use
77             L, 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 or L 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
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