Banal-Config
view release on metacpan or search on metacpan
lib/Banal/Config.pm view on Meta::CPAN
use Banal::Config::General::Extended;
use Banal::Utils::Data qw(banal_get_data);
use Moose;
has 'verbose' => (is => 'rw', lazy_build=>1);
has 'debug' => (is => 'rw', lazy_build=>1);
has 'switches' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # Typically contains command line switches as produced by Getopt::Long or Getopt::Descriptive, or something that resembles it.
has 'options' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # The hash that gets passed to "new" for the actual configuration object (xcfg). The 'ConfigFile' option will default to the value of 'source' property.
has 'source' => (is => 'rw', isa => 'Str', lazy_build=>1); # The path to the configuration file. If it's not set, it will be guessed based on the name of the running process ($0). Note that this may be overriden by the "-ConfigFile" option.
has 'xcfg_class' => (is => 'rw', isa => 'Str', default=>'Banal::Config::General::Extended' );
has 'xcfg' => (
is => 'rw',
isa => 'Banal::Config::General::Extended',
lazy_build => 1,
handles => [qw (obj value hash array is_hash is_array is_scalar exists keys delete configfile find)],
);
has 'cfg_hash' => (is => 'rw', lazy_build=>1);
lib/Banal/Config.pm view on Meta::CPAN
my $self = shift;
return $self->get_default_config_term();
}
#***************************************************
#Â Less likely overrides
#***************************************************
#-----------------------------------------------
sub guess_config_file_path {
my $self = shift;
my $args = $self->switches;
$args = {%$args, @_}; # swicth overrides are possible by passing arguments to the function.
# If we have an explicit argument for the config file path, return that.
foreach my $opt ($self->get_possible_option_names_for_config_file_path(@_)) {
my $p = $args->{$opt};
return $p if ($p);
}
lib/Banal/Config.pm view on Meta::CPAN
#--------------------------------------
sub _build_debug {
my $self = shift;
return ($self->verbose >= 7);
}
#--------------------------------------
sub _build_source {
my $self = shift;
return $self->guess_config_file_path();
}
#--------------------------------------
sub _build_xcfg {
my $self = shift;
return $self->load();
}
#--------------------------------------
sub _build_cfg_hash {
lib/Banal/Config.pm view on Meta::CPAN
- for generating the default configuration context within the config file.
By default, returns the base name of the main program (script).
Can be overridden.
=head2 get_default_config_file_base_name()
The default base name of the configuration file, which will be searched in several places when trying to "guess" the config file path.
This would only be needed when there is no explicit config file path given.
By default, simply calls "get_default_config_term()".
=head2 guess_config_file_path()
A call to this class method is made in order to build the default value of the "source" attribute, which will be used as the source path for the config file UNLESS one is explicetly given in the options argument to new().
The current implementation goes as follows:
- It will first try suitable "switches". If one that designates the config fie path is defined, the that one will be return. By default, here are those switches that will be checked for definedness:
cfg_[%TERM%] [%TERM%]_cfg cfg
where TERM is obtained by a call to get_default_config_term()
- Then, we will see if there is an ENVIRONMENT variable,
lib/Banal/Config.pm view on Meta::CPAN
"./test/etc/" . $base_name . ".conf", # this one is for testing purposes during "make test"
"~/." . $base_name . ".conf",
"/etc/" . $base_name . ".conf",
"." . $base_name . ".conf",
where $base_name is obtained by a call to get_default_config_file_base_name()
=head2 get_possible_option_names_for_config_file_path
Used by guess_config_file_path() to check for command line switches.
Currently returns the list: cfg_[%TERM%] [%TERM%]_cfg cfg
where TERM is obtained by a call to get_default_config_term()
=head2 get_possible_environment_variable_names_for_config_file_path
Used by guess_config_file_path() to check for ENVIRONMENT variables.
Currently returns the list: [%TERM%]_CFG CFG_[%TERM%]
where TERM is obtained by a call to get_default_config_term()
=head2 get_possible_config_file_paths()
Used by guess_config_file_path() after trying command line swictches and ENV variables.
At this point (when everything else is exhausted), the first file that exists in the list returned by this function will be used as the config file.
Currently returns the list:
"./test/etc/" . $base_name . ".conf", # this one is for testing purposes during "make test"
"~/." . $base_name . ".conf",
"/etc/" . $base_name . ".conf",
"." . $base_name . ".conf",
where $base_name is obtained by a call to get_default_config_file_base_name()
( run in 0.708 second using v1.01-cache-2.11-cpan-702932259ff )