Sebastian Stellingwerff (webmind)
and 1 contributors


Hash::Mogrify - Perl extension for modifying hashes


  use Hash::Mogrify qw(kmap vmap hmap);
   # or :all
  use Hash::Mogrify qw(kmap vmap hmap :force :nowarning :dieonerror);
   # to set global bitmaps
  use Hash::Mogrify qw(:all :const);
    # also get constants for setting local bitmaps.

  my %hash = ( foo  => 'bar',
               quuz => 'quux',
               bla  => 'bulb',);

  my %newhash     = kmap { $_ =~ s/foo/food/ } %hash;
  my $newhashref  = vmap { $_ =~ s/bulb/burp/ } %hash;
  my $samehashref = hmap { $_[0] =~ s/foo/food/; $_[1] =~ s/bulb/burp/ } \%hash;

  ## setting local bitmaps
  my %newhash     = kmap { $_ =~ s/foo/food/ } %hash, NOWARNING | FORCE;
  # to enable nowarning and force for this action.

  kmap { $_ =~ s/foo/food/ } \%hash, DIEONERR
  # to let kmap die on error.

  ktrans { foo => 'food' }, \%hash;
 # Change key foo into key food.


Hash::Mogrify contains functions for changes parts of hashes, change/mogrify it's keys or it's values.

The functions are flexible in design. The functions kmap, vmap and hmap return a hash/list in list context and a hash-reference in scalar context. The first argument to these functions is a code block to mogrify the hash, the second either a hash or a hashref.

If a hash(list) is provided as an argument a new hash is created. When a hash-reference (e.a \%hash) is provided the original hash is changed.

The function ktrans works similar to kmap, except that it takes a hashref as translation table instead of a codeblock.

By default no function overwrites existing keys and warns about this when trying. this can be changed by setting the global or local bitmap. The global bitmap can be set on load by the following keys: :nowarning # do not warn about errors :dieonerror # die incase you're trying to override an existing key :force # override existing keys (overrrides :dieonerror). The local bitmap can be set by adding to the end of the function, there are the following constants: NOWARNING FORCE DIEONERR The local bitmap will completely override the global bitmap.

More options might be provided in later versions.


None by default.


Util::List, Hash::Util, Hash::MoreUtils, Hash::Transform, Hash::Rename


Sebastian Stellingwerff, <>


Copyright (C) 2009 by Sebastian Stellingwerff

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.