make perl loader check mtime for cache, prefer JSON::DWIW for JSON
[p5sagit/Config-Any.git] / lib / Config / Any / YAML.pm
1 package Config::Any::YAML;
2
3 use strict;
4 use warnings;
5
6 use base 'Config::Any::Base';
7
8 use Carp ();
9
10 =head1 NAME
11
12 Config::Any::YAML - Load YAML config files
13
14 =head1 DESCRIPTION
15
16 Loads YAML files. Example:
17
18     ---
19     name: TestApp
20     Controller::Foo:
21         foo: bar
22     Model::Baz:
23         qux: xyzzy
24     
25
26 =head1 METHODS
27
28 =head2 extensions( )
29
30 return an array of valid extensions (C<yml>, C<yaml>).
31
32 =cut
33
34 sub extensions {
35     return qw( yml yaml );
36 }
37
38 =head2 load( $file )
39
40 Attempts to load C<$file> as a YAML file.
41
42 =cut
43
44 sub load {
45     my $class = shift;
46     my $file  = shift;
47
48     eval { require YAML::XS };
49     unless ( $@ ) {
50         return YAML::XS::LoadFile( $file );
51     }
52
53     Carp::carp
54         'Use of YAML::Syck or YAML to parse config files is DEPRECATED. '
55         . 'Please install YAML::XS for proper YAML support';
56
57     eval { require YAML::Syck; YAML::Syck->VERSION( '0.70' ) };
58     unless ( $@ ) {
59         open( my $fh, $file ) or die $!;
60         my $content = do { local $/; <$fh> };
61         close $fh;
62         return YAML::Syck::Load( $content );
63     }
64
65     require YAML;
66     return YAML::LoadFile( $file );
67 }
68
69 =head2 requires_any_of( )
70
71 Specifies that this modules requires one of L<YAML::XS>, L<YAML::Syck> (0.70) or
72 L<YAML> in order to work.
73
74 =cut
75
76 sub requires_any_of { 'YAML::XS', [ 'YAML::Syck', '0.70' ], 'YAML' }
77
78 =head1 AUTHOR
79
80 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
81
82 =head1 COPYRIGHT AND LICENSE
83
84 Copyright 2006-2009 by Brian Cassidy
85
86 This library is free software; you can redistribute it and/or modify
87 it under the same terms as Perl itself. 
88
89 =head1 SEE ALSO
90
91 =over 4 
92
93 =item * L<Catalyst>
94
95 =item * L<Config::Any>
96
97 =item * L<YAML::XS>
98
99 =item * L<YAML>
100
101 =item * L<YAML::Syck>
102
103 =back
104
105 =cut
106
107 1;