One further note: this uses the package variables $a and $b to communicate the current key and value to the callback. Therefore, this code must be included (e.g. via "require") in each and every namespace where you want to use it.
As an enhancement, we'd like to avoid the overhead of constructing the result list if the caller won't be using it. --
sub hasheesh(&\%)
{
my( $c, $h ) = @_;
local( $a, $b );
if ( wantarray ) # list context
{
@_ = ();
while ( ( $a, $b ) = each %$h )
{
push @_, $c->();
}
return @_;
}
elsif ( defined wantarray ) # scalar context
{
my $n;
while ( ( $a, $b ) = each %$h )
{
my @a = $c->();
$n += @a;
}
return $n;
}
else # void context
{
while ( ( $a, $b ) = each %$h )
{
$c->();
}
}
}