File Coverage

script/yaml2json.pl
Criterion Covered Total %
statement 36 47 76.6
branch 6 14 42.8
condition n/a
subroutine 6 8 75.0
pod n/a
total 48 69 69.5


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 2     2   8884 use strict;
  2         4  
  2         71  
4 2     2   8 use warnings;
  2         3  
  2         103  
5              
6 2     2   955 use utf8;
  2         481  
  2         10  
7              
8 2         429529 our $VERSION = '0.31';
9              
10 2     2   114 binmode STDERR, ':encoding(UTF-8)';
  2         1990  
  2         42  
  2         13  
11 2         2751 binmode STDOUT, ':encoding(UTF-8)';
12 2         121 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1529 use Getopt::Long qw(:config no_ignore_case);
  2         29816  
  2         11  
15              
16 2     2   1609 use Data::Roundtrip;
  2         7  
  2         53  
17              
18 2         97 my $INPUT_STRING = undef;
19 2         24 my $INPUT_FILE = undef;
20 2         3 my $OUTPUT_FILE = undef;
21 2         11 my %params = (
22             'escape-unicode' => 0,
23             'pretty' => 0,
24             );
25              
26             sub usage { return
27 0     0     "Usage : $0 [--I 'a-yaml-string' | --i 'afile.yaml'] [--o afile] [--(no-)escape-unicode|-e] [--(no-)pretty]\n"
28             ."\nIt will read a YAML string from command line (-I), or from a file (-i)\n"
29             ."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
30             ."It will print its contents as YAML to STDOUT or to a file (--o).\n"
31             ."It can escape/un-escape unicode characters (--escape-unicode) and/or do pretty-printing (--pretty).\n"
32             }
33 2 50       29 if( ! Getopt::Long::GetOptions(
34             'i=s' => \$INPUT_FILE,
35 0     0   0 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
36             'o=s' => \$OUTPUT_FILE,
37             'pretty|p!' => \$params{'pretty'},
38             'escape-unicode|e!' => \$params{'escape-unicode'},
39 0         0 ) ){ die usage() }
40              
41 2 50       2770 if( defined $INPUT_FILE ){
    0          
42 2         13 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
43 2 50       11 if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
  0         0  
  0         0  
44             } elsif( ! defined $INPUT_STRING ){
45             # read from STDIN
46 0         0 $INPUT_STRING = do { local $/; }
  0         0  
  0         0  
47             }
48              
49 2         11 my $result = Data::Roundtrip::yaml2json($INPUT_STRING, \%params);
50 2 100       9 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::yaml2jsonl()'." has failed.\n"; exit(1) }
  1         7  
  1         0  
51              
52 1 50       3 if( defined $OUTPUT_FILE ){
53 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  
54             } else {
55 1         7 print STDOUT $result
56             }
57             1;
58             __END__