line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Autoload; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24007
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
593
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
|
|
|
|
|
|
|
11
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
12
|
0
|
|
|
|
|
|
my $file = shift; |
13
|
0
|
|
|
|
|
|
my $construct = shift; |
14
|
|
|
|
|
|
|
|
15
|
0
|
0
|
|
|
|
|
if ( ! -f $file ) { |
|
|
0
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
croak "The config file doesn't exist"; |
17
|
|
|
|
|
|
|
} elsif ( ! -r $file ) { |
18
|
0
|
|
|
|
|
|
croak "The config file isn't readable"; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
0
|
0
|
0
|
|
|
|
if ( defined($construct) && ref($construct) ne 'CODE' ) { |
22
|
0
|
|
|
|
|
|
croak "The second argument for new() must be a coderef"; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my $mtime = (stat($file))[9]; |
26
|
0
|
|
|
|
|
|
my $c; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
if ( defined $construct ) { |
29
|
0
|
|
|
|
|
|
$c = $construct->($file); |
30
|
0
|
0
|
|
|
|
|
unless (ref($c) eq 'HASH') { |
31
|
0
|
|
|
|
|
|
croak "Not a hashref returned from the subroutine"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} else { |
34
|
0
|
|
|
|
|
|
$c = _construct($file); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
bless { file => $file, mtime => $mtime, |
38
|
|
|
|
|
|
|
c => $c, construct => $construct }, $class; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _construct { |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
0
|
|
|
my $file = shift; |
44
|
0
|
|
|
|
|
|
my %hash; |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
open HD,$file or croak $!; |
47
|
0
|
|
|
|
|
|
while() { |
48
|
0
|
0
|
|
|
|
|
next if /^#|^$/; |
49
|
0
|
|
|
|
|
|
chomp; |
50
|
0
|
|
|
|
|
|
my ($k,$v) = split; |
51
|
0
|
|
|
|
|
|
$hash{$k} = $v; |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
|
close HD; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return \%hash; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub load_key { |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
my $key = shift; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
return undef unless defined $key; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $file = $self->{file}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
if ( ! -f $file ) { |
68
|
0
|
|
|
|
|
|
for (0..3) { |
69
|
0
|
|
|
|
|
|
select(undef,undef,undef,0.25); |
70
|
0
|
0
|
|
|
|
|
last if -f $file; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
croak "The config file seems not exists" unless -f $file; |
75
|
0
|
|
|
|
|
|
my $mtime = (stat($file))[9]; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if ( $mtime != $self->{mtime} ) { |
78
|
0
|
|
|
|
|
|
$self = __PACKAGE__->new($file,$self->{construct}); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $self->{c}->{$key}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Config::Autoload - Autoloads the config file whenever it is changed |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 VERSION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Version 0.01 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 SYNOPSIS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
use Config::Autoload; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $config = Config::Autoload->new("/path/sample.conf"); |
101
|
|
|
|
|
|
|
my $value = $config->load_key('key'); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# or |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $config = Config::Autoload->new("/path/sample.conf",\&construct); |
106
|
|
|
|
|
|
|
my $value = $config->load_key('key'); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub construct { |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $file = shift; |
111
|
|
|
|
|
|
|
my %hash; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
open FD,$file or die $!; |
114
|
|
|
|
|
|
|
while() { |
115
|
|
|
|
|
|
|
next if /^#|^$/; |
116
|
|
|
|
|
|
|
chomp; |
117
|
|
|
|
|
|
|
my ($k,$v) = split/ = /,$_; |
118
|
|
|
|
|
|
|
$hash{$k} =$v; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
close FD; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
\%hash; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 METHODS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 new() |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Create an object. The full path to the config file is required. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my $config = Config::Autoload->new("/path/sample.conf"); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
If the second argument is ignored, new() uses a defalut construct() |
135
|
|
|
|
|
|
|
to built up a hash for storing the keys/values in the config file. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
The default config file should be like: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
host 192.168.1.100 |
140
|
|
|
|
|
|
|
port 1234 |
141
|
|
|
|
|
|
|
user guest |
142
|
|
|
|
|
|
|
pass mypasswd |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
If you are using the different style of config file, you could built |
145
|
|
|
|
|
|
|
up your own construct() and pass it to the new() method. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
For example, the config file looks like: |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
host = 192.168.1.100 |
150
|
|
|
|
|
|
|
port = 1234 |
151
|
|
|
|
|
|
|
user = guest |
152
|
|
|
|
|
|
|
pass = mypasswd |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Then a construct() for it could be: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub construct { |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $file = shift; # you just shift it |
159
|
|
|
|
|
|
|
my %hash; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
open FD,$file or die $!; |
162
|
|
|
|
|
|
|
while() { |
163
|
|
|
|
|
|
|
next if /^#|^$/; |
164
|
|
|
|
|
|
|
chomp; |
165
|
|
|
|
|
|
|
my ($k,$v) = split/ = /,$_; |
166
|
|
|
|
|
|
|
$hash{$k} =$v; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
close FD; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
\%hash; # requires a hashref to be returned |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
And pass the reference of this subroutine to new(): |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
my $config = Config::Autoload->new("/path/sample.conf",\&construct); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 load_key() |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Load the value with a key from the config file. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $value = $config->load_key('key'); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
I primarily used this module for mod_perl, under which the object exists |
185
|
|
|
|
|
|
|
for long time. Whenever the config file was changed, load_key() method will |
186
|
|
|
|
|
|
|
get the updated value. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 AUTHOR |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Jeff Pang |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 BUGS/LIMITATIONS |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
If you have found bugs, please send email to |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SUPPORT |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
perldoc Config::Autoload |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Copyright 2010 Jeff Pang, all rights reserved. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
211
|
|
|
|
|
|
|
it under the same terms as Perl itself. |