I would attack this problem differently, by use of higher-level functions (predicates).
Like this:
#!/usr/bin/perl -w use strict; sub map_recursive { my ($structure, $predicate) = @_; my @result; if (UNIVERSAL::isa($structure, 'ARRAY')) { for (@{$structure}) { push @result, map_recursive($_, $predicate); } } elsif (UNIVERSAL::isa($structure, 'HASH')) { for (keys %{$structure}) { push @result, map_recursive($structure->{$_}, $predicate); } } else { push @result, $predicate->($structure); } return @result; } my $scalar = 2; my @list = (1, 3, 8, 10); my %hash = (a => 1, b => 2, c => 3); my $oddp = sub { (((shift) % 2) == 1) ? 1 : 0 }; print "#1: ", join(",", map_recursive($scalar, $oddp)), "\n"; print "#1: ", join(",", map_recursive(\@list, $oddp)), "\n"; print "#1: ", join(",", map_recursive(\%hash, $oddp)), "\n";
Then you can hide the complexity of actually checking the type in the predicate.
Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com
In reply to Re: Type checker not checking out
by clemburg
in thread Type checker not checking out
by premchai21
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |