| 1 | package SRS::EPP::Proxy::SimpleConfig; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | with 'SRS::EPP::Proxy::ConfigFromFile'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Config::Any (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | sub get_config_from_file { | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | my ($class, $file) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_config_from_file: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | my $files_ref = ref $file eq 'ARRAY' ? $file : [$file]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | my $can_config_any_args = $class->can('config_any_args'); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $extra_args = $can_config_any_args ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | $can_config_any_args->($class, $file) : {}; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my $raw_cfany = Config::Any->load_files({ | |||||||||||||||||||||||||||||||||||||||||
| 18 | %$extra_args, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | use_ext => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | files => $files_ref, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | flatten_to_hash => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | } ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my %raw_config; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | foreach my $file_tested ( reverse @{$files_ref} ) { | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ( ! exists $raw_cfany->{$file_tested} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $cfany_hash = $raw_cfany->{$file_tested}; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | die "configfile must represent a hash structure in file: $file_tested" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$cfany_hash and ref $cfany_hash and ref $cfany_hash eq "HASH"
$cfany_hash and ref $cfany_hash
MC/DC0%0/3
$cfany_hash and ref $cfany_hash and ref $cfany_hash eq "HASH"
$cfany_hash
ref $cfany_hash
ref $cfany_hash eq "HASH"
Truth table0%0/4
A and B and C
A= $cfany_hashB= ref $cfany_hashC= ref $cfany_hash eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | %raw_config = ( %raw_config, %{$cfany_hash} ); | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | \%raw_config; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | no Moose::Role; 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | ## A YAML configfile named /etc/my_app.yaml: | ||||||||||||||||||||||||||||||||||||||||||
| 53 | foo: bar | ||||||||||||||||||||||||||||||||||||||||||
| 54 | baz: 123 | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | ## In your class | ||||||||||||||||||||||||||||||||||||||||||
| 57 | package My::App; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | with 'MooseX::SimpleConfig'; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # ... rest of the class here | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | ## in your script | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | use My::App; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # ... rest of the script here | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | #################### | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ###### combined with MooseX::Getopt: | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | ## In your class | ||||||||||||||||||||||||||||||||||||||||||
| 79 | package My::App; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | with 'MooseX::SimpleConfig'; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | with 'MooseX::Getopt'; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # ... rest of the class here | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | ## in your script | ||||||||||||||||||||||||||||||||||||||||||
| 91 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | use My::App; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | my $app = My::App->new_with_options(); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # ... rest of the script here | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | ## on the command line | ||||||||||||||||||||||||||||||||||||||||||
| 99 | % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | This role loads simple configfiles to set object attributes. It | ||||||||||||||||||||||||||||||||||||||||||
| 104 | is based on the abstract role L<MooseX::ConfigFromFile>, and uses | ||||||||||||||||||||||||||||||||||||||||||
| 105 | L<Config::Any> to load your configfile. L<Config::Any> will in | ||||||||||||||||||||||||||||||||||||||||||
| 106 | turn support any of a variety of different config formats, detected | ||||||||||||||||||||||||||||||||||||||||||
| 107 | by the file extension. See L<Config::Any> for more details about | ||||||||||||||||||||||||||||||||||||||||||
| 108 | supported formats. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this | ||||||||||||||||||||||||||||||||||||||||||
| 111 | module is automatically supported by the L<MooseX::Getopt> role as | ||||||||||||||||||||||||||||||||||||||||||
| 112 | well, which allows specifying C<-configfile> on the commandline. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 configfile | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | Provided by the base role L<MooseX::ConfigFromFile>. You can | ||||||||||||||||||||||||||||||||||||||||||
| 119 | provide a default configfile pathname like so: | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | has +configfile ( default => '/etc/myapp.yaml' ); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | You can pass an array of filenames if you want, but as usual the array | ||||||||||||||||||||||||||||||||||||||||||
| 124 | has to be wrapped in a sub ref. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | has +configfile ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | Config files are trivially merged at the top level, with the right-hand files taking precedence. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =head2 new_with_config | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Provided by the base role L<MooseX::ConfigFromFile>. Acts just like | ||||||||||||||||||||||||||||||||||||||||||
| 135 | regular C<new()>, but also accepts an argument C<configfile> to specify | ||||||||||||||||||||||||||||||||||||||||||
| 136 | the configfile from which to load other attributes. Explicit arguments | ||||||||||||||||||||||||||||||||||||||||||
| 137 | to C<new_with_config> will override anything loaded from the configfile. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head2 get_config_from_file | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Called internally by either C<new_with_config> or L<MooseX::Getopt>'s | ||||||||||||||||||||||||||||||||||||||||||
| 142 | C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | Brandon L. Black, E<lt>blblack@gmail.comE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 151 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||||||||||||||||