Config::Context::ConfigScoped - Use Config::Scoped config files with Config::Context
use Config::Context; my $config_text = ' Location /users { user_area = 1 } LocationMatch '\.*(jpg|gif|png)$' { image_file = 1 } '; my $conf = Config::Context->new( string => $config_text, driver => 'ConfigScoped', match_sections => [ { name => 'Location', match_type => 'path', }, { name => 'LocationMatch', match_type => 'regex', }, ], ); my %config = $conf->context('/users/~mary/index.html'); use Data::Dumper; print Dumper(\%config); -------- $VAR1 = { 'title' => 'User Area', 'image_file' => undef, }; my %config = $conf->context('/users/~biff/images/flaming_logo.gif'); print Dumper(\%config); -------- $VAR1 = { 'title' => 'User Area', 'image_file' => 1, };
This module uses Config::Scoped to parse config files for Config::Context.
Config::Scoped
Config::Context
In addition to the options normally enabled by Config::Scoped, the following options are turned on by default:
warnings => { parameter => 'off', declaration => 'off', }
You can change this behaviour by passing a different value to driver_params to new:
driver_params
new
my $conf = Config::Context->new( driver => 'ConfigScoped', driver_options => { ConfigScoped = > { warnings => { parameter => 'on', } }, }, );
my $driver = Config::Context::ConfigScoped->new( file => $config_file, lower_case_names => 1, # optional options => { # ... } );
or:
my $driver = Config::Context::ConfigScoped->new( string => $config_string, lower_case_names => 1, # optional options => { # ... } );
Returns a new driver object, using the provided options.
Returns the data structure for the parsed config.
Returns a list of all the config files read, including any config files included in the main file.
Returns the modules used to parse the config. In this case: Config::Scoped
When one Config::Scoped file includes another, and they both contain declarations, the declarations are merged. For instance:
# config1.conf %include config2.conf section /users { title = 'Members Area'; } # config2.conf section /users { user_area = 1; title = 'users area'; }
This will result in:
{ 'section' => { '/users' => { 'user_area' => '1', 'title' => 'Members Area', }, }, }
However, if you use hashes instead of declarations, then the second hash with the same name will completely overwrite the first one. For instance:
# config1.conf %include config2.conf section = { '/users' = { title = 'Members Area'; } } # config2.conf section = { '/users' = { user_area = 1; title = 'users area'; } }
{ 'section' => { '/users' => { 'title' => 'Members Area', }, }, }
This is important to keep in mind because Config::Scoped does not allow nested declarations.
When using the Config::Context::ConfigScoped driver, you must be careful with the use of the default section, since Config::Scoped does its own inheritance from the global scope into named sections. See the documentation for Config::Context::ConfigScoped for more information.
Config::Context::ConfigScoped
So for instance, the following will not work as expected.
private_area = 0 image_file = 0 LocationMatch admin { private_area = 1 } LocationMatch '\.(gif)|(jpg)|(png)$' { image_file = 1 }
Since this is equivalent to:
LocationMatch admin { image_file = 0 private_area = 1 } LocationMatch '\.(gif)|(jpg)|(png)$' { private_area = 0 image_file = 1 }
Values set in the default section are inherited into sections before those sections are matched.
One solution is to use hashes, rather than declarations:
private_area = 0 image_file = 0 LocationMatch = { admin = { private_area = 1 } '\.(gif)|(jpg)|(png)$' = { image_file = 1 } }
This works, because in Config::Scoped, hashes do not inherit parameters from their enclosing scope.
However, note that when two hashes with the same name collide, their values are not merged together. Instead, one hash replaces the other hash. See above under "Limitations of hash merging with included files".
If you use the lower_case_names option, be aware that it also affects the names of declaration blocks. For instance, the following configuration,
Location /FOO { Some_Param = 'Some Value' }
becomes:
{ 'location' => { '/foo' => { 'some param' => 'Some Value'; } } }
Unless you expect this behaviour (which you probably don't), you should probably avoid using the lower_case_names option with Config::Scoped.
lower_case_names
Normally, if there are no declarations in a Config::Scope file, all configuration is placed under a key called _GLOBAL. Config::Context::ConfigScoped detects this condition and moves the data under _GLOBAL up a level. Basically, it does the equivalent of:
Config::Scope
_GLOBAL
$config = $config->{_GLOBAL};
The reason for this is to allow use of hashes instead of declarations to enable default values. See "Limitations of hash merging with included files", above.
Instead of:
LocationMatch \.*(jpg|gif|png)$ { # some configuration }
use:
LocationMatch '\.*(jpg|gif|png)$' { # some configuration }
Note that regular expression characters don't have to be quoted beyond this.
Config::Context CGI::Application::Plugin::Config::Context Config::Scoped
Copyright 2004-2005 Michael Graham, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Config::Context, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Context
CPAN shell
perl -MCPAN -e shell install Config::Context
For more information on module installation, please visit the detailed CPAN module installation guide.