NAME Data::Dmap - just like map, but on deep data structures VERSION Version 0.04 SYNOPSIS This module provides the single function "dmap" which carries out a "map"-like operation on deep data structures. use Data::Dmap; my $foo = { cars => [ 'ford', 'opel', 'BMW' ], birds => [ 'cuckatoo', 'ostrich', 'frigate' ], handler => sub { print "barf\n" } }; # This removes all keys named 'cars' my($bar) = dmap { delete $_->{cars} if ref eq 'HASH'; $_ } $foo; # This replaces arrays with the number of elements they contains my($other) = dmap { $_ = scalar @$_ if ref eq 'ARRAY'; $_ } $foo; use Data::Dumper; print Dumper $other; # # Prints # { # birds => 3, # handler => sub { "DUMMY" } # } # (Data::Dumper doesn't dump subs) $other->{handler}->(); # Prints # barf EXPORT "dmap" - the dmap function that does deep in-place mapping SUBROUTINES/METHODS "dmap" This function works like "map" - it takes an expression followed by a list, evaluates the expression on each member of the list and returns the result. The only difference is that any references returned by the expression will also be traversed and passed to the expression once again, thus making it possible to make deep traversal of any data structure. Objects (references blessed to something) are just traversed as if they weren't blessed. Examples Delete all hash references use Data::Dmap; use Data::Dump 'pp'; pp dmap { return $_ unless ref eq 'HASH'; return; } 1, 'foo', [ { a => 1 }, 2]; # Prints: # (1, "foo", [2]) Delete every odd number use Data::Dmap; use Data::Dump 'pp'; pp dmap { return if $_ % 2; $_ } [ 1 .. 10 ]; # Prints: # [2, 4, 6, 8, 10] Replace all hash refs with some $object of class "thingy". use Data::Dmap; use Data::Dump 'pp'; pp dmap { return bless $_, 'thingy' if ref eq 'HASH'; $_ } [ 1, "hello", { a => 1 } ]; Because the output from the expression is being traversed, you can use "dmap" to generate data structures: use Data::Dmap; use Data::Dump 'pp'; my $height = 3; pp dmap { if(ref eq 'HASH' and $height--) { $_->{a} = {height => $height} } $_ } {}; # Prints: # { # a => { # a => { # a => { # height => 0 # }, # height => 1 # }, # height => 2 # } # } # (My own formatting above.) AUTHOR Michael Zedeler, "" BUGS If you find a bug, please consider helping to fix the bug by doing this: * Fork "Data::Dmap" from * Write a test case in the "t" directory, commit and push it. * Fix the bug or (if you don't know how to fix it), report the bug Bugs and feature requests can be reported through the web interface at . I may not be notified, so send me a mail too. SUPPORT You can find documentation for this module with the perldoc command. perldoc Data::Dmap You can also look for information at: * The github issue tracker * AnnoCPAN: Annotated CPAN documentation * CPAN Ratings * Search CPAN ACKNOWLEDGEMENTS LICENSE AND COPYRIGHT Copyright 2010 Michael Zedeler. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information.