General::Extended(3pm) | User Contributed Perl Documentation | General::Extended(3pm) |
Config::General::Extended - Extended access to Config files
use Config::General; $conf = Config::General->new( -ConfigFile => 'configfile', -ExtendedAccess => 1 );
This is an internal module which makes it possible to use object oriented methods to access parts of your config file.
Normally you don't call it directly.
<individual> <martin> age 23 </martin> <joseph> age 56 </joseph> </individual> <other> blah blubber blah gobble leer </other>
and already read it in using Config::General::Extended::new(), then you can get a new object from the "individual" block this way:
$individual = $conf->obj("individual");
Now if you call getall on $individual (just for reference) you would get:
$VAR1 = ( martin => { age => 13 } );
Or, here is another use:
my $individual = $conf->obj("individual"); foreach my $person ($conf->keys("individual")) { $man = $individual->obj($person); print "$person is " . $man->value("age") . " years old\n"; }
See the discussion on hash() and value() below.
If the key from which you want to create a new object is empty, an empty object will be returned. If you run the following on the above config:
$obj = $conf->obj("other")->obj("leer");
Then $obj will be empty, just like if you have had run this:
$obj = Config::General::Extended->new( () );
Read operations on this empty object will return nothing or even fail. But you can use an empty object for creating a new config using write operations, i.e.:
$obj->someoption("value");
See the discussion on AUTOLOAD METHODS below.
If the key points to a list of hashes, a list of objects will be returned. Given the following example config:
<option> name = max </option> <option> name = bea </option>
you could write code like this to access the list the OOP way:
my $objlist = $conf->obj("option"); foreach my $option (@{$objlist}) { print $option->name; }
Please note that the list will be returned as a reference to an array.
Empty elements or non-hash elements of the list, if any, will be skipped.
my %sub_hash = $conf->hash("individual"); print Dumper(\%sub_hash); $VAR1 = { martin => { age => 13 } };
$other = $conf->obj("other"); my @blahs = $other->array("blah"); print Dumper(\@blahs); $VAR1 = [ "blubber", "gobble" ];
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"!
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"; }
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 foreach loops as seen in an example above(obj() ).
For example, given the values find (qw (A B C)) and the following tree (</end> tags omitted for brevity):
<A> <FOO> ... <B> <BAZ> ... <C> BAR = shoo
find() will find the object at C with the value BAR = shoo and return it.
Another useful feature is implemented in this class using the 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:
my $conf = Config::General::Extended->new("configfile"); my $person = $conf->obj("person"); print $person->prename . " " . $person->name . " is " . $person->age . " years old\n";
This notation supports only scalar values! You need to make sure, that the block <person> does not contain any subblock or multiple identical options(which will become an array after parsing)!
If you access a non-existent key this way, Config::General will croak an error. You can turn this behavior off by setting -StrictObjects to 0 or "no". In this case undef will be returned.
Of course you can use this kind of methods for writing data too:
$person->name("Neustein");
This changes the value of the "name" key to "Neustein". This feature behaves exactly like value(), which means you can assign hash or array references as well and that existing values under the given key will be overwritten.
Copyright (c) 2000-2022 Thomas Linden
This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.
none known yet.
Thomas Linden <tlinden |AT| cpan.org>
2.07
2022-10-13 | perl v5.34.0 |