in reply to map-like hash iterator
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->(); } } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: map-like hash iterator
by ikegami (Patriarch) on Mar 26, 2010 at 23:11 UTC |