|
NAMEData::Walk - Traverse Perl data structuresSYNOPSISuse Data::Walk; walk \&wanted, @items_to_walk; use Data::Walk; walkdepth \&wanted, @items_to_walk; use Data::Walk; walk { wanted => \&process, follow => 1 }, $self; DESCRIPTIONThe above synopsis bears an amazing similarity to File::Find(3pm) and this is not coincidental.Data::Walk(3pm) is for data what File::Find(3pm) is for files. You can use it for rolling your own serialization class, for displaying Perl data structures, for deep copying or comparing, for recursive deletion of data, or ... If you are impatient and already familiar with File::Find(3pm), you can skip the following documentation and proceed with "DIFFERENCES TO FILE::FIND". FUNCTIONSThe module exports two functions by default:
OPTIONSThe first argument to "walk()" and "walkdepth()" is either a code reference to your &wanted function, or a hash reference describing the operations to be performed for each visited node.Here are the possible keys for the hash.
All other options are silently ignored. THE WANTED FUNCTIONThe &wanted function does whatever verifications you want on each item in the data structure. Note that despite its name, the &wanted function is a generic callback and does not tell Data::Walk(3pm) if an item is "wanted" or not. In fact, its return value is ignored.The wanted function takes no arguments but rather does its work through a collection of variables:
These variables should not be modified. DIFFERENCES TO FILE::FINDThe API of Data::Walk(3pm) tries to mimic the API of File::Find(3pm) to a certain extent. If you are already familiar with File::Find(3pm) you will find it very easy to use Data::Walk(3pm). Even the documentation for Data::Walk(3pm) is in parts similar or identcal to that of File::Find(3pm).AnalogiesThe equivalent of directories in File::Find(3pm) are the container data types in Data::Walk(3pm). Container data types are arrays (aka lists) and associative arrays (aka hashes). Files are equivalent to scalars. Wherever File::Find(3pm) passes lists of strings to functions, Data::Walk(3pm) passes lists of variables.Function NamesInstead of "find()" and "finddepth()", Data::Walk(3pm) uses "walk()" and "walkdepth()", like the smart reader has already guessed after reading the "SYNOPSIS".VariablesThe variable $Data::Walk::container is vaguely equivalent to $File::Find::dir. All other variables are specific to the corresponding module.Wanted FunctionLike its archetype from File::Find(3pm), the wanted function of Data::Walk(3pm) is called with $_ set to the currently inspected item.OptionsThe option follow has the effect that Data::Walk(3pm) also descends into nodes it has already visited. Unless you take extra measures, this will lead to an infinite loop!A number of options are not applicable to data traversion and are ignored by Data::Walk(3pm). Examples are follow_fast, follow_skip, no_chdir, untaint, untaint_pattern, and untaint_skip. To give truth the honor, all unrecognized options are skipped. EXAMPLESFollowing are some recipies for common tasks.Recurse To Maximum DepthIf you want to stop the recursion at a certain level, do it as follows:my $max_depth = 20; sub not_too_deep { if ($Data::Walk::depth > $max_depth) { return (); } else { return @_; } } sub do_something1 { # Your code goes here. } walk { wanted => \&do_something, preprocess => \¬_too_deep }; BUGSIf you think you have spotted a bug, you can share it with others in the bug tracking system at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Walk.COPYINGCopyright (C) 2005-2016 Guido Flohr <http://www.guido-flohr.net/>, <mailto:guido.flohr@cantanea.com>, all rights reserved.This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. SEE ALSOData::Dumper(3pm), Storable(3pm), File::Find(3pm), perl(1)
Visit the GSP FreeBSD Man Page Interface. |