Bundle-PBib
view release on metacpan or search on metacpan
lib/Config/General/Extended.pm view on Meta::CPAN
$obj = new Config::General::Extended( () );
Read operations on this empty object will return nothing or even fail.
But you can use an empty object for I<creating> a new config using write
operations, i.e.:
$obj->someoption("value");
See the discussion on B<AUTOLOAD METHODS> below.
=item hash('key')
This method returns a hash(if it B<is> one!) from the config which is referenced by
"key". Given the sample config above you would get:
my %sub_hash = $conf->hash("individual");
print Dumper(\%sub_hash);
$VAR1 = {
martin => { age => 13 }
};
=item array('key')
This the equivalent of B<hash()> mentioned above, except that it returns an array.
Again, we use the sample config mentioned above:
$other = $conf->obj("other");
my @blahs = $other->array("blah");
print Dumper(\@blahs);
$VAR1 = [ "blubber", "gobble" ];
=item value('key')
This method returns the scalar value of a given key. Given the following sample
config:
name = arthur
age = 23
you could do something like that:
print $conf->value("name") . " is " . $conf->value("age") . " years old\n";
You can use this method also to set the value of "key" to something if you give over
a hash reference, array reference or a scalar in addition to the key. An example:
$conf->value("key", \%somehash);
# or
$conf->value("key", \@somearray);
# or
$conf->value("key", $somescalar);
Please note, that this method does not complain about existing values within "key"!
=item is_hash('key') is_array('key') is_scalar('key')
As seen above, you can access parts of your current config using hash, array or scalar
methods. But you are right if you guess, that this might become problematic, if
for example you call B<hash()> on a key which is in real not a hash but a scalar. Under
normal circumstances perl would refuse this and die.
To avoid such behavior you can use one of the methods is_hash() is_array() is_scalar() to
check if the value of "key" is really what you expect it to be.
An example(based on the config example from above):
if($conf->is_hash("individual") {
$individual = $conf->obj("individual");
}
else {
die "You need to configure a "individual" block!\n";
}
=item exists('key')
This method returns just true if the given key exists in the config.
=item keys('key')
Returns an array of the keys under the specified "key". If you use the example
config above you yould do that:
print Dumper($conf->keys("individual");
$VAR1 = [ "martin", "joseph" ];
If no key name was supplied, then the keys of the object itself will be returned.
You can use this method in B<foreach> loops as seen in an example above(obj() ).
=item delete ('key')
This method removes the given key and all associated data from the internal
hash structure. If 'key' contained data, then this data will be returned,
otherwise undef will be returned.
=back
=head1 AUTOLOAD METHODS
Another usefull feature is implemented in this class using the B<AUTOLOAD> feature
of perl. If you know the keynames of a block within your config, you can access to
the values of each individual key using the method notation. See the following example
and you will get it:
We assume the following config:
<person>
name = Moser
prename = Peter
birth = 12.10.1972
</person>
Now we read it in and process it:
( run in 0.497 second using v1.01-cache-2.11-cpan-39bf76dae61 )