/usr/share/perl5/Data/Walk.pm is in libdata-walk-perl 1.00-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | # $Id: Walk.pm,v 1.15 2006/05/11 14:10:54 guido Exp $
# Traverse Perl data structures.
# Copyright (C) 2005-2006 Guido Flohr <guido@imperia.net>,
# 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.
package Data::Walk;
use strict;
use 5.004;
require Exporter;
use vars qw ($VERSION @ISA @EXPORT);
$VERSION = '1.00';
@ISA = qw (Exporter);
@EXPORT = qw (walk walkdepth);
use vars qw ($container $type $seen $address $depth);
# Forward declarations.
sub walk;
sub walkdepth;
sub __walk;
sub __recurse;
sub walk {
my ($options, @args) = @_;
unless ('HASH' eq ref $options) {
$options = { wanted => $options };
}
__walk ($options, @args);
}
sub walkdepth {
my ($options, @args) = @_;
unless ('HASH' eq ref $options) {
$options = { wanted => $options };
}
$options->{bydepth} = 1;
__walk ($options, @args);
}
sub __walk {
my ($options, @args) = @_;
$options->{seen} = {};
$options->{copy} = 1 unless exists $options->{copy};
foreach my $item (@args) {
local $depth;
$depth = 0;
__recurse $options, $item;
}
return 1;
}
sub __recurse {
my ($options, $item) = @_;
++$depth;
my @children;
my $data_type;
local ($address, $seen);
undef $address;
$seen = 0;
my $ref = ref $item;
if ($ref) {
my $blessed = -1 != index $ref, '=';
# Avoid fancy overloading stuff.
bless $item if $blessed;
$address = int $item;
$seen = $options->{seen}->{$address}++;
if (UNIVERSAL::isa ($item, 'HASH')) {
$data_type = 'HASH';
} elsif (UNIVERSAL::isa ($item, 'ARRAY')) {
$data_type = 'ARRAY';
} else {
$data_type = '';
}
if ($data_type eq 'HASH' || $data_type eq 'ARRAY') {
if (('ARRAY' eq $data_type || 'HASH' eq $data_type)) {
if ('ARRAY' eq $data_type) {
@children = @{$item};
} else {
@children = %{$item};
}
if ($options->{copy}) {
if ('ARRAY' eq $data_type) {
@children = $options->{preprocess} (@{$item})
if $options->{preprocess};
} else {
@children = %{$item};
@children = $options->{preprocess} (@children)
if $options->{preprocess};
@children = $options->{preprocess_hash} (@children)
if $options->{preprocess_hash};
}
} else {
$item = $options->{preprocess} ($item)
if $options->{preprocess};
$item = $options->{preprocess_hash} ($item)
if 'HASH' eq $data_type && $options->{preprocess_hash};
@children = 'HASH' eq $data_type ? %{$item} : @{$item};
}
}
}
}
unless ($options->{bydepth}) {
$_ = $item;
$options->{wanted}->($item);
}
local ($container, $type);
$type = $data_type;
$container = $item;
if ($options->{follow} || !$seen) {
foreach my $child (@children) {
__recurse $options, $child;
}
}
if ($options->{bydepth}) {
$_ = $item;
$options->{wanted}->($item);
}
$options->{postprocess}->() if $options->{postprocess};
--$depth;
# void
}
1;
=head1 NAME
Data::Walk - Traverse Perl data structures
=head1 SYNOPSIS
use Data::Walk;
walk \&wanted, @items_to_walk;
use Data::Walk;
walkdepth \&wanted, @items_to_walk;
use Data::Walk;
walk { wanted => \&process, follow => 1 }, $self;
=head1 DESCRIPTION
The 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
L</"DIFFERENCES TO FILE::FIND">.
=head1 FUNCTIONS
The module exports two functions by default:
=over 4
=item B<walk>
walk \&wanted, @items;
walk \%options, @items;
As the name suggests, the function traverses the items in the order
they are given. For every object visited, it calls the &wanted
subroutine. See L</"THE WANTED FUNCTION"> for details.
=item B<walkdepth>
walkdepth \&wanted, @items;
walkdepth \%options, @items;
Works exactly like C<walk()> but it first descends deeper into
the structure, before visiting the nodes on the current level.
If you want to delete visited nodes, then C<walkdepth()> is probably
your friend.
=back
=head1 OPTIONS
The first argument to C<walk()> and C<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.
=over 4
=item B<wanted>
The value should be a code reference. This code reference is
described in L</"THE WANTED FUNCTION"> below.
=item B<bydepth>
Visits nodes on the current level of recursion only B<after>
descending into subnotes. The entry point C<walkdepth()> is
a shortcut for specifying C<{ bydepth =E<gt> 1 }>.
=item B<preprocess>
The value should be a code reference. This code reference is used
to preprocess the current node $Data::Walk::container. Your
preprocessing function is called before the loop that calls the
C<wanted()> function. It is called with a list of member nodes
and is expected to return such a list. The list will contain
all sub-nodes, regardless of the value of the option I<follow>!
The list is normally a shallow copy of the data contained in the original
structure. You can therefore safely delete items in it, without
affecting the original data. You can use the option I<copy>,
if you want to change that behavior.
The behavior is identical for regular arrays and hashes, so you
probably want to coerce the list passed as an argument into a hash
then. The variable $Data::Walk::type will contain the string
"HASH" if the currently inspected node is a hash.
You can use the preprocessing function to sort the items
contained or to filter out unwanted items. The order is also preserved
for hashes!
=item B<preprocess_hash>
The value should be a code reference. The code is executed
right after an eventual I<preprocess_hash> handler, but only
if the current container is a hash. It is skipped for regular
arrays.
You will usually prefer a I<preprocess_hash> handler over a
I<preprocess> handler if you only want to sort hash keys.
=item B<postprocess>
The value should be a code reference. It is invoked just before
leaving the currently visited node. It is called in void context
with no arguments. The variable $Data::Walk::container points
to the currently visited node.
=item B<follow>
Causes cyclic references to be followed. Normally, the traversal
will not descend into nodes that have already been visited. If
you set the option I<follow> to a truth value, you can change this
behavior. Unless you take additional measures, this will always
imply an infinite loop!
Please note that the &wanted function is also called for nodes
that have already been visited! The effect of I<follow> is to
suppress descending into subnodes.
=item B<copy>
Normally, the &preprocess function is called with a shallow copy
of the data. If you set the option I<copy> to a false value,
the &preprocess function is called with one single argument,
a reference to the original data structure. In that case, you
also have to return a suitable reference.
Using this option will result in a slight performance win, and
can make it sometimes easier to manipulate the original data.
What is a shallow copy? Think of a list containing references
to hashes:
my @list = ({ foo => 'bar' }, { foo => 'baz' });
my @shallow = @list;
After this, @shallow will contain a new list, but the items
stored in it are exactly identical to the ones stored in the
original. In other words, @shallow occupies new memory, whereas
both lists contain references to the same memory for the list
members.
=back
All other options are silently ignored.
=head1 THE WANTED FUNCTION
The &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 B<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:
=over 4
=item B<$_>
The currently visited node. Think "file" in terms of File::Find(3pm)!
=item B<$Data::Walk::container>
The node containing the currently visited node, either a reference to
a hash or an array. Think "directory" in terms of File::Find(3pm)!
=item B<$Data::Walk::type>
The base type of the object that $Data::Walk::container
references. This is either "ARRAY" or "HASH".
=item B<$Data::Walk::seen>
For references, this will hold the number of times the currently
visited node has been visited I<before>. The value is consequently
set to 0 not 1 on the first visit. For non-references, the value
is undefined.
=item B<$Data::Walk::address>
For references, this will hold the memory address it points to. It
can be used as a unique identifier for the current node. For non-
references, the value is undefined.
=item B<$Data::Walk::depth>
The depth of the current recursion.
=back
These variables should not be modified.
=head1 DIFFERENCES TO FILE::FIND
The 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).
=head2 Analogies
The 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.
=head2 Function Names
Instead of C<find()> and C<finddepth()>, Data::Walk(3pm) uses
C<walk()> and C<walkdepth()>, like the smart reader
has already guessed after reading the L</"SYNOPSIS">.
=head2 Variables
The variable $Data::Walk::container is vaguely equivalent to
$File::Find::dir. All other variables are specific to the
corresponding module.
=head2 Wanted Function
Like its archetype from File::Find(3pm), the wanted function of
Data::Walk(3pm) is called with $_ set to the currently inspected
item.
=head2 Options
The option I<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 I<follow_fast>,
I<follow_skip>, I<no_chdir>, I<untaint>, I<untaint_pattern>, and
I<untaint_skip>. To give truth the honor, all unrecognized options
are skipped.
You may argue, that the options I<untaint> and friends would be
useful, too, allowing you to recursively untaint data structures.
But, hey, that is what Data::Walk(3pm) is all about. It makes
it very easy for you to write that yourself.
=head1 EXAMPLES
Following are some recipies for common tasks.
=head2 Recursive Untainting
sub untaint {
s/(.*)/$1/s unless ref $_;
};
walk \&untaint, $data;
See perlsec(1), if you don't understand why the untaint() function
untaints your data here.
=head2 Recurse To Maximum Depth
If 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 };
=head1 BUGS
If 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.
=head1 COPYING
Copyright (C) 2005-2006, Guido Flohr E<lt>guido@imperia.netE<gt>, 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.
=head1 SEE ALSO
Data::Dumper(3pm), Storable(3pm), File::Find(3pm), perl(1)
=cut
#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#cperl-indent-level: 4
#cperl-continued-statement-offset: 2
#tab-width: 8
#End:
|