| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Config::Layered::Source::ENV; |
|
2
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
|
3
|
|
|
|
|
23
|
|
|
|
3
|
|
|
|
|
98
|
|
|
3
|
3
|
|
|
3
|
|
15
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
113
|
|
|
4
|
3
|
|
|
3
|
|
15
|
use Storable qw( dclone ); |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
179
|
|
|
5
|
3
|
|
|
3
|
|
19
|
use base 'Config::Layered::Source'; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
1207
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub get_config { |
|
8
|
42
|
|
|
42
|
0
|
61
|
my ( $self ) = @_; |
|
9
|
|
|
|
|
|
|
|
|
10
|
42
|
|
|
|
|
114
|
my $struct = dclone($self->layered->default); |
|
11
|
42
|
|
|
|
|
76
|
my $config = {}; |
|
12
|
42
|
|
50
|
|
|
137
|
$self->args->{prefix} ||= "CONFIG"; |
|
13
|
|
|
|
|
|
|
|
|
14
|
42
|
|
|
|
|
52
|
for my $key ( keys %{$struct} ) { |
|
|
42
|
|
|
|
|
136
|
|
|
15
|
109
|
100
|
|
|
|
263
|
$config->{$key} = $ENV{ $self->args->{prefix} . "_" . uc($key) } |
|
16
|
|
|
|
|
|
|
if exists $ENV{ $self->args->{prefix} . "_" . uc($key) }; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
|
|
|
|
|
|
|
|
19
|
42
|
|
|
|
|
76
|
for my $key ( @{$self->args->{params}} ) { |
|
|
42
|
|
|
|
|
103
|
|
|
20
|
0
|
0
|
|
|
|
0
|
$config->{$key} = $ENV{ $self->args->{prefix} . "_" . uc($key) } |
|
21
|
|
|
|
|
|
|
if exists $ENV{ $self->args->{prefix} . "_" . uc($key) }; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
42
|
|
|
|
|
816
|
return $config; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
1; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 NAME |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Config::Layered::Source::ENV - The Environment Variable Source |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
The ENV source provides configuration through environment variables. |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
For each top-level key in the default data structure, it checks for the |
|
38
|
|
|
|
|
|
|
environment variable C where $KEY is the name of the key used |
|
39
|
|
|
|
|
|
|
in the default data structure. |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 EXAMPLE |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $config = Config::Layered->load_config( |
|
44
|
|
|
|
|
|
|
default => { |
|
45
|
|
|
|
|
|
|
foo => "bar", |
|
46
|
|
|
|
|
|
|
blee => "baz", |
|
47
|
|
|
|
|
|
|
bax => { |
|
48
|
|
|
|
|
|
|
chicken => "eggs", |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
With the above configuration, the following keys will be checked: |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=over 4 |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item * CONFIG_FOO |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item * CONFIG_BLEE |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item * CONFIG_BAX |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=back |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
The following would *NOT* be checked: |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=over 4 |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item * CONFIG_CHICKEN |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=back |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Given the above default data structure, a command run as |
|
74
|
|
|
|
|
|
|
C would result in a |
|
75
|
|
|
|
|
|
|
C<$config> structure like this: |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
{ |
|
79
|
|
|
|
|
|
|
foo => "Hello World", |
|
80
|
|
|
|
|
|
|
blee => "baz", |
|
81
|
|
|
|
|
|
|
bax => { |
|
82
|
|
|
|
|
|
|
chicken => "eggs", |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 SOURCE ARGUMENTS |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=over 4 |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item * params is an array ref of keys to check Default: Keys of the default |
|
90
|
|
|
|
|
|
|
data structure. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item * prefix is a word prepended to your key that is used to check |
|
93
|
|
|
|
|
|
|
C<$ENV{$prefix . "_" . uc($key) }>. |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=back |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Example: |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Config::Layered->load_config( |
|
100
|
|
|
|
|
|
|
sources => [ |
|
101
|
|
|
|
|
|
|
'ENV' => { prefix => "MYAPP", params => [qw( bar blee )] } |
|
102
|
|
|
|
|
|
|
], |
|
103
|
|
|
|
|
|
|
default => { debug => 0, verbose => 1 }, |
|
104
|
|
|
|
|
|
|
); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
The following keys would be checked: |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=over 4 |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item * MYAPP_BAR |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * MYAPP_BLEE |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item * MYAPP_DEBUG |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item * MYAPP_VERBOSE |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=back |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |