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