# Traverse a tree using localize state
$tree = [
one =>
two =>
[
three_one =>
three_two =>
[
three_three_one =>
],
three_four =>
],
four =>
[
[
five_one_one =>
],
],
];
@path = ('q');
rmap_to {
if(ref $_) {
local(@path) = (@path, 1); # ARRAY adds a new level to the path
$_[0]->recurse(); # does stuff within local(@path)'s scope
} else {
print join('.', @path), " = $_ \n"; # show the scalar's path
}
$path[-1]++; # bump last element (even when it was an aref)
} ARRAY|VALUE, $tree;
# OUTPUT
# q.1 = one
# q.2 = two
# q.3.1 = three_one
# q.3.2 = three_two
# q.3.3.1 = three_three_one
# q.3.4 = three_four
# q.4 = four
# q.5.1.1 = five_one_one
####
my @servers = (
{
type => production | development,
env => Windows | unix | Database,
hostname => the hostname
targets => [
name1 => [ #total, #free ].
name2 => [ #total, #free ],
...
],
},
{
type => production | development,
env => Windows | unix | Database,
hostname => the hostname
targets => [
name1 => [ #total, #free ].
name2 => [ #total, #free ],
...
],
},
...
);
####
use constant { TOTAL => 0, FREE => 1 };
for my @server ( @servers ) {
printf "Server: %s type:%s Env: %s\n",
$server->{ hostname },
$server->{ type },
$server->{ env };
for my $target ( @{ $server->{ targets } } ) {
printf "\tTotal: %d Free: %d\n", @{ $target }[ TOTAL, FREE ];
}
}