.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Data::Transformer 3pm" .TH Data::Transformer 3pm "2021-01-09" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Data::Transformer \- Traverse a data structure, altering it in place .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Data::Transformer; \& \& # A: SIMPLE USAGE: \& # trim extra whitespace from normal strings inside %data. \& my $trim = sub { local($_)=shift; $$_ =~ s/^\es*//; $$_ =~ s/\es*$//; }; \& my $t = Data::Transformer\->new(normal=>$trim); \& $t\->traverse(\e%data); \& \& # B: MORE COMPLEX USAGE: \& # (a) uppercase all keys in all hashes contained in $data \& # and (b) convert any arrays to hashes: \& my $uc_hash = sub { \& my $h = shift; \& my @keys = keys %h; \& foreach (@keys) { \& my $uc = uc($_); \& if ($uc ne $_ && !exists($h\->{$uc})) { \& $h\->{$uc} = $h\->{$_}; \& delete $h\->{$_}; \& } elsif ($uc ne $_) { \& die "Bad key $_: \*(Aq$uc\*(Aq exists already"; \& } \& } \& }; \& my $ar_conv = sub { \& my %h = @{$_[0]}; \& return sub { \e%h }; \& }; \& my $t = Data::Transformer\->new( \& hash => $uc_hash, \& array => $ar_conv, \& node_limit => 500_000 ); \& eval { $t\->traverse($data) }; \& warn "Could not complete transformation: $@" if $@; \& \& # C: NON\-DESTRUCTIVE TRAVERSAL \& # You don\*(Aqt actually have to change anything... \& my $size = 0; \& my $t = Data::Transformer\->new( \& normal => sub { $size+=length(${ $_[0] }) }, \& hash => sub { $size+=length($_) for keys %{ $_[0] } }, \& ); \& my $nodes = $t\->tranverse(\e%data); \& print "Number of nodes: $nodes\en"; \& print "Size of keys + values: $size\en"; \& \& # D: OBJECTS INSIDE A DATA STRUCTURE \& # Affect objects by using the class name as a key: \& my $t = Data::Transformer\->new( \& \*(AqMy::Class\*(Aq => sub { shift\->set_foo(\*(Aqbar\*(Aq) } \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" .SS "Data type callbacks" .IX Subsection "Data type callbacks" The basic idea is that you provide a callback subroutine for each type of data that you wish to affect or collect information from. .PP The constructor, \f(CW\*(C`new()\*(C'\fR, expects a hash with at least one of the following keys: .PP .Vb 6 \& * normal : used for normal, non\-reference data \& * array : used for array references \& * hash : used for hash references \& * code : used for anonymous subroutines (coderefs) \& * scalar : used for scalar references \& * glob : used for globs (such as filehandle holders) .Ve .PP The value in each case is a coderef representing the callback for the data type in question. .PP The array and hash types are special in that they are traversed into. .PP It is possible to affect objects inside the data structure by specifying a callback keyed to the name of the class they belong to. They are not automatically recursed into, however, even if they happen to be blessed hash or array references. .PP Similarly, a scalar reference is not automatically traversed into, even if it may contain a reference to an arrayref or a hashref. To make the module traverse into scalar references, you need to return a coderef encapsulating a different data type in the scalar handler, thus changing them (and prompting a reiteration over that data point). This applies to objects as well. .SS "Additional option for the constructor" .IX Subsection "Additional option for the constructor" .IP "node_limit:" 4 .IX Item "node_limit:" If an integer value for this is specified, it overrides the default node processing limit of 2**16. This cannot be set higher than 2**20\-1. .SS "\fBtraverse()\fP" .IX Subsection "traverse()" The \fBtraverse()\fR method returns the number of nodes processed. This may be different from both the number of nodes in the actual data structure and the number of nodes after the transformation, for the following reasons: .PP .Vb 2 \& * Reiteration into a particular node may have occurred, which \& increments the node count. \& \& * Blessed references (objects) will not normally be iterated into, \& but are merely treated as leaves in the data structure. \& \& * The processing code passed to the constructor may well affect the \& number of nodes. .Ve .SS "Note on data type changes" .IX Subsection "Note on data type changes" If you want to change a data type (for instance replace an array by a hash as in example B, above) you have to return a coderef from the callback for the original data type. This coderef encapsulates the replacement data for the node in question. .PP After the node has thus been replaced, it is re-evaluated to apply any transformations you may have defined for the new data type. .PP Be careful of potential infinite loops when doing this with more than one data type at a time or when replacing coderefs with other coderefs. Also, because of reiteration, complex changes of large data structures may require setting the node processing limit higher than the default. .SS "Note on circular references" .IX Subsection "Note on circular references" Data structures containing circular references should not cause problems. Data::Transformer will skip any node containing a reference which has already been processed. .SH "CAVEATS" .IX Header "CAVEATS" It is not feasible to use this module for very large data structures. Accordingly, there is a hard node processing boundary of 2**20\-1 (about 1 million); attempting to set the limit higher results in an immediate, fatal error. For the vast majority of cases, however, the default limit of 2**16 (about 65 thousand) should be more than enough. .SH "SEE ALSO" .IX Header "SEE ALSO" I am aware of two modules doing similar things. Check them out if this one does not fit your needs: .IP "\(bu" 4 Data::Rmap by Brad Bowman .IP "\(bu" 4 Data::Walk by Guido Flohr .SH "AUTHOR" .IX Header "AUTHOR" Baldur Kristinsson , 2006 .PP .Vb 3 \& Copyright (c) 2006 Baldur Kristinsson. All rights reserved. \& This program is free software; you can redistribute it and/or \& modify it under the same terms as Perl itself. .Ve