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