| 1 | package Config::Layered::Source::ENV; | ||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use Storable qw( dclone ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use base 'Config::Layered::Source'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 42 | ▶ | sub get_config { | ||||||||||
|
Subroutine
get_config: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||
| 8 | 42 | my ( $self ) = @_; | |||||||||||
| 9 | |||||||||||||
| 10 | 42 | my $struct = dclone($self->layered->default); | |||||||||||
| 11 | 42 | my $config = {}; | |||||||||||
| 12 | 42 | ▶ | $self->args->{prefix} ||= "CONFIG"; | ||||||||||
|
Condition50%1/2
$self->args->{"prefix"} ||= "CONFIG"
MC/DC0%0/1
$self->args->{"prefix"} ||= "CONFIG"
$self->args->{"prefix"}
Truth table50%1/2
A
A= $self->args->{"prefix"}
| |||||||||||||
| 13 | |||||||||||||
| 14 | 42 | for my $key ( keys %{$struct} ) { | |||||||||||
| 15 | $config->{$key} = $ENV{ $self->args->{prefix} . "_" . uc($key) } | ||||||||||||
| 16 | 109 | ▶ | if exists $ENV{ $self->args->{prefix} . "_" . uc($key) }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | } | ||||||||||||
| 18 | |||||||||||||
| 19 | 42 | for my $key ( @{$self->args->{params}} ) { | |||||||||||
| 20 | $config->{$key} = $ENV{ $self->args->{prefix} . "_" . uc($key) } | ||||||||||||
| 21 | 0 | ▶ | if exists $ENV{ $self->args->{prefix} . "_" . uc($key) }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | 42 | return $config; | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | 3 | 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<CONFIG_$KEY> 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<CONFIG_FOO="Hello World" ./myprogram> 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 | ||||||||||||