line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::YAML; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
105176
|
use Catmandu::Sane; |
|
5
|
|
|
|
|
14
|
|
|
5
|
|
|
|
|
48
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
48
|
use YAML::XS (); |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
121
|
|
8
|
5
|
|
|
5
|
|
25
|
use Moo; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
41
|
|
9
|
5
|
|
|
5
|
|
8482
|
use Devel::Peek; |
|
5
|
|
|
|
|
2160
|
|
|
5
|
|
|
|
|
47
|
|
10
|
5
|
|
|
5
|
|
544
|
use namespace::clean; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
41
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $RE_EOF = qr'^\.\.\.$'; |
15
|
|
|
|
|
|
|
my $RE_SEP = qr'^---'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub generator { |
18
|
|
|
|
|
|
|
my ($self) = @_; |
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
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
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; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $importer = Catmandu->importer('YAML',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 |