script/perl2json.pl

filter: Statementstmt 73.3% 33 / 45 Branchbran 37.5% 6 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod n/a totaltotal 63.7% 44 / 69 CC 2 SCAR 8.5
CC
2
Cov
64%
__ANON__
6.0
usage
2.0
__ANON__
2.0
CRAP 2.3
« script/json2perl.pl ↑ Summary script/yaml2perl.pl »
1 #!/usr/bin/env perl
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.31';
9  
10 2 binmode STDERR, ':encoding(UTF-8)';
11 2 binmode STDOUT, ':encoding(UTF-8)';
12 2 binmode STDIN, ':encoding(UTF-8)';
13  
14 2 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Data::Roundtrip;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 my $INPUT_STRING = undef;
19 2 my $INPUT_FILE = undef;
20 2 my $OUTPUT_FILE = undef;
21 2 my %params = (
22 'dont-bloody-escape-unicode' => 1,
23 'pretty' => 0,
24 );
25 0 ▶sub usage { return
Subroutine usage: not called CC=1 SCAR=6.9
26 0 "Usage : $0 [--I 'a-perl-var-as-string' | --i 'afile.pl'] [--o afile.json] [--(no-)escape-unicode|-e] [--(no-)pretty]\n"
27 ."\nIt will read a Perl variable as a string from command line (-I), or a file (-i)\n"
28 ."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
29 ."It will print its JSON equivalent to STDOUT or to a file (--o).\n"
30 ."It can optionally escape unicode characters (--escape-unicode) and/or do pretty-printing (--pretty).\n"
31 }
32 2 ▶if( ! Getopt::Long::GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
unless (&Getopt::Long::GetOptions("i=s", \$INPUT_FILE, "I=s", sub { $INPUT_STRING = &Encode::decode_utf8($_[1]); } , "o=s", \$OUTPUT_FILE, "pretty|p!", \$params{"pretty"}, "escape-unicode|e!", sub { $params{"dont-bloody-escape-unicode"} = $_[1] ? 0 : 1; } )) 0 2 2
33 'i=s' => \$INPUT_FILE,
34 0 ▶ 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 'o=s' => \$OUTPUT_FILE,
36 'pretty|p!' => \$params{'pretty'},
37 0 ▶ 'escape-unicode|e!' => sub { $params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
38 0 ) ){ die usage() }
39  
40 2 ▶if( defined $INPUT_FILE ){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $INPUT_FILE) { } 2 0 2
elsif (not defined $INPUT_STRING) { } 0 0 0
41 2 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
42 0 ▶ if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INPUT_STRING) 0 2 2
43 } elsif( ! defined $INPUT_STRING ){
44 # read from STDIN
45 0 $INPUT_STRING = do { local $/; <STDIN> }
46 }
47  
48 2 my $result = Data::Roundtrip::dump2json($INPUT_STRING, \%params);
49 2 ▶if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::dump2jsonl()'." has failed.\n"; exit(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 1 1 2
50  
51 1 ▶if( defined $OUTPUT_FILE ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OUTPUT_FILE) { } 0 1 1
52 0 ▶ if( ! Data::Roundtrip::write_to_file($OUTPUT_FILE, $result) ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::write_to_file()'." has failed for '$OUTPUT_FILE'.\n"; exit(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Roundtrip::write_to_file($OUTPUT_FILE, $result)) 0 0 0
53 } else {
54 1 print STDOUT $result
55 }
56 1;
57 __END__
« script/json2perl.pl ↑ Summary script/yaml2perl.pl »