Config-Hierarchical
view release on metacpan or search on metacpan
t/099_Cookbook.t view on Meta::CPAN
$config->Set(NAME => 'CC', VALUE => 'gcc') ;
$config->Set(NAME => 'LD', VALUE => 'ld') ;
print "Value for 'CC' is '" . $config->Get(NAME => 'CC') . "'.\n" ;
print "'LD' exists.\n" if $config->Exists(NAME => 'LD') ;
Would display:
=begin hidden
my $cc_value = $config->Get(NAME => 'CC') ;
is($cc_value, 'gcc', 'Get returns right value') ;
generate_pod("\tValue for 'CC' is '$cc_value'.\n") ; # note the \t in the string so the pod is presented properly
generate_pod("\t'LD' exists.\n") if $config->Exists(NAME => 'LD') ;
generate_pod("\n") ;
=end hidden
=head3 Set the same variable multiple time with different values
$config->Set(NAME => 'CC', VALUE => 'gcc') ;
$config->Set(NAME => 'CC', VALUE => 'cl') ;
print "Value for 'CC' is '" . $config->Get(NAME => 'CC') . "'.\n\n" ;
Would display:
=begin hidden
$cc_value = $config->Get(NAME => 'CC') ;
is($cc_value, 'cl', 'Get returns right value') ;
generate_pod("\tValue for 'CC' is '$cc_value'.\n\n") ;
=end hidden
B<Config::Hierarchical> does not generate any warning when you override a variable's value. If you would like to get an error,
lock the variable.
=head4 Locking variables
=begin not_tested
$config->Set(NAME => 'CC', VALUE => 'gcc') ;
$config->Lock(NAME => 'CC') ;
$config->Set(NAME => 'CC', VALUE => 'cl') ;
=end not_tested
This would generate the following error followed by a stack trace.
=begin hidden
# the code above generates an error and dies so we can't run it directly in a common section
dies_ok
{
$config->Set(NAME => 'CC', VALUE => 'gcc') ;
$config->Lock(NAME => 'CC') ;
$config->Set(NAME => 'CC', VALUE => 'cl') ;
} 'Setting locked variable' ;
$die =~ s/^/\t/gm ;
generate_pod($die . "\n") ;
$cc_value = $config->Get(NAME => 'CC') ;
is($cc_value, 'gcc', 'Get returns right value') ;
=end hidden
=head4 Setting Locked variables
$config->Set(FORCE_LOCK => 1, NAME => 'CC', VALUE => 'cl') ;
print "Value for 'CC' is '" . $config->Get(NAME => 'CC') . "'.\n\n" ;
Would display:
=begin hidden
$cc_value = $config->Get(NAME => 'CC') ;
is($cc_value, 'cl', 'Get returns right value') ;
generate_pod("\t$warnings\n") ;
generate_pod("\tValue for 'CC' is '$cc_value'.\n\n") ;
=end hidden
=head4 Getting the lock state
print "'CC' is locked.\n" if $config->IsLocked(NAME => 'CC') ;
print "'LD' is locked.\n" if $config->IsLocked(NAME => 'LD') ;
Would display:
=begin hidden
is($config->IsLocked(NAME => 'LD'), 0, 'config not locked') ;
is($config->IsLocked(NAME => 'CC'), 1, 'config locked') ;
generate_pod("\t'CC' is locked.\n") if $config->IsLocked(NAME => 'CC') ;
generate_pod("\t'LD' is locked.\n") if $config->IsLocked(NAME => 'LD') ;
generate_pod("\n") ;
=end hidden
=for POD::Tested reset
=head2 Setting variable in the constructor
use Config::Hierarchical ;
my $config = new Config::Hierarchical
(
NAME => 'Config initialized in constructor',
INITIAL_VALUES =>
[
{NAME => 'CC', VALUE => 1},
{NAME => 'CC', VALUE => 2},
{NAME => 'LD', VALUE => 3, LOCK => 1},
{NAME => 'AS', VALUE => 4, LOCK => 1},
],
) ;
=begin hidden
my ($warnings, $die) ;
$config->{INTERACTION}{WARN} = sub{$warnings = join('', @_) ; use Carp ;carp $warnings; } ;
$config->{INTERACTION}{DIE} = sub{$die= join('', @_) ; use Carp ;croak $die} ;
is($config->IsLocked(NAME => 'CC'), 0, 'config not locked') ;
is($config->IsLocked(NAME => 'LD'), 1, 'config locked') ;
is($config->Get(NAME => 'CC'), '2', 'initialized ok') or diag $config->GetDump();
is($config->Get(NAME => 'LD'), '3', 'initialized ok') ;
is($config->Get(CATEGORY => 'CURRENT', NAME => 'AS'), 4, 'initialized ok') ;
is($config->Exists(NAME => 'AS'), 1, 'exist') ;
=end hidden
=head2 Getting a non existing variable value
my $value = $config->Get(NAME => 'NON_EXISTING') ;
print "Value for 'NON_EXISTING' is defined\n" if defined $value ;
Would display:
=begin hidden
generate_pod("\t$warnings\n") ;
generate_pod("Value for 'NON_EXISTING' is defined\n") if defined $value ;
=end hidden
=head2 Getting multiple variable values
my @variables = qw(CC LD AS) ;
my %values ;
@values{@variables} = $config->GetMultiple(@variables) ;
use Data::TreeDumper ;
my $dump = DumpTree \%values, 'Variables', INDENTATION => "\t", DISPLAY_ADDRESS => 0 ;
print $dump ;
Would display:
=begin hidden
is_deeply(\%values, {CC => 2, LD => 3, AS => 4}, 'GetMultiple OK') ;
generate_pod("$dump\n") ;
generate_pod("\n") ;
=end hidden
=head2 Variable Attribute
You can attach an attribute to a configuration variable. If the same variable exists in different categories,
each the variables have a separate attribute. Changing a variable value doesn't change the attribute.
$config->SetAttribute(NAME => 'CC', VALUE => 'attribute') ;
$config->Set(NAME => 'CC', VALUE => 'some_compiler') ;
my ($attribute, $attribute_exists) = $config->GetAttribute(NAME => 'CC') ;
=begin hidden
is($attribute, 'attribute') ;
( run in 1.010 second using v1.01-cache-2.11-cpan-39bf76dae61 )