more code updates, before i look in depth at the _load() routine
[p5sagit/Config-Any.git] / lib / Config / Any / General.pm
CommitLineData
f0e3c221 1package Config::Any::General;
2
3use strict;
4use warnings;
5
6=head1 NAME
7
8Config::Any::General - Load Config::General files
9
10=head1 DESCRIPTION
11
12Loads 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
26return an array of valid extensions (C<cnf>, C<conf>).
27
28=cut
29
30sub extensions {
31 return qw( cnf conf );
32}
33
34=head2 load( $file )
35
36Attempts to load C<$file> via Config::General.
37
38=cut
39
40sub load {
41 my $class = shift;
42 my $file = shift;
e0c0c283 43 my $args = shift || {};
f0e3c221 44
45 # work around bug (?) in Config::General
92a04e78 46 # return if $class->_test_perl($file);
f0e3c221 47
92a04e78 48 $args->{ -ConfigFile } = $file;
e0c0c283 49
f0e3c221 50 require Config::General;
e0c0c283 51 my $configfile = Config::General->new( %$args );
f0e3c221 52 my $config = { $configfile->getall };
92a04e78 53
f0e3c221 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
63sub _test_perl {
92a04e78 64 my ( $class, $file ) = @_;
f0e3c221 65 my $is_perl_src;
66 eval { $is_perl_src = do "$file"; };
92a04e78 67 delete $INC{ $file }; # so we don't screw stuff later on
f0e3c221 68 return defined $is_perl_src;
69}
70
71=head1 AUTHOR
72
73=over 4
74
75=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
76
77=back
78
79=head1 CONTRIBUTORS
80
81=over 4
82
83=item * Joel Bernstein C<< <rataxis@cpan.org> >>
84
85=back
86
87=head1 COPYRIGHT AND LICENSE
88
89Copyright 2006 by Brian Cassidy
90
91Portions Copyright 2006 Portugal Telecom
92
93This library is free software; you can redistribute it and/or modify
94it under the same terms as Perl itself.
95
96=head1 SEE ALSO
97
98=over 4
99
100=item * L<Catalyst>
101
102=item * L<Config::Any>
103
104=item * L<Config::General>
105
106=back
107
108=cut
109
1101;
111