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
In reply to Re: Inheritance in Hash
by ikegami
in thread Inheritance in Hash
by Eyck
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |