refactor _load(). fix an issue with use_ext => 0. use_ext now on by default. throw...
[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 =head1 AUTHOR
72
73 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
74
75 =head1 CONTRIBUTORS
76
77 Joel Bernstein C<< <rataxis@cpan.org> >>
78
79 =head1 COPYRIGHT AND LICENSE
80
81 Copyright 2007 by Brian Cassidy
82
83 Portions Copyright 2006 Portugal Telecom
84
85 This library is free software; you can redistribute it and/or modify
86 it under the same terms as Perl itself. 
87
88 =head1 SEE ALSO
89
90 =over 4 
91
92 =item * L<Catalyst>
93
94 =item * L<Config::Any>
95
96 =item * L<Config::General>
97
98 =back
99
100 =cut
101
102 1;
103