Mike Friedman


Data::Traverse - Callback-based depth-first traversal of Perl data structures




  use Data::Traverse qw(traverse);
  my $struct = [ 1, 2, { foo => 42 }, [ 3, 4, [ 5, 6, { bar => 43 } ] ], 7, 8 ];
  traverse { print "$a\n" if /ARRAY/; print "$a => $b\n" if /HASH/ } $struct;


Data::Traverse exports a single function, traverse, which takes a BLOCK and a reference to a data structure containing arrays or hashes. Objects are treated as normal references (e.g. a blessed hash will be traversed the same way as an unblessed one.) Only arrays and hashes are traversed; if an unsupported reference type is found (such as a glob or regex), Data::Traverse will croak with an error. Alternatively, you can make Data::Traverse ignore unsupported reference types by doing:

  use Data::Traverse qw(traverse);
  Data::Traverse->ignore_unsupported_refs( 1 );

Data::Traverse performs a depth-first traversal of the structure and calls the code in the BLOCK for each scalar it finds. $_ is set to the type of the container ('ARRAY' or 'HASH'). For arrays, the magic variable $a contains the data. For hashes, the magic variables $a and $b contain the key and the value, respectively.



None by default.


Mike Friedman, <friedo at friedo dot com>


Copyright (C) 2004 by Mike Friedman

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