| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bot::Cobalt::Serializer; |
|
2
|
|
|
|
|
|
|
$Bot::Cobalt::Serializer::VERSION = '0.021001'; |
|
3
|
16
|
|
|
16
|
|
61604
|
use strictures 2; |
|
|
16
|
|
|
|
|
2216
|
|
|
|
16
|
|
|
|
|
549
|
|
|
4
|
16
|
|
|
16
|
|
2136
|
use Carp; |
|
|
16
|
|
|
|
|
19
|
|
|
|
16
|
|
|
|
|
694
|
|
|
5
|
16
|
|
|
16
|
|
50
|
use Scalar::Util 'reftype'; |
|
|
16
|
|
|
|
|
17
|
|
|
|
16
|
|
|
|
|
515
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
## These two must be present anyway: |
|
8
|
16
|
|
|
16
|
|
5422
|
use YAML::XS (); |
|
|
16
|
|
|
|
|
29390
|
|
|
|
16
|
|
|
|
|
249
|
|
|
9
|
16
|
|
|
16
|
|
5959
|
use JSON::MaybeXS (); |
|
|
16
|
|
|
|
|
63375
|
|
|
|
16
|
|
|
|
|
326
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
16
|
|
|
16
|
|
69
|
use Fcntl ':flock'; |
|
|
16
|
|
|
|
|
18
|
|
|
|
16
|
|
|
|
|
1689
|
|
|
12
|
16
|
|
|
16
|
|
5655
|
use Time::HiRes 'sleep'; |
|
|
16
|
|
|
|
|
12220
|
|
|
|
16
|
|
|
|
|
66
|
|
|
13
|
16
|
|
|
16
|
|
1927
|
use Scalar::Util 'blessed'; |
|
|
16
|
|
|
|
|
41
|
|
|
|
16
|
|
|
|
|
592
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
16
|
|
|
16
|
|
1395
|
use Bot::Cobalt::Common ':types'; |
|
|
16
|
|
|
|
|
21
|
|
|
|
16
|
|
|
|
|
88
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
16
|
|
|
16
|
|
2998
|
use Moo; |
|
|
16
|
|
|
|
|
40928
|
|
|
|
16
|
|
|
|
|
72
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has Format => ( |
|
21
|
|
|
|
|
|
|
is => 'rw', |
|
22
|
|
|
|
|
|
|
isa => Str, |
|
23
|
65
|
|
|
65
|
|
1081
|
builder => sub { 'YAMLXS' }, |
|
24
|
|
|
|
|
|
|
trigger => sub { |
|
25
|
|
|
|
|
|
|
my ($self, $format) = @_; |
|
26
|
|
|
|
|
|
|
$format = uc($format); |
|
27
|
|
|
|
|
|
|
confess "Unknown format $format" |
|
28
|
|
|
|
|
|
|
unless grep { $_ eq $format } keys %{ $self->_types }; |
|
29
|
|
|
|
|
|
|
confess "Requested format $format but can't find a module for it" |
|
30
|
|
|
|
|
|
|
unless $self->_check_if_avail($format) |
|
31
|
|
|
|
|
|
|
}, |
|
32
|
|
|
|
|
|
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has _types => ( |
|
35
|
|
|
|
|
|
|
lazy => 1, |
|
36
|
|
|
|
|
|
|
is => 'ro', |
|
37
|
|
|
|
|
|
|
isa => HashRef, |
|
38
|
|
|
|
|
|
|
builder => sub { |
|
39
|
|
|
|
|
|
|
+{ |
|
40
|
4
|
|
|
4
|
|
1373
|
YAML => 'YAML::Syck', |
|
41
|
|
|
|
|
|
|
YAMLXS => 'YAML::XS', |
|
42
|
|
|
|
|
|
|
JSON => 'JSON::MaybeXS', |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
}, |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has yamlxs_from_ref => ( |
|
48
|
|
|
|
|
|
|
is => 'rw', |
|
49
|
|
|
|
|
|
|
lazy => 1, |
|
50
|
|
|
|
|
|
|
coerce => sub { YAML::XS::Dump($_[0]) }, |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has ref_from_yamlxs => ( |
|
54
|
|
|
|
|
|
|
is => 'rw', |
|
55
|
|
|
|
|
|
|
lazy => 1, |
|
56
|
|
|
|
|
|
|
coerce => sub { YAML::XS::Load($_[0]) }, |
|
57
|
|
|
|
|
|
|
); |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has yaml_from_ref => ( |
|
60
|
|
|
|
|
|
|
is => 'rw', |
|
61
|
|
|
|
|
|
|
lazy => 1, |
|
62
|
|
|
|
|
|
|
coerce => sub { require YAML::Syck; YAML::Syck::Dump($_[0]) }, |
|
63
|
|
|
|
|
|
|
); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
has ref_from_yaml => ( |
|
66
|
|
|
|
|
|
|
is => 'rw', |
|
67
|
|
|
|
|
|
|
lazy => 1, |
|
68
|
|
|
|
|
|
|
coerce => sub { require YAML::Syck; YAML::Syck::Load($_[0]) }, |
|
69
|
|
|
|
|
|
|
); |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
has json_from_ref => ( |
|
72
|
|
|
|
|
|
|
is => 'rw', |
|
73
|
|
|
|
|
|
|
lazy => 1, |
|
74
|
|
|
|
|
|
|
coerce => sub { |
|
75
|
|
|
|
|
|
|
my $jsify = JSON::MaybeXS->new( |
|
76
|
|
|
|
|
|
|
utf8 => 1, allow_nonref => 1, convert_blessed => 1 |
|
77
|
|
|
|
|
|
|
); |
|
78
|
|
|
|
|
|
|
$jsify->utf8->encode($_[0]); |
|
79
|
|
|
|
|
|
|
}, |
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
has ref_from_json => ( |
|
83
|
|
|
|
|
|
|
is => 'rw', |
|
84
|
|
|
|
|
|
|
lazy => 1, |
|
85
|
|
|
|
|
|
|
coerce => sub { |
|
86
|
|
|
|
|
|
|
my $jsify = JSON::MaybeXS->new( |
|
87
|
|
|
|
|
|
|
utf8 => 1, allow_nonref => 1 |
|
88
|
|
|
|
|
|
|
); |
|
89
|
|
|
|
|
|
|
$jsify->utf8->decode($_[0]) |
|
90
|
|
|
|
|
|
|
}, |
|
91
|
|
|
|
|
|
|
); |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub BUILDARGS { |
|
95
|
69
|
|
|
69
|
0
|
27386
|
my ($class, @args) = @_; |
|
96
|
|
|
|
|
|
|
## my $serializer = Bot::Cobalt::Serializer->new( %opts ) |
|
97
|
|
|
|
|
|
|
## Serialize to YAML using YAML::XS: |
|
98
|
|
|
|
|
|
|
## ->new() |
|
99
|
|
|
|
|
|
|
## - or - |
|
100
|
|
|
|
|
|
|
## ->new($format) |
|
101
|
|
|
|
|
|
|
## ->new('JSON') # f.ex |
|
102
|
|
|
|
|
|
|
## - or - |
|
103
|
|
|
|
|
|
|
## ->new( Format => 'JSON' ) ## --> to JSON |
|
104
|
|
|
|
|
|
|
## - or - |
|
105
|
|
|
|
|
|
|
## ->new( Format => 'YAML' ) ## --> to YAML1.0 |
|
106
|
69
|
100
|
|
|
|
988
|
@args == 1 ? { Format => $args[0] } : { @args } |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub freeze { |
|
110
|
|
|
|
|
|
|
## ->freeze($ref) |
|
111
|
5
|
|
|
5
|
1
|
887
|
my ($self, $ref) = @_; |
|
112
|
5
|
50
|
|
|
|
14
|
unless (defined $ref) { |
|
113
|
0
|
|
|
|
|
0
|
carp "freeze() received no data"; |
|
114
|
|
|
|
|
|
|
return |
|
115
|
0
|
|
|
|
|
0
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
5
|
|
|
|
|
49
|
my $method = lc( $self->Format ); |
|
118
|
5
|
|
|
|
|
1406
|
$method = $method . "_from_ref"; |
|
119
|
|
|
|
|
|
|
|
|
120
|
5
|
|
|
|
|
17
|
$self->$method($ref) |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub thaw { |
|
124
|
|
|
|
|
|
|
## ->thaw($data) |
|
125
|
25
|
|
|
25
|
1
|
497
|
my ($self, $data) = @_; |
|
126
|
25
|
50
|
|
|
|
63
|
unless (defined $data) { |
|
127
|
0
|
|
|
|
|
0
|
carp "thaw() received no data"; |
|
128
|
|
|
|
|
|
|
return |
|
129
|
0
|
|
|
|
|
0
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
25
|
|
|
|
|
393
|
my $method = lc( $self->Format ); |
|
132
|
25
|
|
|
|
|
3545
|
$method = "ref_from_" . $method ; |
|
133
|
|
|
|
|
|
|
|
|
134
|
25
|
|
|
|
|
227
|
$self->$method($data) |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub writefile { |
|
138
|
3
|
|
|
3
|
1
|
2192
|
my ($self, $path, $ref, $opts) = @_; |
|
139
|
|
|
|
|
|
|
## $serializer->writefile($path, $ref [, { Opts }); |
|
140
|
|
|
|
|
|
|
|
|
141
|
3
|
50
|
|
|
|
12
|
if (!$path) { |
|
|
|
50
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
confess "writefile called without path argument" |
|
143
|
|
|
|
|
|
|
} elsif (!defined $ref) { |
|
144
|
0
|
|
|
|
|
0
|
confess "writefile called without data to serialize" |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
3
|
|
|
|
|
11
|
my $frozen = $self->freeze($ref); |
|
148
|
|
|
|
|
|
|
|
|
149
|
3
|
|
|
|
|
40
|
$self->_write_serialized($path, $frozen, $opts) |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub readfile { |
|
153
|
20
|
|
|
20
|
1
|
992
|
my ($self, $path, $opts) = @_; |
|
154
|
|
|
|
|
|
|
## my $ref = $serializer->readfile($path) |
|
155
|
|
|
|
|
|
|
|
|
156
|
20
|
50
|
|
|
|
218
|
if (!$path) { |
|
|
|
50
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
0
|
confess "readfile called without path argument"; |
|
158
|
|
|
|
|
|
|
} elsif (!-e $path ) { |
|
159
|
0
|
|
|
|
|
0
|
confess "readfile called on nonexistant file $path"; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
20
|
|
|
|
|
349
|
my $data = $self->_read_serialized($path, $opts); |
|
163
|
|
|
|
|
|
|
|
|
164
|
20
|
|
|
|
|
7337
|
$self->thaw($data) |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub version { |
|
168
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
169
|
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
0
|
my $module = $self->_types->{ $self->Format }; |
|
171
|
0
|
|
|
|
|
0
|
{ local $@; eval "require $module" } |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
172
|
0
|
|
|
|
|
0
|
return($module, $module->VERSION); |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _check_if_avail { |
|
178
|
4
|
|
|
4
|
|
7
|
my ($self, $type) = @_; |
|
179
|
|
|
|
|
|
|
|
|
180
|
4
|
|
|
|
|
6
|
my $module; |
|
181
|
4
|
50
|
|
|
|
56
|
return unless $module = $self->_types->{$type}; |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
{ |
|
184
|
4
|
|
|
|
|
25
|
local $@; |
|
|
4
|
|
|
|
|
4
|
|
|
185
|
4
|
|
|
|
|
199
|
eval "require $module"; |
|
186
|
4
|
50
|
|
|
|
22
|
return if $@; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
4
|
|
|
|
|
212
|
return $module |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub _read_serialized { |
|
194
|
20
|
|
|
20
|
|
29
|
my ($self, $path, $opts) = @_; |
|
195
|
20
|
50
|
|
|
|
45
|
return unless defined $path; |
|
196
|
|
|
|
|
|
|
|
|
197
|
20
|
|
|
|
|
24
|
my $lock = 1; |
|
198
|
20
|
0
|
33
|
|
|
54
|
if (defined $opts && ref $opts && reftype $opts eq 'HASH') { |
|
|
|
|
33
|
|
|
|
|
|
199
|
0
|
0
|
|
|
|
0
|
$lock = $opts->{Locking} if defined $opts->{Locking}; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
20
|
100
|
66
|
|
|
189
|
if (blessed $path && $path->can('slurp_utf8')) { |
|
203
|
17
|
|
|
|
|
51
|
return $path->slurp_utf8 |
|
204
|
|
|
|
|
|
|
} else { |
|
205
|
3
|
50
|
|
|
|
68
|
open(my $in_fh, '<:encoding(UTF-8)', $path) |
|
206
|
|
|
|
|
|
|
or confess "open failed for $path: $!"; |
|
207
|
|
|
|
|
|
|
|
|
208
|
3
|
50
|
|
|
|
111
|
if ($lock) { |
|
209
|
3
|
50
|
|
|
|
13
|
flock($in_fh, LOCK_SH) |
|
210
|
|
|
|
|
|
|
or confess "LOCK_SH failed for $path: $!"; |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
3
|
|
|
|
|
81
|
my $data = join '', <$in_fh>; |
|
214
|
|
|
|
|
|
|
|
|
215
|
3
|
50
|
|
|
|
53
|
flock($in_fh, LOCK_UN) if $lock; |
|
216
|
|
|
|
|
|
|
|
|
217
|
3
|
50
|
|
|
|
24
|
close($in_fh) |
|
218
|
|
|
|
|
|
|
or carp "close failed for $path: $!"; |
|
219
|
|
|
|
|
|
|
|
|
220
|
3
|
|
|
|
|
10
|
return $data |
|
221
|
|
|
|
|
|
|
} |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub _write_serialized { |
|
225
|
3
|
|
|
3
|
|
34
|
my ($self, $path, $data, $opts) = @_; |
|
226
|
3
|
50
|
33
|
|
|
24
|
return unless $path and defined $data; |
|
227
|
|
|
|
|
|
|
|
|
228
|
3
|
|
|
|
|
3
|
my $lock = 1; |
|
229
|
3
|
|
|
|
|
3
|
my $timeout = 2; |
|
230
|
|
|
|
|
|
|
|
|
231
|
3
|
0
|
33
|
|
|
8
|
if (defined $opts && ref $opts && reftype $opts eq 'HASH') { |
|
|
|
|
33
|
|
|
|
|
|
232
|
0
|
0
|
|
|
|
0
|
$lock = $opts->{Locking} if defined $opts->{Locking}; |
|
233
|
0
|
0
|
|
|
|
0
|
$timeout = $opts->{Timeout} if $opts->{Timeout}; |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
2
|
50
|
|
2
|
|
8
|
open(my $out_fh, '>>:encoding(UTF-8)', $path) |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
10
|
|
|
|
3
|
|
|
|
|
72
|
|
|
237
|
|
|
|
|
|
|
or confess "open failed for $path: $!"; |
|
238
|
|
|
|
|
|
|
|
|
239
|
3
|
50
|
|
|
|
1800
|
if ($lock) { |
|
240
|
3
|
|
|
|
|
5
|
my $timer = 0; |
|
241
|
|
|
|
|
|
|
|
|
242
|
3
|
|
|
|
|
18
|
until ( flock $out_fh, LOCK_EX | LOCK_NB ) { |
|
243
|
0
|
0
|
|
|
|
0
|
confess "Failed writefile lock ($path), timed out ($timeout)" |
|
244
|
|
|
|
|
|
|
if $timer > $timeout; |
|
245
|
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
0
|
sleep 0.01; |
|
247
|
0
|
|
|
|
|
0
|
$timer += 0.01; |
|
248
|
|
|
|
|
|
|
} |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
3
|
50
|
|
|
|
20
|
seek($out_fh, 0, 0) |
|
253
|
|
|
|
|
|
|
or confess "seek failed for $path: $!"; |
|
254
|
3
|
50
|
|
|
|
99
|
truncate($out_fh, 0) |
|
255
|
|
|
|
|
|
|
or confess "truncate failed for $path"; |
|
256
|
|
|
|
|
|
|
|
|
257
|
3
|
|
|
|
|
13
|
print $out_fh $data; |
|
258
|
|
|
|
|
|
|
|
|
259
|
3
|
50
|
|
|
|
124
|
flock($out_fh, LOCK_UN) if $lock; |
|
260
|
|
|
|
|
|
|
|
|
261
|
3
|
50
|
|
|
|
35
|
close($out_fh) |
|
262
|
|
|
|
|
|
|
or carp "close failed for $path: $!"; |
|
263
|
|
|
|
|
|
|
|
|
264
|
3
|
|
|
|
|
18
|
return 1 |
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
1; |
|
268
|
|
|
|
|
|
|
__END__ |