| 1 |
NAME |
| 2 |
Data::Rmap - recursive map, apply a block to a data structure |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
$ perl -MData::Rmap -e 'print rmap { $_ } 1, [2,3], \\4, "\n"' |
| 6 |
1234 |
| 7 |
|
| 8 |
$ perl -MData::Rmap=:all |
| 9 |
rmap_all { print (ref($_) || "?") ,"\n" } \@array, \%hash, \*glob; |
| 10 |
|
| 11 |
# OUTPUT (Note: a GLOB always has a SCALAR, hence the last two items) |
| 12 |
# ARRAY |
| 13 |
# HASH |
| 14 |
# GLOB |
| 15 |
# SCALAR |
| 16 |
# ? |
| 17 |
|
| 18 |
# Upper-case your leaves in-place |
| 19 |
$array = [ "a", "b", "c" ]; |
| 20 |
$hash = { key => "a value" }; |
| 21 |
rmap { $_ = uc $_; } $array, $hash; |
| 22 |
|
| 23 |
use Data::Dumper; $Data::Dumper::Terse=1; $Data::Dumper::Indent=0; |
| 24 |
print Dumper($array), " ", Dumper($hash), "\n"; |
| 25 |
|
| 26 |
# OUTPUT |
| 27 |
# ['A','B','C'] {'key' => 'A VALUE'} |
| 28 |
|
| 29 |
# Simple array dumper. |
| 30 |
# Uses $self->recurse method to alter traversal order |
| 31 |
($dump) = rmap_to { |
| 32 |
|
| 33 |
return "'$_'" unless ref($_); # scalars are quoted and returned |
| 34 |
|
| 35 |
my $self = shift; |
| 36 |
# use $self->recurse to grab results and wrap them |
| 37 |
return '[ ' . join(', ', $self->recurse() ) . ' ]'; |
| 38 |
|
| 39 |
} ARRAY|VALUE, [ 1, [ 2, [ [ 3 ], 4 ] ], 5 ]; |
| 40 |
|
| 41 |
print "$dump\n"; |
| 42 |
# OUTPUT |
| 43 |
# [ '1', [ '2', [ [ '3' ], '4' ] ], '5' ] |
| 44 |
|
| 45 |
DESCRIPTION |
| 46 |
rmap BLOCK LIST |
| 47 |
|
| 48 |
Recursively evaluate a BLOCK over a list of data structures (locally |
| 49 |
setting $_ to each element) and return the list composed of the results |
| 50 |
of such evaluations. $_ can be used to modify the elements. |
| 51 |
|
| 52 |
Data::Rmap currently traverses HASH, ARRAY, SCALAR and GLOB reference |
| 53 |
types and ignores others. Depending on which rmap_* wrapper is used, the |
| 54 |
BLOCK is called for only scalar values, arrays, hashes, references, all |
| 55 |
elements or a customizable combination. |
| 56 |
|
| 57 |
The list of data structures is traversed pre-order in a depth-first |
| 58 |
fashion. That is, the BLOCK is called for the container reference before |
| 59 |
is it called for it's elements (although see "recurse" below for |
| 60 |
post-order). The values of a hash are traversed in the usual "values" |
| 61 |
order which may affect some applications. |
| 62 |
|
| 63 |
If the "cut" subroutine is called in the BLOCK then the traversal stops |
| 64 |
for that branch, say if you "cut" an array then the code is never called |
| 65 |
for it's elements (or their sub-elements). To simultaneously return |
| 66 |
values and cut, simply pass the return list to cut: |
| 67 |
"cut('add','to','returned');" |
| 68 |
|
| 69 |
The first parameter to the BLOCK is an object which maintains the state |
| 70 |
of the traversal. Methods available on this object are described in |
| 71 |
"State Object" below. |
| 72 |
|
| 73 |
EXPORTS |
| 74 |
By default: |
| 75 |
|
| 76 |
rmap, rmap_all, cut |
| 77 |
|
| 78 |
Optionally: |
| 79 |
|
| 80 |
rmap_scalar rmap_hash rmap_array rmap_ref rmap_to |
| 81 |
:types => [ qw(NONE VALUE HASH ARRAY SCALAR REF OBJECT ALL) ], |
| 82 |
:all => ... # everything |
| 83 |
|
| 84 |
Functions |
| 85 |
The various names are just wrappers which select when to call the code |
| 86 |
BLOCK. rmap_all always calls it, the others are more selective while |
| 87 |
rmap_to takes an extra parameter permitting you to provide selection |
| 88 |
criteria. Furthermore, you can always just rmap_all and skip nodes which |
| 89 |
are not of interest. |
| 90 |
|
| 91 |
rmap_to { ... } $want, @data_structures; |
| 92 |
Most general first. |
| 93 |
|
| 94 |
Recurse the @data_structures and apply the BLOCK to elements |
| 95 |
selected by $want. The $want parameter is the bitwise "or" of |
| 96 |
whatever types you choose (imported with :types): |
| 97 |
|
| 98 |
VALUE - non-reference scalar, eg. 1 |
| 99 |
HASH - hash reference |
| 100 |
ARRAY - array reference |
| 101 |
SCALAR - scalar refernce, eg. \1 |
| 102 |
REF - higher-level reference, eg. \\1, \\{} |
| 103 |
B<NOT> any reference type, see <Scalar::Util>'s reftype: |
| 104 |
perl -MScalar::Util=reftype -le 'print map reftype($_), \1, \\1' |
| 105 |
GLOB - glob reference, eg. \*x |
| 106 |
(scalar, hash and array recursed) |
| 107 |
ALL - all of the above |
| 108 |
NONE - none of the above |
| 109 |
|
| 110 |
So to call the block for arrays and scalar values do: |
| 111 |
|
| 112 |
use Data::Rmap ':all'; # or qw(:types rmap_to) |
| 113 |
rmap { ... } ARRAY|VALUE, @data_structures; |
| 114 |
|
| 115 |
(ALL & !GLOB) might also be handy. |
| 116 |
|
| 117 |
The remainder of the wrappers are given in terms of the $want for |
| 118 |
rmap_to. |
| 119 |
|
| 120 |
rmap { ... } @list; |
| 121 |
Recurse and call the BLOCK on non-reference scalar values. $want = |
| 122 |
VALUE |
| 123 |
|
| 124 |
rmap_all BLOCK LIST |
| 125 |
Recurse and call the BLOCK on everything. $want = ALL |
| 126 |
|
| 127 |
rmap_scalar { ... } @list |
| 128 |
Recurse and call the BLOCK on non-collection scalars. $want = |
| 129 |
VALUE|SCALAR|REF |
| 130 |
|
| 131 |
rmap_hash |
| 132 |
Recurse and call the BLOCK on hash refs. $want = HASH |
| 133 |
|
| 134 |
rmap_array |
| 135 |
Recurse and call the BLOCK on array refs. $want = ARRAY |
| 136 |
|
| 137 |
rmap_ref |
| 138 |
Recurse and call the BLOCK on all references (not GLOBS). $want = |
| 139 |
HASH|ARRAY|SCALAR|REF |
| 140 |
|
| 141 |
Note: rmap_ref isn't the same as rmap_to {} REF |
| 142 |
|
| 143 |
cut(@list) |
| 144 |
Don't traverse sub-elements and return the @list immediately. For |
| 145 |
example, if $_ is an ARRAY reference, then the array's elements are |
| 146 |
not traversed. |
| 147 |
|
| 148 |
If there's two paths to an element, both will need to be cut. |
| 149 |
|
| 150 |
State Object |
| 151 |
The first parameter to the BLOCK is an object which maintains most of |
| 152 |
the traversal state (except current node, which is $_). *You will ignore |
| 153 |
it most of the time*. The "recurse" method may be useful. Other methods |
| 154 |
should only be used in throw away tools, see TODO |
| 155 |
|
| 156 |
Methods: |
| 157 |
|
| 158 |
recurse |
| 159 |
Process child nodes of $_ now and return the result. |
| 160 |
|
| 161 |
This makes it easier to perform post-order and in-order processing |
| 162 |
of a structure. Note that since the same "seen list" is used, the |
| 163 |
child nodes aren't reprocessed. |
| 164 |
|
| 165 |
code |
| 166 |
The code reference of the BLOCK itself. Possible useful in some |
| 167 |
situations. |
| 168 |
|
| 169 |
seen |
| 170 |
(Warning: I'm undecided whether this method should be public) |
| 171 |
|
| 172 |
Reference to the HASH used to track where we have visited. You may |
| 173 |
want to modify it in some situations (though I haven't yet). Beware |
| 174 |
circular references. The (current) convention used for the key is in |
| 175 |
the source. |
| 176 |
|
| 177 |
want |
| 178 |
(Warning: I'm undecided whether this method should be public) |
| 179 |
|
| 180 |
The $want state described in rmap_to. |
| 181 |
|
| 182 |
EXAMPLES |
| 183 |
# command-line play |
| 184 |
$ perl -MData::Rmap -le 'print join ":", rmap { $_ } 1,2,[3..5],\\6' |
| 185 |
1:2:3:4:5:6 |
| 186 |
|
| 187 |
# Linearly number questions on a set of pages |
| 188 |
my $qnum = 1; |
| 189 |
rmap_hash { |
| 190 |
$_->{qnum} = $qnum++ if($_->{qn}); |
| 191 |
} @pages; |
| 192 |
|
| 193 |
# Grep recursively, finding ALL objects |
| 194 |
use Scalar::Util qw(blessed); |
| 195 |
my @objects = rmap_ref { |
| 196 |
blessed($_) ? $_ : (); |
| 197 |
} $data_structure; |
| 198 |
|
| 199 |
# Grep recursively, finding public objects (note the cut) |
| 200 |
use Scalar::Util qw(blessed); |
| 201 |
my @objects = rmap_ref { |
| 202 |
blessed($_) ? cut($_) : (); |
| 203 |
} $data_structure; |
| 204 |
|
| 205 |
# Return a modified structure |
| 206 |
# (result flattening means we must cheat by cloning then modifying) |
| 207 |
use Storable qw(dclone); |
| 208 |
use Lingua::EN::Numbers::Easy; |
| 209 |
|
| 210 |
$words = [ 1, \2, { key => 3 } ]; |
| 211 |
$nums = dclone $words; |
| 212 |
rmap { $_ = $N{$_} || $_ } $nums; |
| 213 |
|
| 214 |
# Make an assertion about a structure |
| 215 |
use Data::Dump; |
| 216 |
rmap_ref { |
| 217 |
blessed($_) && $_->isa('Question') && defined($_->name) |
| 218 |
or die "Question doesn't have a name:", dump($_); |
| 219 |
} @pages; |
| 220 |
|
| 221 |
# Traverse a tree using localize state |
| 222 |
$tree = [ |
| 223 |
one => |
| 224 |
two => |
| 225 |
[ |
| 226 |
three_one => |
| 227 |
three_two => |
| 228 |
[ |
| 229 |
three_three_one => |
| 230 |
], |
| 231 |
three_four => |
| 232 |
], |
| 233 |
four => |
| 234 |
[ |
| 235 |
[ |
| 236 |
five_one_one => |
| 237 |
], |
| 238 |
], |
| 239 |
]; |
| 240 |
|
| 241 |
@path = ('q'); |
| 242 |
rmap_to { |
| 243 |
if(ref $_) { |
| 244 |
local(@path) = (@path, 1); # ARRAY adds a new level to the path |
| 245 |
$_[0]->recurse(); # does stuff within local(@path)'s scope |
| 246 |
} else { |
| 247 |
print join('.', @path), " = $_ \n"; # show the scalar's path |
| 248 |
} |
| 249 |
$path[-1]++; # bump last element (even when it was an aref) |
| 250 |
} ARRAY|VALUE, $tree; |
| 251 |
|
| 252 |
# OUTPUT |
| 253 |
# q.1 = one |
| 254 |
# q.2 = two |
| 255 |
# q.3.1 = three_one |
| 256 |
# q.3.2 = three_two |
| 257 |
# q.3.3.1 = three_three_one |
| 258 |
# q.3.4 = three_four |
| 259 |
# q.4 = four |
| 260 |
# q.5.1.1 = five_one_one |
| 261 |
|
| 262 |
Troubleshooting |
| 263 |
Beware comma after block: |
| 264 |
|
| 265 |
rmap { print }, 1..3; |
| 266 |
^-------- bad news, you get and empty list: |
| 267 |
rmap(sub { print $_; }), 1..3; |
| 268 |
|
| 269 |
If you don't import a function, perl's confusion may produce: |
| 270 |
|
| 271 |
$ perl -MData::Rmap -le 'rmap_scalar { print } 1' |
| 272 |
Can't call method "rmap_scalar" without a package or object reference... |
| 273 |
|
| 274 |
$ perl -MData::Rmap -le 'rmap_scalar { $_++ } 1' |
| 275 |
Can't call method "rmap_scalar" without a package or object reference... |
| 276 |
|
| 277 |
If there's two paths to an element, both will need to be cut. |
| 278 |
|
| 279 |
If there's two paths to an element, one will be taken randomly when |
| 280 |
there is an intervening hash. |
| 281 |
|
| 282 |
TODO |
| 283 |
put for @_ iin wrapper to allow parameters in a different wrapper, solve |
| 284 |
localizing problem. |
| 285 |
|
| 286 |
Note that the package/class name of the "State Object" is subject to |
| 287 |
change. |
| 288 |
|
| 289 |
The want and seen accessors may change or become useful dynamic |
| 290 |
mutators. |
| 291 |
|
| 292 |
Store custom localized data about the traversal. Seems too difficult and |
| 293 |
ugly when compare to doing it at the call site. Should support multiple |
| 294 |
reentrancy so avoid the symbol table. |
| 295 |
|
| 296 |
"rmap_args { } $data_structure, @args" form to pass parameters. Could |
| 297 |
potentially help localizing needs. (Maybe only recurse last item) |
| 298 |
|
| 299 |
Benchmark. Use array based object and/or direct access internally. |
| 300 |
|
| 301 |
rmap_objects shortcut for Scalar::Utils::blessed (Let me know of other |
| 302 |
useful rmap_??? wrappers) |
| 303 |
|
| 304 |
Think about permitting different callback for different types. The |
| 305 |
prototype syntax is a bit too flaky.... |
| 306 |
|
| 307 |
Ensure that no memory leaks are possible, leaking the closure. |
| 308 |
|
| 309 |
Read http://www.cs.vu.nl/boilerplate/ |
| 310 |
|
| 311 |
SEE ALSO |
| 312 |
map, grep, Storable's dclone, Scalar::Util's reftype and blessed |
| 313 |
|
| 314 |
Faint traces of treemap: |
| 315 |
|
| 316 |
http://www.perlmonks.org/index.pl?node_id=60829 |
| 317 |
|
| 318 |
AUTHOR |
| 319 |
Brad Bowman <[email protected]> Copyright (C) 2004 All rights reserved. |
| 320 |
|