make perl loader check mtime for cache, prefer JSON::DWIW for JSON
[p5sagit/Config-Any.git] / lib / Config / Any / Perl.pm
1 package Config::Any::Perl;
2
3 use strict;
4 use warnings;
5
6 use base 'Config::Any::Base';
7
8 my %cache;
9
10 =head1 NAME
11
12 Config::Any::Perl - Load Perl config files
13
14 =head1 DESCRIPTION
15
16 Loads Perl files. Example:
17
18     {
19         name => 'TestApp',
20         'Controller::Foo' => {
21             foo => 'bar'
22         },
23         'Model::Baz' => {
24             qux => 'xyzzy'
25         }
26     }
27
28 =head1 METHODS
29
30 =head2 extensions( )
31
32 return an array of valid extensions (C<pl>, C<perl>).
33
34 =cut
35
36 sub extensions {
37     return qw( pl perl );
38 }
39
40 =head2 load( $file )
41
42 Attempts to load C<$file> as a Perl file.
43
44 =cut
45
46 sub load {
47     my $class = shift;
48     my $file  = shift;
49     my $content;
50
51     my $mtime = (stat($file))[9];
52
53     if ( (not exists $cache{ $file }) || $cache{ $file }{ mtime } < $mtime ) {
54         my $exception;
55         {
56             local $@;
57             $content = do $file;
58             $exception = $@;
59         }
60         die $exception if $exception;
61
62         $cache{ $file }{ mtime   } = $mtime;
63         $cache{ $file }{ content } = $content;
64     }
65
66     return $cache{ $file }{ content };
67 }
68
69 =head1 AUTHOR
70
71 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
72
73 =head1 COPYRIGHT AND LICENSE
74
75 Copyright 2006-2009 by Brian Cassidy
76
77 This library is free software; you can redistribute it and/or modify
78 it under the same terms as Perl itself. 
79
80 =head1 SEE ALSO
81
82 =over 4 
83
84 =item * L<Catalyst>
85
86 =item * L<Config::Any>
87
88 =back
89
90 =cut
91
92 1;