blib/lib/Catmandu/Importer/YAML.pm

filter: Statementstmt 100% 22 / 22 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 96.5% 28 / 29 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Catmandu/Fix/to_json.pm ↑ Summary blib/lib/Catmandu/Fix/add_field.pm »
1 package Catmandu::Importer::YAML;
2  
3 3 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.0201_02';
6  
7 3 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Devel::Peek;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 with 'Catmandu::Importer';
13  
14 2 my $RE_EOF = qr'^\.\.\.$';
15 2 my $RE_SEP = qr'^---';
16  
17 sub generator {
18 2 ▶ my ($self) = @_;
Subroutine generator: called CC=1 SCAR=0.0
Pod undocumented
19 sub {
20 state $fh = $self->fh;
21 state $yaml = "";
22 state $data;
23 state $line;
24 while (defined($line = <$fh>)) {
25 if ($line =~ $RE_EOF) {
26 last;
27 }
28 if ($line =~ $RE_SEP && $yaml) {
29 utf8::encode($yaml);
30 $data = YAML::XS::Load($yaml);
31 $yaml = $line;
32 return $data;
33 }
34 $yaml .= $line;
35 }
36 if ($yaml) {
37 utf8::encode($yaml);
38 $data = YAML::XS::Load($yaml);
39 $yaml = "";
40 return $data;
41 }
42 return;
43 2 };
44 }
45  
46 2 1;
47  
48 __END__
49  
50 =pod
51  
52 =head1 NAME
53  
54 Catmandu::Importer::YAML - Package that imports YAML data
55  
56 =head1 SYNOPSIS
57  
58 # From the command line
59  
60 $ catmandu convert YAML to JSON < data.yaml
61  
62 # In a Perl script
63
64 use Catmandu::Importer::YAML;
65  
66 my $importer = Catmandu::Importer::YAML->new(file => "/foo/bar.yaml");
67  
68 my $n = $importer->each(sub {
69 my $hashref = $_[0];
70 # ...
71 });
72  
73 The YAML input file needs to be separated into records:
74  
75 ---
76 - recordno: 1
77 - name: Alpha
78 ---
79 - recordno: 2
80 - name: Beta
81 ...
82  
83 where '---' is the record separator and '...' the EOF indicator.
84  
85 =head1 CONFIGURATION
86  
87 =over
88  
89 =item file
90  
91 Read input from a local file given by its path. Alternatively a scalar
92 reference can be passed to read from a string.
93  
94 =item fh
95  
96 Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to
97 create the input stream from the C<file> argument or by using STDIN.
98  
99 =item encoding
100  
101 Binmode of the input stream C<fh>. Set to C<:utf8> by default.
102  
103 =item fix
104  
105 An ARRAY of one or more fixes or file scripts to be applied to imported items.
106  
107 =back
108  
109 =head1 METHODS
110  
111 Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are
112 inherited. The Catmandu::Importer::YAML methods are not idempotent: YAML feeds
113 can only be read once.
114  
115 =head1 SEE ALSO
116  
117 L<Catmandu::Exporter::YAML>
118  
119 =cut
« blib/lib/Catmandu/Fix/to_json.pm ↑ Summary blib/lib/Catmandu/Fix/add_field.pm »