| 1 |
NAME |
| 2 |
Data::Transformer - Traverse a data structure, altering it in place |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Data::Transformer; |
| 6 |
|
| 7 |
# A: SIMPLE USAGE: |
| 8 |
# trim extra whitespace from normal strings inside %data. |
| 9 |
my $trim = sub { local($_)=shift; $$_ =~ s/^\s*//; $$_ =~ s/\s*$//; }; |
| 10 |
my $t = Data::Transformer->new(normal=>$trim); |
| 11 |
$t->traverse(\%data); |
| 12 |
|
| 13 |
# B: MORE COMPLEX USAGE: |
| 14 |
# (a) uppercase all keys in all hashes contained in $data |
| 15 |
# and (b) convert any arrays to hashes: |
| 16 |
my $uc_hash = sub { |
| 17 |
my $h = shift; |
| 18 |
my @keys = keys %h; |
| 19 |
foreach (@keys) { |
| 20 |
my $uc = uc($_); |
| 21 |
if ($uc ne $_ && !exists($h->{$uc})) { |
| 22 |
$h->{$uc} = $h->{$_}; |
| 23 |
delete $h->{$_}; |
| 24 |
} elsif ($uc ne $_) { |
| 25 |
die "Bad key $_: '$uc' exists already"; |
| 26 |
} |
| 27 |
} |
| 28 |
}; |
| 29 |
my $ar_conv = sub { |
| 30 |
my %h = @{$_[0]}; |
| 31 |
return sub { \%h }; |
| 32 |
}; |
| 33 |
my $t = Data::Transformer->new( |
| 34 |
hash => $uc_hash, |
| 35 |
array => $ar_conv, |
| 36 |
node_limit => 500_000 ); |
| 37 |
eval { $t->traverse($data) }; |
| 38 |
warn "Could not complete transformation: $@" if $@; |
| 39 |
|
| 40 |
# C: NON-DESTRUCTIVE TRAVERSAL |
| 41 |
# You don't actually have to change anything... |
| 42 |
my $size = 0; |
| 43 |
my $t = Data::Transformer->new( |
| 44 |
normal => sub { $size+=length(${ $_[0] }) }, |
| 45 |
hash => sub { $size+=length($_) for keys %{ $_[0] } }, |
| 46 |
); |
| 47 |
my $nodes = $t->tranverse(\%data); |
| 48 |
print "Number of nodes: $nodes\n"; |
| 49 |
print "Size of keys + values: $size\n"; |
| 50 |
|
| 51 |
# D: OBJECTS INSIDE A DATA STRUCTURE |
| 52 |
# Affect objects by using the class name as a key: |
| 53 |
my $t = Data::Transformer->new( |
| 54 |
'My::Class' => sub { shift->set_foo('bar') } |
| 55 |
); |
| 56 |
|
| 57 |
DESCRIPTION |
| 58 |
Data type callbacks |
| 59 |
The basic idea is that you provide a callback subroutine for each type |
| 60 |
of data that you wish to affect or collect information from. |
| 61 |
|
| 62 |
The constructor, "new()", expects a hash with at least one of the |
| 63 |
following keys: |
| 64 |
|
| 65 |
* normal : used for normal, non-reference data |
| 66 |
* array : used for array references |
| 67 |
* hash : used for hash references |
| 68 |
* code : used for anonymous subroutines (coderefs) |
| 69 |
* scalar : used for scalar references |
| 70 |
* glob : used for globs (such as filehandle holders) |
| 71 |
|
| 72 |
The value in each case is a coderef representing the callback for the |
| 73 |
data type in question. |
| 74 |
|
| 75 |
The array and hash types are special in that they are traversed into. |
| 76 |
|
| 77 |
It is possible to affect objects inside the data structure by specifying |
| 78 |
a callback keyed to the name of the class they belong to. They are not |
| 79 |
automatically recursed into, however, even if they happen to be blessed |
| 80 |
hash or array references. |
| 81 |
|
| 82 |
Similarly, a scalar reference is not automatically traversed into, even |
| 83 |
if it may contain a reference to an arrayref or a hashref. To make the |
| 84 |
module traverse into scalar references, you need to return a coderef |
| 85 |
encapsulating a different data type in the scalar handler, thus changing |
| 86 |
them (and prompting a reiteration over that data point). This applies to |
| 87 |
objects as well. |
| 88 |
|
| 89 |
Additional option for the constructor |
| 90 |
node_limit: |
| 91 |
If an integer value for this is specified, it overrides the default |
| 92 |
node processing limit of 2**16. This cannot be set higher than |
| 93 |
2**20-1. |
| 94 |
|
| 95 |
traverse() |
| 96 |
The traverse() method returns the number of nodes processed. This may be |
| 97 |
different from both the number of nodes in the actual data structure and |
| 98 |
the number of nodes after the transformation, for the following reasons: |
| 99 |
|
| 100 |
* Reiteration into a particular node may have occurred, which |
| 101 |
increments the node count. |
| 102 |
|
| 103 |
* Blessed references (objects) will not normally be iterated into, |
| 104 |
but are merely treated as leaves in the data structure. |
| 105 |
|
| 106 |
* The processing code passed to the constructor may well affect the |
| 107 |
number of nodes. |
| 108 |
|
| 109 |
Note on data type changes |
| 110 |
If you want to change a data type (for instance replace an array by a |
| 111 |
hash as in example B, above) you have to return a coderef from the |
| 112 |
callback for the original data type. This coderef encapsulates the |
| 113 |
replacement data for the node in question. |
| 114 |
|
| 115 |
After the node has thus been replaced, it is re-evaluated to apply any |
| 116 |
transformations you may have defined for the new data type. |
| 117 |
|
| 118 |
Be careful of potential infinite loops when doing this with more than |
| 119 |
one data type at a time or when replacing coderefs with other coderefs. |
| 120 |
Also, because of reiteration, complex changes of large data structures |
| 121 |
may require setting the node processing limit higher than the default. |
| 122 |
|
| 123 |
Note on circular references |
| 124 |
Data structures containing circular references should not cause |
| 125 |
problems. Data::Transformer will skip any node containing a reference |
| 126 |
which has already been processed. |
| 127 |
|
| 128 |
CAVEATS |
| 129 |
It is not feasible to use this module for very large data structures. |
| 130 |
Accordingly, there is a hard node processing boundary of 2**20-1 (about |
| 131 |
1 million); attempting to set the limit higher results in an immediate, |
| 132 |
fatal error. For the vast majority of cases, however, the default limit |
| 133 |
of 2**16 (about 65 thousand) should be more than enough. |
| 134 |
|
| 135 |
SEE ALSO |
| 136 |
I am aware of two modules doing similar things. Check them out if this |
| 137 |
one does not fit your needs: |
| 138 |
|
| 139 |
* Data::Rmap by Brad Bowman |
| 140 |
|
| 141 |
* Data::Walk by Guido Flohr |
| 142 |
|
| 143 |
AUTHOR |
| 144 |
Baldur Kristinsson <[email protected]>, 2006 |
| 145 |
|
| 146 |
Copyright (c) 2006 Baldur Kristinsson. All rights reserved. |
| 147 |
This program is free software; you can redistribute it and/or |
| 148 |
modify it under the same terms as Perl itself. |
| 149 |
|