in reply to Inheritance in Hash
How do you like the folowing?
use strict; use warnings; sub get { my $ref = shift(@_); my $key = shift(@_); my $val; for (;;) { last unless UNIVERSAL::isa($ref, 'HASH'); $val = $ref->{$key} if exists $ref->{$key}; last unless @_; my $branch = shift(@_); $ref = $ref->{$branch}; } return $val; } my $data = { Colour => 'blue', Entries => { Flowers => { Dahlia => { Smell => 'nice' }, Rose => { Colour => 'red' }, }, }, }; print get($data, 'Colour', qw( Entries Flowers Rose )), $/, # red get($data, 'Colour', qw( Entries Flowers Dahlia )), $/; # blue
If $data was blessed to the package which contained get, you could improve the syntax a bit:
print $data->get('Colour', qw( Entries Flowers Rose )), $/, # red $data->get('Colour', qw( Entries Flowers Dahlia )), $/; # blue
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Inheritance in Hash
by ikegami (Patriarch) on Apr 21, 2005 at 03:54 UTC | |
by ikegami (Patriarch) on Apr 21, 2005 at 18:45 UTC |