File Coverage

script/perl2json.pl
Criterion Covered Total %
statement 36 48 75.0
branch 6 16 37.5
condition n/a
subroutine 6 9 66.6
pod n/a
total 48 73 65.7


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 2     2   11283 use strict;
  2         4  
  2         85  
4 2     2   11 use warnings;
  2         3  
  2         129  
5              
6 2     2   1163 use utf8;
  2         596  
  2         14  
7              
8 2         401380 our $VERSION = '0.31';
9              
10 2     2   107 binmode STDERR, ':encoding(UTF-8)';
  2         1521  
  2         31  
  2         11  
11 2         2155 binmode STDOUT, ':encoding(UTF-8)';
12 2         95 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1803 use Getopt::Long qw(:config no_ignore_case);
  2         37652  
  2         15  
15              
16 2     2   1956 use Data::Roundtrip;
  2         11  
  2         16  
17              
18 2         71 my $INPUT_STRING = undef;
19 2         16 my $INPUT_FILE = undef;
20 2         3 my $OUTPUT_FILE = undef;
21 2         9 my %params = (
22             'dont-bloody-escape-unicode' => 1,
23             'pretty' => 0,
24             );
25             sub usage { return
26 0     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 50       43 if( ! Getopt::Long::GetOptions(
33             'i=s' => \$INPUT_FILE,
34 0     0   0 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
35             'o=s' => \$OUTPUT_FILE,
36             'pretty|p!' => \$params{'pretty'},
37 0 0   0   0 'escape-unicode|e!' => sub { $params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 },
38 0         0 ) ){ die usage() }
39              
40 2 50       2090 if( defined $INPUT_FILE ){
    0          
41 2         12 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
42 2 50       8 if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
  0         0  
  0         0  
43             } elsif( ! defined $INPUT_STRING ){
44             # read from STDIN
45 0         0 $INPUT_STRING = do { local $/; }
  0         0  
  0         0  
46             }
47              
48 2         10 my $result = Data::Roundtrip::dump2json($INPUT_STRING, \%params);
49 2 100       5 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::dump2jsonl()'." has failed.\n"; exit(1) }
  1         52  
  1         0  
50              
51 1 50       3 if( defined $OUTPUT_FILE ){
52 0 0       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) }
  0         0  
  0         0  
53             } else {
54 1         11 print STDOUT $result
55             }
56             1;
57             __END__