line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package MooseX::YAML; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
56991
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
52
|
|
6
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
83
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.04_001"; |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
115
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
927
|
use MooseX::Blessed::Reconstruct; |
|
2
|
|
|
|
|
545077
|
|
|
2
|
|
|
|
|
136
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $v; |
15
|
|
|
|
|
|
|
sub fixup { ($v ||= MooseX::Blessed::Reconstruct->new)->visit(@_) } |
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
17
|
use namespace::clean; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
9
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Sub::Exporter -setup => { |
20
|
|
|
|
|
|
|
exports => [qw(Load LoadFile)], |
21
|
|
|
|
|
|
|
collectors => [ "-xs", "-syck", "-pp" ], |
22
|
|
|
|
|
|
|
generator => sub { |
23
|
2
|
|
|
|
|
6988
|
foreach my $export ( @_ ) { |
24
|
2
|
|
|
|
|
10
|
my $r = $export->{class}->_resolve($export->{name}, $export->{col}); |
25
|
2
|
|
|
2
|
|
9
|
return sub { fixup( $r->(@_) ) }; |
|
2
|
|
|
|
|
7856
|
|
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
}, |
28
|
2
|
|
|
2
|
|
589
|
}; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
55
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _resolve { |
31
|
3
|
|
|
3
|
|
10
|
my ( $class, $routine, $flags ) = @_; |
32
|
|
|
|
|
|
|
|
33
|
3
|
100
|
|
|
|
11
|
if ( keys %$flags ) { |
34
|
1
|
50
|
|
|
|
4
|
croak "Can't use more than one of -xs, -syck or -pp" if keys %$flags > 1; |
35
|
|
|
|
|
|
|
|
36
|
1
|
50
|
|
|
|
5
|
if ( exists $flags->{-xs} ) { |
|
|
50
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
require YAML::XS; |
38
|
0
|
|
|
|
|
0
|
my $sub = YAML::XS->can($routine); |
39
|
|
|
|
|
|
|
return sub { |
40
|
0
|
|
|
0
|
|
0
|
local $YAML::XS::LoadBlessed = 1; |
41
|
0
|
|
|
|
|
0
|
$sub->(@_); |
42
|
0
|
|
|
|
|
0
|
}; |
43
|
|
|
|
|
|
|
} elsif ( exists $flags->{-syck} ) { |
44
|
0
|
|
|
|
|
0
|
require YAML::Syck; |
45
|
0
|
|
|
|
|
0
|
my $sub = YAML::Syck->can($routine); |
46
|
|
|
|
|
|
|
return sub { |
47
|
0
|
|
|
0
|
|
0
|
local $YAML::Syck::LoadBlessed = 1; |
48
|
0
|
|
|
|
|
0
|
$sub->(@_); |
49
|
0
|
|
|
|
|
0
|
}; |
50
|
|
|
|
|
|
|
} else { |
51
|
1
|
|
|
|
|
37
|
require YAML; |
52
|
1
|
|
|
|
|
10
|
my $sub = YAML->can($routine); |
53
|
|
|
|
|
|
|
return sub { |
54
|
1
|
|
|
1
|
|
3
|
local $YAML::LoadBlessed = 1; |
55
|
1
|
|
|
|
|
4
|
$sub->(@_); |
56
|
1
|
|
|
|
|
7
|
}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} else { |
59
|
|
|
|
|
|
|
my $drv = ( |
60
|
2
|
|
33
|
|
|
5
|
do { local $@; eval { require YAML::XS; "YAML::XS" } } |
61
|
|
|
|
|
|
|
or |
62
|
|
|
|
|
|
|
require YAML && "YAML" |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
33
|
|
|
13174
|
my $sub = $drv->can($routine) || croak "Can't find a provided for $routine (fallback is $drv)"; |
66
|
|
|
|
|
|
|
return sub { |
67
|
2
|
|
|
2
|
|
14
|
local $YAML::LoadBlessed = 1; |
68
|
2
|
|
|
|
|
3
|
local $YAML::XS::LoadBlessed = 1; |
69
|
2
|
|
|
|
|
10
|
$sub->(@_); |
70
|
2
|
|
|
|
|
13
|
}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $load; |
75
|
|
|
|
|
|
|
sub Load { |
76
|
1
|
|
33
|
1
|
1
|
7937
|
$load ||= __PACKAGE__->_resolve("Load"); |
77
|
1
|
|
|
|
|
3
|
fixup( $load->(@_) ); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $loadfile; |
81
|
|
|
|
|
|
|
sub LoadFile { |
82
|
0
|
|
0
|
0
|
1
|
|
$loadfile ||= __PACKAGE__->_resolve("LoadFile"); |
83
|
0
|
|
|
|
|
|
fixup( $loadfile->(@_) ); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__PACKAGE__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=pod |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
MooseX::YAML - DWIM loading of Moose objects from YAML |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 SYNOPSIS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# given some class: |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
package My::Module; |
101
|
|
|
|
|
|
|
use Moose; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
has package => ( |
104
|
|
|
|
|
|
|
is => "ro", |
105
|
|
|
|
|
|
|
init_arg => "name", |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
has version => ( |
109
|
|
|
|
|
|
|
is => "rw", |
110
|
|
|
|
|
|
|
init_arg => undef, |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub BUILD { shift->version(3) } |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# load an object like so: |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
use MooseX::YAML qw(Load -xs); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $obj = Load(<<'YAML'); |
122
|
|
|
|
|
|
|
--- !My::Module # this syntax requires YAML::XS |
123
|
|
|
|
|
|
|
name: "MooseX::YAML" |
124
|
|
|
|
|
|
|
YAML |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$obj->package; # "MooseX::YAML" |
127
|
|
|
|
|
|
|
$obj->version; # 3, BUILD was called |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 DESCRIPTION |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
This module provides DWIM loading of L<Moose> based objects from YAML |
132
|
|
|
|
|
|
|
documents. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Any hashes blessed into a L<Moose> class will be replaced with a properly |
135
|
|
|
|
|
|
|
constructed instance (respecting init args, C<BUILDALL>, and the meta instance |
136
|
|
|
|
|
|
|
type). |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is similar to L<YAML::Active> in that certain nodes in the loaded YAML |
139
|
|
|
|
|
|
|
documented are treated specially. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 EXPORTS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
All exports are setup by L<Sub::Exporter> using currying. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
C<-xs>, C<-syck> or C<-pp> can be specified to specify L<YAML::XS>, |
146
|
|
|
|
|
|
|
L<YAML::Syck> or L<YAML> on a per import basis. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
If no driver is explicitly chosen L<YAML::XS> will be tried first, falling back |
149
|
|
|
|
|
|
|
to L<YAML>. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=over 4 |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item Load |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item LoadFile |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 VERSION CONTROL |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This module is maintained using Darcs. You can get the latest version from |
162
|
|
|
|
|
|
|
L<http://nothingmuch.woobling.org/code>, and use C<darcs send> to commit |
163
|
|
|
|
|
|
|
changes. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHOR |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt> |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Copyright (c) 2008 Yuval Kogman. All rights reserved |
172
|
|
|
|
|
|
|
This program is free software; you can redistribute |
173
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |