Can't do it with built-ins, but it's easy to create a function that can do it.
#!perl
use strict;
use warnings;
BEGIN {
package Hash::Adjust;
use Exporter::Shiny 'ha';
sub _generate_ha {
my ( $me, $name, undef, $opt ) = ( shift, @_ );
my ( $aref, $bref ) = do {
no strict 'refs';
my $caller = $opt->{into};
( \${"$caller\::a"}, \${"$caller\::b"} );
};
return sub (&\%) {
my ( $code, $hashref ) = ( @_ );
@_ = ();
my %tmp;
while ( ( $$aref, $$bref ) = each %$hashref ) {
&$code;
$tmp{$$aref} = $$bref if defined $$aref;
}
%$hashref = %tmp;
};
}
};
use Hash::Adjust 'ha';
use Data::Dumper;
my %hash = ( FOO => 42, BAR => 666 );
ha {
$a =~ s/O/o/g;
++$b;
} %hash;
print Dumper \%hash;
The ha function is called like ha { CODE } %hash and modifies the keys and values of the hash. The code gets the key in $a and the value in $b; the same special global variables used by sort. If the code block sets $a = undef then that key-value pair will be removed from the hash.