update is_supported with YAML::Syck version
[p5sagit/Config-Any.git] / lib / Config / Any / General.pm
1 package Config::Any::General;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Config::Any::General - Load Config::General files
9
10 =head1 DESCRIPTION
11
12 Loads Config::General files. Example:
13
14     name = TestApp
15     <Component Controller::Foo>
16         foo bar
17     </Component>
18     <Model Baz>
19         qux xyzzy
20     </Model>
21
22 =head1 METHODS
23
24 =head2 extensions( )
25
26 return an array of valid extensions (C<cnf>, C<conf>).
27
28 =cut
29
30 sub extensions {
31     return qw( cnf conf );
32 }
33
34 =head2 load( $file )
35
36 Attempts to load C<$file> via Config::General.
37
38 =cut
39
40 sub load {
41     my $class = shift;
42     my $file  = shift;
43     my $args  = shift || {};
44
45     # work around bug (?) in Config::General
46     #   return if $class->_test_perl($file);
47
48     $args->{ -ConfigFile } = $file;
49
50     require Config::General;
51     my $configfile = Config::General->new( %$args );
52     my $config     = { $configfile->getall };
53
54     return $config;
55 }
56
57 # this is a bit of a hack but necessary, because Config::General is *far* too lax
58 # about what it will load -- specifically, it seems to be quite happy to load a Perl
59 # config file (ie, a file which is valid Perl and creates a hashref) as if it were
60 # an Apache-style configuration file, presumably due to laziness on the part of the
61 # developer.
62
63 sub _test_perl {
64     my ( $class, $file ) = @_;
65     my $is_perl_src;
66     eval { $is_perl_src = do "$file"; };
67     delete $INC{ $file };    # so we don't screw stuff later on
68     return defined $is_perl_src;
69 }
70
71 =head2 is_supported( )
72
73 Returns true if L<Config::General> is available.
74
75 =cut
76
77 sub is_supported {
78     eval { require Config::General; };
79     return $@ ? 0 : 1;
80 }
81
82 =head1 AUTHOR
83
84 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
85
86 =head1 CONTRIBUTORS
87
88 Joel Bernstein C<< <rataxis@cpan.org> >>
89
90 =head1 COPYRIGHT AND LICENSE
91
92 Copyright 2007 by Brian Cassidy
93
94 Portions Copyright 2006 Portugal Telecom
95
96 This library is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself. 
98
99 =head1 SEE ALSO
100
101 =over 4 
102
103 =item * L<Catalyst>
104
105 =item * L<Config::Any>
106
107 =item * L<Config::General>
108
109 =back
110
111 =cut
112
113 1;
114