bin/piconv

filter: Statementstmt 68.0% 66 / 97 Branchbran 43.7% 28 / 64 Conditioncond 33.9% 18 / 53 MC/DCmcdc 3.5% 1 / 28 Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 48.0% 121 / 252 CC 4 SCAR 7.8
CC
4
Cov
52%
resolve_encoding
6.0
BEGIN
2.1
help
2.0
CRAP 2.2
« blib/lib/Encode/Encoder.pm ↑ Summary blib/lib/Encode/Encoding.pm »
1 #!./perl
2 # $Id: piconv,v 2.8 2016/08/04 03:15:58 dankogai Exp $
3 #
4 3 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 3 0 3
5 3 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Encode ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Encode::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 my %Scheme = map {$_ => 1} qw(from_to decode_encode perlio);
10  
11 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 my $name = basename($0);
13  
14 3 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 my %Opt;
17  
18 3 ▶help()
Branch: 1 branch
BranchTrueFalseTotal
unless &GetOptions(\%Opt, "from|f=s", "to|t=s", "list|l", "string|s=s", "check|C=i", "c", "perlqq|p", "htmlcref", "xmlcref", "debug|D", "scheme|S=s", "resolve|r=s", "help") 0 3 3
19 unless
20 GetOptions(\%Opt,
21 'from|f=s',
22 'to|t=s',
23 'list|l',
24 'string|s=s',
25 'check|C=i',
26 'c',
27 'perlqq|p',
28 'htmlcref',
29 'xmlcref',
30 'debug|D',
31 'scheme|S=s',
32 'resolve|r=s',
33 'help',
34 );
35  
36 3 ▶$Opt{help} and help();
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"help"} and help() 0 3 3
37 3 ▶$Opt{list} and list_encodings();
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"list"} and list_encodings() 0 3 3
38 3 ▶my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
Condition33%2/6
$ENV{'LC_CTYPE'} || $ENV{'LC_ALL'} || $ENV{'LANG'}
l!l&&r!l&&!r
003
$ENV{"LC_CTYPE"} or $ENV{"LC_ALL"}
l!l&&r!l&&!r
003
MC/DC0%0/3
$ENV{'LC_CTYPE'} || $ENV{'LC_ALL'} || $ENV{'LANG'}
$ENV{"LC_CTYPE"} $ENV{"LC_ALL"} $ENV{'LANG'}
Truth table25%1/4
A or B || C
A= $ENV{"LC_CTYPE"}B= $ENV{"LC_ALL"}C= $ENV{'LANG'}
ABCresult
1XX1
01X1
0011
0000
39 3 ▶defined $Opt{resolve} and resolve_encoding($Opt{resolve});
Branch: 1 branch
BranchTrueFalseTotal
defined $Opt{"resolve"} and resolve_encoding($Opt{"resolve"}) 0 3 3
40 3 ▶$Opt{from} || $Opt{to} || help();
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"from"} or $Opt{"to"} or help() 2 1 3
Condition66%2/3
$Opt{"from"} or $Opt{"to"}
l!l&&r!l&&!r
201
MC/DC50%1/2
$Opt{"from"} or $Opt{"to"}
$Opt{"from"} $Opt{"to"}
Truth table66%2/3
A or B
A= $Opt{"from"}B= $Opt{"to"}
ABresult
1X1
011
000
41 2 ▶my $from = $Opt{from} || $locale or help("from_encoding unspecified");
Branch: 1 branch
BranchTrueFalseTotal
my $from = $Opt{"from"} || $locale or help("from_encoding unspecified") 2 0 2
Condition33%1/3
$Opt{'from'} || $locale
l!l&&r!l&&!r
200
MC/DC0%0/2
$Opt{'from'} || $locale
$Opt{'from'} $locale
Truth table33%1/3
A || B
A= $Opt{'from'}B= $locale
ABresult
1X1
011
000
42 2 ▶my $to = $Opt{to} || $locale or help("to_encoding unspecified");
Branch: 1 branch
BranchTrueFalseTotal
my $to = $Opt{"to"} || $locale or help("to_encoding unspecified") 2 0 2
Condition33%1/3
$Opt{'to'} || $locale
l!l&&r!l&&!r
200
MC/DC0%0/2
$Opt{'to'} || $locale
$Opt{'to'} $locale
Truth table33%1/3
A || B
A= $Opt{'to'}B= $locale
ABresult
1X1
011
000
43 2 ▶$Opt{string} and Encode::from_to($Opt{string}, $from, $to) and print $Opt{string} and exit;
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"string"} and &Encode::from_to($Opt{"string"}, $from, $to) and print $Opt{"string"} and exit 0 2 2
Condition33%2/6
$Opt{"string"} and &Encode::from_to($Opt{"string"}, $from, $to) and print $Opt{"string"}
!ll&&!rl&&r
200
$Opt{"string"} and &Encode::from_to($Opt{"string"}, $from, $to)
!ll&&!rl&&r
200
MC/DC0%0/3
$Opt{"string"} and &Encode::from_to($Opt{"string"}, $from, $to) and print $Opt{"string"}
$Opt{"string"} &Encode::from_to($Opt{"string"}, $from, $to) print $Opt{"string"}
Truth table25%1/4
A and B and C
A= $Opt{"string"}B= &Encode::from_to($Opt{"string"}, $from, $to)C= print $Opt{"string"}
ABCresult
0XX0
10X0
1100
1111
44 2 my $scheme = do {
45 2 ▶ if (defined $Opt{scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Opt{'scheme'}) { } 1 1 2
46 1 ▶ if (!exists $Scheme{$Opt{scheme}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Scheme{$Opt{'scheme'}}) { } 1 0 1
47 1 warn "Unknown scheme '$Opt{scheme}', fallback to 'from_to'.\n";
48 1 'from_to';
49 } else {
50 0 $Opt{scheme};
51 }
52 } else {
53 1 'from_to';
54 }
55 };
56  
57 2 ▶$Opt{check} ||= $Opt{c};
Condition33%1/3
$Opt{"check"} ||= $Opt{"c"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$Opt{"check"} ||= $Opt{"c"}
$Opt{"check"} $Opt{"c"}
Truth table33%1/3
A ||= B
A= $Opt{"check"}B= $Opt{"c"}
ABresult
1X1
011
000
58 2 ▶$Opt{perlqq} and $Opt{check} = Encode::PERLQQ;
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"perlqq"} and $Opt{"check"} = 256 0 2 2
59 2 ▶$Opt{htmlcref} and $Opt{check} = Encode::HTMLCREF;
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"htmlcref"} and $Opt{"check"} = 512 0 2 2
60 2 ▶$Opt{xmlcref} and $Opt{check} = Encode::XMLCREF;
Branch: 1 branch
BranchTrueFalseTotal
$Opt{"xmlcref"} and $Opt{"check"} = 1024 0 2 2
61  
62 2 ▶my $efrom = Encode->getEncoding($from) || die "Unknown encoding '$from'";
Condition50%1/2
'Encode'->getEncoding($from) || die("Unknown encoding '${from}'")
l!l
20
MC/DC0%0/1
'Encode'->getEncoding($from) || die("Unknown encoding '${from}'")
'Encode'->getEncoding($from)
Truth table50%1/2
A
A= 'Encode'->getEncoding($from)
Aresult
11
00
63 2 ▶my $eto = Encode->getEncoding($to) || die "Unknown encoding '$to'";
Condition50%1/2
'Encode'->getEncoding($to) || die("Unknown encoding '${to}'")
l!l
20
MC/DC0%0/1
'Encode'->getEncoding($to) || die("Unknown encoding '${to}'")
'Encode'->getEncoding($to)
Truth table50%1/2
A
A= 'Encode'->getEncoding($to)
Aresult
11
00
64  
65 2 my $cfrom = $efrom->name;
66 2 my $cto = $eto->name;
67  
68 2 ▶if ($Opt{debug}){
Branch: 1 branch
BranchTrueFalseTotal
if ($Opt{"debug"}) 0 2 2
69 0 print <<"EOT";
70 Scheme: $scheme
71 From: $from => $cfrom
72 To: $to => $cto
73 EOT
74 }
75  
76 my %use_bom =
77 2 map { $_ => 1 } qw/UTF-16 UTF-16BE UTF-16LE UTF-32 UTF-32BE UTF-32LE/;
78  
79 # we do not use <> (or ARGV) for the sake of binmode()
80 2 ▶@ARGV or push @ARGV, \*STDIN;
Branch: 1 branch
BranchTrueFalseTotal
@ARGV or push @ARGV, \*STDIN 2 0 2
81  
82 2 ▶unless ( $scheme eq 'perlio' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'perlio') { } 0 2 2
83 2 binmode STDOUT;
84 2 ▶ my $need2slurp = $use_bom{ $eto } || $use_bom{ $efrom };
Condition33%1/3
$use_bom{$eto} || $use_bom{$efrom}
l!l&&r!l&&!r
002
MC/DC0%0/2
$use_bom{$eto} || $use_bom{$efrom}
$use_bom{$eto} $use_bom{$efrom}
Truth table33%1/3
A || B
A= $use_bom{$eto}B= $use_bom{$efrom}
ABresult
1X1
011
000
85 2 for my $argv (@ARGV) {
86 2 ▶ my $ifh = ref $argv ? $argv : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $argv ? : 0 2 2
87 2 ▶ $ifh or open $ifh, "<", $argv or warn "Can't open $argv: $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
$ifh or open $ifh, "<", $argv or warn "Can't open ${argv}: $!" and next 1 1 2
Condition62%5/8
$ifh || open($ifh, '<', $argv) || warn("Can't open ${argv}: $!") && (next)
l!l&&r!l&&!r
110
$ifh or open $ifh, "<", $argv
l!l&&r!l&&!r
011
warn "Can't open ${argv}: $!" and next
!ll
01
MC/DC0%0/3
$ifh || open($ifh, '<', $argv) || warn("Can't open ${argv}: $!") && (next)
$ifh open $ifh, "<", $argv warn "Can't open ${argv}: $!"
Truth table25%1/4
A or B || C
A= $ifhB= open $ifh, "<", $argvC= warn "Can't open ${argv}: $!"
ABCresult
1XX1
01X1
0011
0000
88 1 ▶ $ifh or open $ifh, "<", $argv or next;
Branch: 1 branch
BranchTrueFalseTotal
$ifh or open $ifh, "<", $argv or next 1 0 1
Condition33%1/3
$ifh or open $ifh, "<", $argv
l!l&&r!l&&!r
100
MC/DC0%0/2
$ifh or open $ifh, "<", $argv
$ifh open $ifh, "<", $argv
Truth table33%1/3
A or B
A= $ifhB= open $ifh, "<", $argv
ABresult
1X1
011
000
89 1 binmode $ifh;
90 1 ▶ if ( $scheme eq 'from_to' ) { # default
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'from_to') { } 1 0 1
elsif ($scheme eq 'decode_encode') { } 0 0 0
91 1 ▶ if ($need2slurp){
Branch: 1 branch
BranchTrueFalseTotal
if ($need2slurp) { } 0 1 1
92 0 local $/;
93 0 $_ = <$ifh>;
94 0 Encode::from_to( $_, $from, $to, $Opt{check} );
95 0 print;
96 }else{
97 1 ▶ while (<$ifh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 319 1 320
98 319 Encode::from_to( $_, $from, $to, $Opt{check} );
99 319 print;
100 }
101 }
102 }
103 elsif ( $scheme eq 'decode_encode' ) { # step-by-step
104 0 ▶ if ($need2slurp){
Branch: 1 branch
BranchTrueFalseTotal
if ($need2slurp) { } 0 0 0
105 0 local $/;
106 0 $_ = <$ifh>;
107 0 my $decoded = decode( $from, $_, $Opt{check} );
108 0 my $encoded = encode( $to, $decoded );
109 0 print $encoded;
110 }else{
111 0 ▶ while (<$ifh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 0 0 0
112 0 my $decoded = decode( $from, $_, $Opt{check} );
113 0 my $encoded = encode( $to, $decoded );
114 0 print $encoded;
115 }
116 }
117 }
118 else { # won't reach
119 0 die "$name: unknown scheme: $scheme";
120 }
121 }
122 }
123 else {
124  
125 # NI-S favorite
126 0 binmode STDOUT => "raw:encoding($to)";
127 0 for my $argv (@ARGV) {
128 0 ▶ my $ifh = ref $argv ? $argv : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $argv ? : 0 0 0
129 0 ▶ $ifh or open $ifh, "<", $argv or warn "Can't open $argv: $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
$ifh or open $ifh, "<", $argv or warn "Can't open ${argv}: $!" and next 0 0 0
Condition0%0/8
$ifh || open($ifh, '<', $argv) || warn("Can't open ${argv}: $!") && (next)
l!l&&r!l&&!r
000
$ifh or open $ifh, "<", $argv
l!l&&r!l&&!r
000
warn "Can't open ${argv}: $!" and next
!ll
00
MC/DC0%0/3
$ifh || open($ifh, '<', $argv) || warn("Can't open ${argv}: $!") && (next)
$ifh open $ifh, "<", $argv warn "Can't open ${argv}: $!"
Truth table0%0/4
A or B || C
A= $ifhB= open $ifh, "<", $argvC= warn "Can't open ${argv}: $!"
ABCresult
1XX1
01X1
0011
0000
130 0 ▶ $ifh or open $ifh, "<", $argv or next;
Branch: 1 branch
BranchTrueFalseTotal
$ifh or open $ifh, "<", $argv or next 0 0 0
Condition0%0/3
$ifh or open $ifh, "<", $argv
l!l&&r!l&&!r
000
MC/DC0%0/2
$ifh or open $ifh, "<", $argv
$ifh open $ifh, "<", $argv
Truth table0%0/3
A or B
A= $ifhB= open $ifh, "<", $argv
ABresult
1X1
011
000
131 0 binmode $ifh => "raw:encoding($from)";
132 0 ▶ print while (<$ifh>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $ifh) and do { print $_ } 0 0 0
133 }
134 }
135  
136 0 ▶sub list_encodings {
Subroutine list_encodings: not called CC=1 SCAR=6.9
137 0 print join( "\n", Encode->encodings(":all") ), "\n";
138 0 exit 0;
139 }
140  
141 0 ▶sub resolve_encoding {
Subroutine resolve_encoding: not called CC=2 SCAR=17.9
142 0 ▶ if ( my $alias = Encode::resolve_alias( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = &Encode::resolve_alias($_[0])) { } 0 0 0
143 0 print $alias, "\n";
144 0 exit 0;
145 }
146 else {
147 0 warn "$name: $_[0] is not known to Encode\n";
148 0 exit 1;
149 }
150 }
151  
152 1 ▶sub help {
Subroutine help: called CC=2 SCAR=7.0
153 1 my $message = shift;
154 1 ▶ $message and print STDERR "$name error: $message\n";
Branch: 1 branch
BranchTrueFalseTotal
$message and print STDERR "$name error: $message\n" 0 1 1
155 1 print STDERR <<"EOT";
156 $name [-f from_encoding] [-t to_encoding]
157 [-p|--perlqq|--htmlcref|--xmlcref] [-C N|-c] [-D] [-S scheme]
158 [-s string|file...]
159 $name -l
160 $name -r encoding_alias
161 $name -h
162 Common options:
163 -l,--list
164 lists all available encodings
165 -r,--resolve encoding_alias
166 resolve encoding to its (Encode) canonical name
167 -f,--from from_encoding
168 when omitted, the current locale will be used
169 -t,--to to_encoding
170 when omitted, the current locale will be used
171 -s,--string string
172 "string" will be the input instead of STDIN or files
173 The following are mainly of interest to Encode hackers:
174 -C N | -c check the validity of the input
175 -D,--debug show debug information
176 -S,--scheme scheme use the scheme for conversion
177 Those are handy when you can only see ASCII characters:
178 -p,--perlqq transliterate characters missing in encoding to \\x{HHHH}
179 where HHHH is the hexadecimal Unicode code point
180 --htmlcref transliterate characters missing in encoding to &#NNN;
181 where NNN is the decimal Unicode code point
182 --xmlcref transliterate characters missing in encoding to &#xHHHH;
183 where HHHH is the hexadecimal Unicode code point
184  
185 EOT
186 1 exit;
187 }
188  
189 __END__
« blib/lib/Encode/Encoder.pm ↑ Summary blib/lib/Encode/Encoding.pm »