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