Subject: | Object - powered Interpolated Variables |
This is just thought; a "wouldn't it be cool if..."
<FOO>
<ONE>
BIF = Green
</ONE>
<TWO>
BIF = Blue
</TWO>
</FOO>
<BAR>
BORK = $FOO.TWO.BIF # cool part #1: obj.obj.obj...
</BAR>
And...
# cool part #2: extend obj.obj notation to ->value method:
my $bif = $self->value ('FOO.ONE.BIF');
my $bof = $self->value ('BAR.BORK');
print "$bif\n$bof\n";
...
Blue
Green
The alternative, $self->obj ('FOO')->obj ('one')->value ('BIF') is nice,
but wouldn't it be cool if...
I wrote a recursive find method to include in my package which inherits
C::G. It's simplistic and assumes that you've already parsed the A.B.C
to qw (A B C) (split, that is). But it works. I'll attach it for the
interminably curious.
Subject: | find.pm |
package MyPackage;
use strict;
use warnings;
use base qw (Config::General::Extended);
#
# Given a list of nodes, ->find will search for a tree that branches in
# just this way, returning the Config::General::Extended object it finds
# at the bottom if it exists. You can also search partway down the tree
# and ->find should return where you left off.
# For example, given the values ->find (qw (A B C)) and the following
# tree (</end> tags ommitted for brevity):
#<A>
# <FOO>
# ...
# <B>
# <BAZ>
# ...
# <C>
# BAR = shoo
#
# ->find will find the object at <C> with the value BAR = shoo and
# return it.
#
sub find {
my $self = shift;
my $l = shift;
return undef unless $self->exists ($l);
if (@_) {
return $self->obj ($l)->find (@_);
} else {
return $self->obj ($l);
}
}
sub obj {
my ($self, @args) = @_;
bless $self->SUPER::obj (@args), ref $self; # cast back to our class
}
1;