#!/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";