2 # Config::General::Extended - special Class based on Config::General
4 # Copyright (c) 2000-2008 Thomas Linden <tlinden |AT| cpan.org>.
5 # All Rights Reserved. Std. disclaimer applies.
6 # Artificial License, same as perl itself. Have fun.
10 package Config::General::Extended;
12 # yes we need the hash support of new() in 1.18 or higher!
13 use Config::General 1.18;
18 use vars qw(@ISA @EXPORT);
20 # inherit new() and so on from Config::General
21 @ISA = qw(Config::General Exporter);
26 $Config::General::Extended::VERSION = "2.03";
30 croak "Deprecated method Config::General::Extended::new() called.\n"
31 ."Use Config::General::new() instead and set the -ExtendedAccess flag.\n";
37 # returns a config object from a given key
38 # or from the current config hash if the $key does not exist
39 # or an empty object if the content of $key is empty.
43 # just create the empty object, just in case
44 my $empty = $this->SUPER::new( -ExtendedAccess => 1, -ConfigHash => {}, %{$this->{Params}} );
46 if (exists $this->{config}->{$key}) {
47 if (!$this->{config}->{$key}) {
48 # be cool, create an empty object!
51 elsif (ref($this->{config}->{$key}) eq "ARRAY") {
53 foreach my $element (@{$this->{config}->{$key}}) {
54 if (ref($element) eq "HASH") {
56 $this->SUPER::new( -ExtendedAccess => 1,
57 -ConfigHash => $element,
61 if ($this->{StrictObjects}) {
62 croak "element in list \"$key\" does not point to a hash reference!\n";
64 # else: skip this element
69 elsif (ref($this->{config}->{$key}) eq "HASH") {
70 return $this->SUPER::new( -ExtendedAccess => 1,
71 -ConfigHash => $this->{config}->{$key}, %{$this->{Params}} );
75 if ($this->{StrictObjects}) {
76 croak "key \"$key\" does not point to a hash reference!\n";
79 # be cool, create an empty object!
85 # even return an empty object if $key does not exist
93 # returns a value of the config hash from a given key
94 # this can be a hashref or a scalar
96 my($this, $key, $value) = @_;
98 $this->{config}->{$key} = $value;
101 if (exists $this->{config}->{$key}) {
102 return $this->{config}->{$key};
105 if ($this->{StrictObjects}) {
106 croak "Key \"$key\" does not exist within current object\n";
118 # returns a value of the config hash from a given key
121 my($this, $key) = @_;
122 if (exists $this->{config}->{$key}) {
123 return %{$this->{config}->{$key}};
126 if ($this->{StrictObjects}) {
127 croak "Key \"$key\" does not exist within current object\n";
138 # returns a value of the config hash from a given key
141 my($this, $key) = @_;
142 if (exists $this->{config}->{$key}) {
143 return @{$this->{config}->{$key}};
145 if ($this->{StrictObjects}) {
146 croak "Key \"$key\" does not exist within current object\n";
157 # return true if the given key contains a hashref
159 my($this, $key) = @_;
160 if (exists $this->{config}->{$key}) {
161 if (ref($this->{config}->{$key}) eq "HASH") {
177 # return true if the given key contains an arrayref
179 my($this, $key) = @_;
180 if (exists $this->{config}->{$key}) {
181 if (ref($this->{config}->{$key}) eq "ARRAY") {
196 # returns true if the given key contains a scalar(or number)
198 my($this, $key) = @_;
199 if (exists $this->{config}->{$key} && !ref($this->{config}->{$key})) {
209 # returns true if the key exists
211 my($this, $key) = @_;
212 if (exists $this->{config}->{$key}) {
223 # returns all keys under in the hash of the specified key, if
224 # it contains keys (so it must be a hash!)
226 my($this, $key) = @_;
228 if (ref($this->{config}) eq "HASH") {
229 return map { $_ } keys %{$this->{config}};
235 elsif (exists $this->{config}->{$key} && ref($this->{config}->{$key}) eq "HASH") {
236 return map { $_ } keys %{$this->{config}->{$key}};
246 # delete the given key from the config, if any
247 # and return what is deleted (just as 'delete $hash{key}' does)
249 my($this, $key) = @_;
250 if (exists $this->{config}->{$key}) {
251 return delete $this->{config}->{$key};
260 # removed, use save() of General.pm now
263 # # save the config back to disk
265 # my($this,$file) = @_;
266 # my $fh = new FileHandle;
269 # $file = $this->{configfile};
272 # $this->save_file($file);
278 # sets or returns the config filename
280 my($this,$file) = @_;
282 $this->{configfile} = $file;
284 return $this->{configfile};
291 # returns the representing value, if it is a scalar.
293 my($this, $value) = @_;
294 my $key = $Config::General::Extended::AUTOLOAD; # get to know how we were called
295 $key =~ s/.*:://; # remove package name!
297 if (defined $value) {
298 # just set $key to $value!
299 $this->{config}->{$key} = $value;
301 elsif (exists $this->{config}->{$key}) {
302 if ($this->is_hash($key)) {
303 croak "Key \"$key\" points to a hash and cannot be automatically accessed\n";
305 elsif ($this->is_array($key)) {
306 croak "Key \"$key\" points to an array and cannot be automatically accessed\n";
309 return $this->{config}->{$key};
313 if ($this->{StrictObjects}) {
314 croak "Key \"$key\" does not exist within current object\n";
318 return undef; # bugfix rt.cpan.org#42331
337 Config::General::Extended - Extended access to Config files
344 $conf = new Config::General(
345 -ConfigFile => 'configfile',
351 This is an internal module which makes it possible to use object
352 oriented methods to access parts of your config file.
354 Normally you don't call it directly.
360 =item configfile('filename')
362 Set the filename to be used by B<save> to "filename". It returns the current
363 configured filename if called without arguments.
368 Returns a new object (of Config::General::Extended Class) from the given key.
370 Assume you have the following config:
386 and already read it in using B<Config::General::Extended::new()>, then you can get a
387 new object from the "individual" block this way:
389 $individual = $conf->obj("individual");
391 Now if you call B<getall> on I<$individual> (just for reference) you would get:
394 martin => { age => 13 }
397 Or, here is another use:
399 my $individual = $conf->obj("individual");
400 foreach my $person ($conf->keys("individual")) {
401 $man = $individual->obj($person);
402 print "$person is " . $man->value("age") . " years old\n";
405 See the discussion on B<hash()> and B<value()> below.
407 If the key from which you want to create a new object is empty, an empty
408 object will be returned. If you run the following on the above config:
410 $obj = $conf->obj("other")->obj("leer");
412 Then $obj will be empty, just like if you have had run this:
414 $obj = new Config::General::Extended( () );
416 Read operations on this empty object will return nothing or even fail.
417 But you can use an empty object for I<creating> a new config using write
420 $obj->someoption("value");
422 See the discussion on B<AUTOLOAD METHODS> below.
424 If the key points to a list of hashes, a list of objects will be
425 returned. Given the following example config:
434 you could write code like this to access the list the OOP way:
436 my $objlist = $conf->obj("option");
437 foreach my $option (@{$objlist}) {
441 Please note that the list will be returned as a reference to an array.
443 Empty elements or non-hash elements of the list, if any, will be skipped.
447 This method returns a hash(if it B<is> one!) from the config which is referenced by
448 "key". Given the sample config above you would get:
450 my %sub_hash = $conf->hash("individual");
451 print Dumper(\%sub_hash);
453 martin => { age => 13 }
458 This the equivalent of B<hash()> mentioned above, except that it returns an array.
459 Again, we use the sample config mentioned above:
461 $other = $conf->obj("other");
462 my @blahs = $other->array("blah");
463 print Dumper(\@blahs);
464 $VAR1 = [ "blubber", "gobble" ];
469 This method returns the scalar value of a given key. Given the following sample
475 you could do something like that:
477 print $conf->value("name") . " is " . $conf->value("age") . " years old\n";
481 You can use this method also to set the value of "key" to something if you give over
482 a hash reference, array reference or a scalar in addition to the key. An example:
484 $conf->value("key", \%somehash);
486 $conf->value("key", \@somearray);
488 $conf->value("key", $somescalar);
490 Please note, that this method does not complain about existing values within "key"!
492 =item is_hash('key') is_array('key') is_scalar('key')
494 As seen above, you can access parts of your current config using hash, array or scalar
495 methods. But you are right if you guess, that this might become problematic, if
496 for example you call B<hash()> on a key which is in real not a hash but a scalar. Under
497 normal circumstances perl would refuse this and die.
499 To avoid such behavior you can use one of the methods is_hash() is_array() is_scalar() to
500 check if the value of "key" is really what you expect it to be.
502 An example(based on the config example from above):
504 if($conf->is_hash("individual") {
505 $individual = $conf->obj("individual");
508 die "You need to configure a "individual" block!\n";
514 This method returns just true if the given key exists in the config.
519 Returns an array of the keys under the specified "key". If you use the example
520 config above you yould do that:
522 print Dumper($conf->keys("individual");
523 $VAR1 = [ "martin", "joseph" ];
525 If no key name was supplied, then the keys of the object itself will be returned.
527 You can use this method in B<foreach> loops as seen in an example above(obj() ).
532 This method removes the given key and all associated data from the internal
533 hash structure. If 'key' contained data, then this data will be returned,
534 otherwise undef will be returned.
539 =head1 AUTOLOAD METHODS
541 Another usefull feature is implemented in this class using the B<AUTOLOAD> feature
542 of perl. If you know the keynames of a block within your config, you can access to
543 the values of each individual key using the method notation. See the following example
546 We assume the following config:
554 Now we read it in and process it:
556 my $conf = new Config::General::Extended("configfile");
557 my $person = $conf->obj("person");
558 print $person->prename . " " . $person->name . " is " . $person->age . " years old\n";
560 This notation supports only scalar values! You need to make sure, that the block
561 <person> does not contain any subblock or multiple identical options(which will become
562 an array after parsing)!
564 If you access a non-existent key this way, Config::General will croak an error.
565 You can turn this behavior off by setting B<-StrictObjects> to 0 or "no". In
566 this case undef will be returned.
568 Of course you can use this kind of methods for writing data too:
570 $person->name("Neustein");
572 This changes the value of the "name" key to "Neustein". This feature behaves exactly like
573 B<value()>, which means you can assign hash or array references as well and that existing
574 values under the given key will be overwritten.
579 Copyright (c) 2000-2008 Thomas Linden
581 This library is free software; you can redistribute it and/or
582 modify it under the same terms as Perl itself.
592 Thomas Linden <tlinden |AT| cpan.org>