File Coverage

script/yaml2perl.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   11459 use strict;
  2         4  
  2         85  
4 2     2   10 use warnings;
  2         4  
  2         130  
5              
6 2     2   1113 use utf8;
  2         674  
  2         12  
7              
8 2         363348 our $VERSION = '0.31';
9              
10 2     2   120 binmode STDERR, ':encoding(UTF-8)';
  2         1644  
  2         35  
  2         12  
11 2         2158 binmode STDOUT, ':encoding(UTF-8)';
12 2         91 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1925 use Getopt::Long qw(:config no_ignore_case);
  2         39196  
  2         17  
15              
16 2     2   2012 use Data::Roundtrip;
  2         9  
  2         12  
17              
18 2         73 my $INPUT_STRING = undef;
19 2         4 my $INPUT_FILE = undef;
20 2         30 my $OUTPUT_FILE = undef;
21 2         11 my %params = (
22             'dont-bloody-escape-unicode' => 1,
23             'terse' => 0,
24             'indent' => 1,
25             );
26              
27             sub usage { return
28 0     0     "Usage : $0 [--I 'a-yaml-string' | --i 'afile.yaml'] [--o afile] [--(no-)escape-unicode|-e] [--(no-)pretty] [--(no-)terse] [--(no-)indent]\n"
29             ."\nIt will read a YAML string from command line (-I), or from a file (-i)\n"
30             ."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
31             ."It will print its contents as a Perl variable (dump) to STDOUT or to a file (--o).\n"
32             ."It can escape/un-escape unicode characters (--escape-unicode) and/or --terse and --no-indent.\n"
33             }
34 2 50       30 if( ! Getopt::Long::GetOptions(
35             'i=s' => \$INPUT_FILE,
36 0     0   0 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
37             'o=s' => \$OUTPUT_FILE,
38             'terse|r!' => \$params{'terse'},
39             'indent|d!' => \$params{'indent'},
40 0 0   0   0 'escape-unicode|e!' => sub { $params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 },
41 0         0 ) ){ die usage() }
42              
43 2 50       2196 if( defined $INPUT_FILE ){
    0          
44 2         12 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
45 2 50       9 if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
  0         0  
  0         0  
46             } elsif( ! defined $INPUT_STRING ){
47             # read from STDIN
48 0         0 $INPUT_STRING = do { local $/; }
  0         0  
  0         0  
49             }
50              
51 2         11 my $result = Data::Roundtrip::yaml2dump($INPUT_STRING, \%params);
52 2 100       9 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::yaml2perll()'." has failed.\n"; exit(1) }
  1         8  
  1         0  
53              
54 1 50       4 if( defined $OUTPUT_FILE ){
55 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  
56             } else {
57 1         7 print STDOUT $result
58             }
59             1;
60             __END__