| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Config::KeyValue; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
76372
|
use warnings; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
72
|
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
78
|
|
|
5
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
1312
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Config::KeyValue - Module for reading simple "KEY=VALUE" formatted configuration files. |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.02 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Config::KeyValue; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Instantiate |
|
25
|
|
|
|
|
|
|
my $cfg = Config::KeyValue->new(); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Parse file, returning found key-value pairs |
|
28
|
|
|
|
|
|
|
my $parsed_config = $cfg->load_file('/path/to/your/config/file'); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Fetch a specific key |
|
31
|
|
|
|
|
|
|
print $cfg->get('SOME_CONFIGURATON_KEY'), "\n"; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Fetch a specific key with leading-and-trailing quotes removed |
|
34
|
|
|
|
|
|
|
print $cfg->get_tidy('SOME_CONFIGURATON_KEY'), "\n"; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 new() |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Constructor. |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
|
45
|
1
|
|
|
1
|
1
|
17
|
my $self = {}; |
|
46
|
1
|
|
|
|
|
5
|
$self->{CONFIG} = {}; # Start with an empty hash of configuration key-values |
|
47
|
1
|
|
|
|
|
3
|
bless($self); |
|
48
|
1
|
|
|
|
|
5
|
return $self; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 get(key) |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Get configuration value for I. Returns an empty string if I is not defined. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub get { |
|
59
|
6
|
|
|
6
|
1
|
1384
|
my ($self, $key) = @_; |
|
60
|
6
|
|
50
|
|
|
56
|
return $self->{CONFIG}{ $key } || ''; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 get_tidy(key) |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Get configuration value for I, stripping leading and trailing matching quote characters |
|
66
|
|
|
|
|
|
|
(e.g. I<'>, I<">). Returns an empty string if I is not defined. |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub get_tidy { |
|
71
|
6
|
|
|
6
|
1
|
18
|
my ($self, $key) = @_; |
|
72
|
6
|
|
50
|
|
|
123
|
my $value = $self->{CONFIG}{ $key } || ''; |
|
73
|
6
|
|
|
|
|
96
|
$value =~ s/^'(.+)'$/$1/; # Trim matched single quotes |
|
74
|
6
|
|
|
|
|
21
|
$value =~ s/^"(.+)"$/$1/; # Trim matched double quotes |
|
75
|
6
|
|
|
|
|
36
|
return $value; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 load_file(file_name) |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Read configuration information from I. Returns hashref of configuration key=value |
|
81
|
|
|
|
|
|
|
pairs. |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub load_file { |
|
86
|
3
|
|
|
3
|
1
|
4473
|
my ($self, $file_name) = @_; |
|
87
|
|
|
|
|
|
|
|
|
88
|
3
|
|
|
|
|
8
|
my $cfg = {}; |
|
89
|
3
|
100
|
|
|
|
456
|
open(my $fh, '<', $file_name) or croak("could not open file for reading: '$file_name'"); |
|
90
|
2
|
|
|
|
|
41
|
while (my $l=<$fh>) { |
|
91
|
22
|
100
|
|
|
|
83
|
next if ($l =~ /^#/); |
|
92
|
20
|
100
|
|
|
|
204
|
if ($l =~ /^(\S+)=(.+)$/) { |
|
93
|
12
|
|
|
|
|
42
|
my ($k, $v) = ($1, $2); |
|
94
|
12
|
|
|
|
|
104
|
$v =~ s/\s*#.*$//; # remove trailing whitespace and comment |
|
95
|
12
|
|
|
|
|
78
|
$cfg->{ $k } = $v; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
} |
|
98
|
2
|
50
|
|
|
|
34
|
close($fh) or croak("could not close file: '$file_name'"); |
|
99
|
2
|
|
|
|
|
83
|
$self->{CONFIG} = $cfg; # replace the current CONFIG hashref |
|
100
|
2
|
|
|
|
|
17
|
return $cfg; # return what we extracted from configuration file |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 AUTHOR |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
blair christensen, C<< >> |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 BUGS |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
112
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
113
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SUPPORT |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
perldoc Config::KeyValue |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
You can also look for information at: |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=over 4 |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
L |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
L |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item * Search CPAN |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Copyright 2008 blair christensen, all rights reserved. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
154
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; # End of Config::KeyValue |