in reply to Inheritance in Hash
This allows for less typing, but more importantly, I think, it just looks a lot cleaner. You could even expand the getprop function to handle accessing arrays stashed in your properties data structure with something like Some.Array[1].Property. Update:: This idea is actually stolen from JavaScript. It allows you to "walk" a data structure in a similar fashion. For instance, to access a hidden field named "somedata" in the second form of a web page, you'd write something like var value = document.forms[1].somedata.use strict; use warnings; # Recursive function to traverse a HoH according to a # "dotted" properties path: sub getprop{ my ($properties, $path) = @_; return $properties unless $path; my ($key, @remaining_path) = split /\./, $path; return getprop($properties->{$key}, join('.' => @remaining_path)); } # Define our properties: my $mylovelydata= { 'Colour'=>'blue', 'Entries'=> { 'Flowers'=> { 'Dahlia'=>{'Smell'=>'nice',}, 'Rose'=>{'Colour'=>'red'}, } }, }; # Test the function for my $flower qw/Dahlia Rose/{ my $colour = getprop($mylovelydata, "Entries.Flowers.$flower.Colou +r"); $colour = getprop($mylovelydata, "Entries.Colour") unless defined( +$colour); $colour = getprop($mylovelydata, "Colour") unless defined($colour) +; print "A ${flower}'s color is: $colour\n"; } __END__ A Dahlia's color is: blue A Rose's color is: red
|
|---|