SPUG: A hash of hashes and arrays
Ivan Heffner
iheffner at gmail.com
Mon Aug 1 11:58:32 PDT 2005
On 8/1/05, Yitzchak Scott-Thoennes <sthoenna at efn.org> wrote:
>
> If there are no objects involved, this is easy. See perldoc -f ref
>
> while ( my ($key, $val) = each %outer_hash ) {
> if ( ref $val eq "HASH" ) {
> ...
> } elsif ( ref $val eq "ARRAY" ) {
> ...
> } else {
> warn "I don't think we're in Kansas anymore, Toto";
> }
> }
>
> If there are objects involved, the problem is not generally solvable; you
> have to decide what you will treat as a hash and what as an array.
>
You can still make this work (although not quite as nicely in some cases) if
your values are objects. Objects are still references, they just have
"specialness" (blessed). By changing the above equality tests to regexes,
you can treat objects as the base data type:
my $ref = ref $val;
if (!defined $ref) {
# simple scalar value
}
elsif ($ref =~ /HASH/) {
...
}
elsif ($ref =~ /ARRAY/) {
...
}
# other conditions for different data types (SCALAR, CODE, etc.)
else {
# catch-all if you didn't specifically handle the type
}
-- Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20050801/61efaf91/attachment.html
More information about the spug-list
mailing list