| 1 |
############################################## |
## --------------------------------------------------------------------------- |
| 2 |
# |
## $Id$ |
| 3 |
# $Id$ |
## --------------------------------------------------------------------------- |
| 4 |
# |
## $Log$ |
| 5 |
# $Log$ |
## Revision 1.22 2003/05/10 17:09:18 jonen |
| 6 |
# Revision 1.5 2002/12/16 19:57:54 joko |
## + added keep of empty arrays/hashes if 'expand' for php |
| 7 |
# + option 'init' |
## |
| 8 |
# |
## Revision 1.21 2003/04/09 07:21:56 joko |
| 9 |
# Revision 1.4 2002/12/05 13:56:49 joko |
## childObj2string now inside Encode.pm, renamed to 'twingle_reference' |
| 10 |
# - var_deref |
## |
| 11 |
# + expand - more sophisticated dereferencing with callbacks using Iterate |
## Revision 1.20 2003/04/04 17:31:23 joko |
| 12 |
# |
## minor update to 'childObj2string' |
| 13 |
# Revision 1.3 2002/12/03 05:34:55 joko |
## |
| 14 |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
## Revision 1.19 2003/03/28 03:11:25 jonen |
| 15 |
# |
## + bugfix |
| 16 |
# Revision 1.2 2002/12/01 04:44:07 joko |
## |
| 17 |
# + code from Data::Transform::OO |
## Revision 1.18 2003/03/28 03:07:26 jonen |
| 18 |
# |
## + minor changes |
| 19 |
# Revision 1.1 2002/11/29 04:49:20 joko |
## |
| 20 |
# + initial check-in |
## Revision 1.17 2003/03/27 15:17:07 joko |
| 21 |
# |
## namespace fixes for Data::Mungle::* |
| 22 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
## |
| 23 |
# + new |
## Revision 1.16 2003/03/27 15:04:52 joko |
| 24 |
# |
## minor update: comment |
| 25 |
############################################## |
## |
| 26 |
|
## Revision 1.15 2003/02/27 14:39:48 jonen |
| 27 |
|
## + fixed bug at _hash2object() |
| 28 |
|
## |
| 29 |
|
## Revision 1.14 2003/02/22 17:13:55 jonen |
| 30 |
|
## + added function 'childObject2string()' to encode 'child'-references to option related string |
| 31 |
|
## + use new option at 'expand()' for replacing 'childObject2string' |
| 32 |
|
## |
| 33 |
|
## Revision 1.13 2003/02/21 01:48:50 joko |
| 34 |
|
## renamed core function |
| 35 |
|
## |
| 36 |
|
## Revision 1.12 2003/02/20 22:45:19 joko |
| 37 |
|
## fix regarding new deep_copy |
| 38 |
|
## |
| 39 |
|
## Revision 1.11 2003/02/20 21:13:54 joko |
| 40 |
|
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
| 41 |
|
## |
| 42 |
|
## Revision 1.10 2003/02/20 20:48:00 joko |
| 43 |
|
## - refactored lots of code to Data::Code::Ref |
| 44 |
|
## + alternative 'deep_copy' implementation |
| 45 |
|
## |
| 46 |
|
## Revision 1.9 2003/02/18 19:35:56 joko |
| 47 |
|
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
| 48 |
|
## |
| 49 |
|
## Revision 1.8 2003/02/09 05:10:56 joko |
| 50 |
|
## + minor update |
| 51 |
|
## |
| 52 |
|
## Revision 1.7 2003/01/19 03:26:59 joko |
| 53 |
|
## + added 'sub deep_copy' - refactored from libp |
| 54 |
|
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
| 55 |
|
## |
| 56 |
|
## Revision 1.6 2002/12/23 11:27:53 jonen |
| 57 |
|
## + changed behavior WATCH! |
| 58 |
|
## |
| 59 |
|
## Revision 1.5 2002/12/16 19:57:54 joko |
| 60 |
|
## + option 'init' |
| 61 |
|
## |
| 62 |
|
## Revision 1.4 2002/12/05 13:56:49 joko |
| 63 |
|
## - var_deref |
| 64 |
|
## + expand - more sophisticated dereferencing with callbacks using Iterate |
| 65 |
|
## |
| 66 |
|
## Revision 1.3 2002/12/03 05:34:55 joko |
| 67 |
|
## + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
| 68 |
|
## |
| 69 |
|
## Revision 1.2 2002/12/01 04:44:07 joko |
| 70 |
|
## + code from Data::Transform::OO |
| 71 |
|
## |
| 72 |
|
## Revision 1.1 2002/11/29 04:49:20 joko |
| 73 |
|
## + initial check-in |
| 74 |
|
## |
| 75 |
|
## Revision 1.1 2002/10/10 03:26:00 cvsjoko |
| 76 |
|
## + new |
| 77 |
|
## --------------------------------------------------------------------------- |
| 78 |
|
|
| 79 |
|
|
| 80 |
package Data::Transform::Deep; |
package Data::Mungle::Transform::Deep; |
| 81 |
|
|
| 82 |
use strict; |
use strict; |
| 83 |
use warnings; |
use warnings; |
| 85 |
require Exporter; |
require Exporter; |
| 86 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
| 87 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
|
&var_NumericalHashToArray |
|
|
&var_mixin |
|
|
&object2hash |
|
|
&hash2object |
|
|
&refexpr2perlref |
|
| 88 |
&expand |
&expand |
| 89 |
|
&deep_copy |
| 90 |
|
&merge_to |
| 91 |
); |
); |
| 92 |
# &var_deref |
|
|
# &getStructSlotByStringyAddress |
|
| 93 |
|
|
| 94 |
use attributes; |
use attributes; |
| 95 |
use Data::Dumper; |
use Data::Dumper; |
|
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
|
|
use libp qw( deep_copy ); |
|
| 96 |
use Iterate; |
use Iterate; |
| 97 |
|
|
| 98 |
sub var_NumericalHashToArray { |
use Pitonyak::DeepCopy; |
| 99 |
|
use Data::Mungle::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar twingle_reference ); |
| 100 |
|
use Data::Mungle::Code::Ref qw( ref_slot ); |
| 101 |
|
|
| 102 |
|
sub numhash2list { |
| 103 |
my $vref = shift; |
my $vref = shift; |
| 104 |
#my $context = shift; |
#my $context = shift; |
| 105 |
if (ref $vref eq 'HASH') { |
if (ref $vref eq 'HASH') { |
| 115 |
} |
} |
| 116 |
$vref->{$_} = \@arr; |
$vref->{$_} = \@arr; |
| 117 |
} |
} |
| 118 |
var_NumericalHashToArray($vref->{$_}); |
numhash2list($vref->{$_}); |
| 119 |
} |
} |
| 120 |
# } else { |
# } else { |
| 121 |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
|
|
| 128 |
sub var_deref { |
# FIXME: could this be refactored using expand? |
| 129 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
| 130 |
|
# there is a perl make with perl |
| 131 |
|
sub _var_deref_test { |
| 132 |
my $obj = shift; |
my $obj = shift; |
| 133 |
my $options = shift; |
my $options = shift; |
| 134 |
my $result; |
my $result; |
| 167 |
} |
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
# convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML |
| 171 |
|
# but still using the known latin locale stuff |
| 172 |
sub expand { |
sub expand { |
| 173 |
|
|
| 174 |
my $obj = shift; |
my $obj = shift; |
| 175 |
my $options = shift; |
my $options = shift; |
| 176 |
my $result; |
my $result; |
| 177 |
|
|
| 178 |
if ((ref $obj) eq 'ARRAY') { |
#print "ref: ", ref $obj, "\n"; |
| 179 |
|
|
| 180 |
|
if (ref $obj eq 'ARRAY') { |
| 181 |
|
|
| 182 |
|
# if we expand for php, keep empty ARRAY |
| 183 |
|
if($#$obj == -1 && $options->{childObj2string}) { |
| 184 |
|
$result = $obj; |
| 185 |
|
} else { |
| 186 |
IterArray @$obj, sub { |
IterArray @$obj, sub { |
| 187 |
my $item; |
my $item; |
| 188 |
# if current item is a reference ... |
# if current item is a reference ... |
| 189 |
if (ref $_[0]) { |
if (ref $_[0]) { |
| 190 |
# ... expand structure recursively |
$item = $_[0]; |
| 191 |
$item = expand($_[0], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
| 192 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
| 193 |
|
if ($item && $options->{childObj2string}) { |
| 194 |
|
$item = twingle_reference($item); |
| 195 |
|
} else { |
| 196 |
|
# ... expand structure recursively |
| 197 |
|
$item = expand($_[0], $options); |
| 198 |
|
} |
| 199 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
| 200 |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
| 201 |
|
|
| 204 |
#$item = deep_copy($_[0]); |
#$item = deep_copy($_[0]); |
| 205 |
$item = $_[0]; |
$item = $_[0]; |
| 206 |
# conversions/encodings |
# conversions/encodings |
| 207 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
| 208 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
| 209 |
} |
} |
| 210 |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
| 211 |
push(@{$result}, $item); # use item in any case |
push(@{$result}, $item); # use item in any case |
| 212 |
|
|
| 213 |
} |
} |
| 214 |
|
} |
| 215 |
|
|
| 216 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
} elsif (ref $obj eq 'CODE') { |
| 217 |
} else { |
#print Dumper($obj); |
| 218 |
|
#exit; |
| 219 |
|
|
| 220 |
|
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
| 221 |
|
} elsif (ref $obj) { |
| 222 |
|
|
| 223 |
|
# if we expand for php, keep empty HASH |
| 224 |
|
my @tmp = keys %$obj; |
| 225 |
|
if($#tmp == -1 && $options->{childObj2string}) { |
| 226 |
|
$result = $obj; |
| 227 |
|
} else { |
| 228 |
IterHash %$obj, sub { |
IterHash %$obj, sub { |
| 229 |
my $item; |
my $item; |
| 230 |
|
|
| 231 |
# if current item is a reference ... |
# if current item is a reference ... |
| 232 |
if (ref $_[1]) { |
if (ref $_[1]) { |
| 233 |
# ... expand structure recursively |
$item = $_[1]; |
| 234 |
$item = expand($_[1], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
| 235 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
| 236 |
|
if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH") && !(ref $_[1] eq "Set::Object")) { |
| 237 |
|
$item = twingle_reference($item); |
| 238 |
|
} else { |
| 239 |
|
# ... expand structure recursively |
| 240 |
|
$item = expand($_[1], $options); |
| 241 |
|
} |
| 242 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
| 243 |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
| 244 |
|
|
| 247 |
#$item = deep_copy($_[1]); |
#$item = deep_copy($_[1]); |
| 248 |
$item = $_[1]; |
$item = $_[1]; |
| 249 |
# conversions/encodings |
# conversions/encodings |
| 250 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
| 251 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
| 252 |
} |
} |
| 253 |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
| 254 |
$result->{$_[0]} = $item; # use item in any case |
$result->{$_[0]} = $item; # use item in any case |
| 255 |
} |
} |
| 256 |
|
} |
| 257 |
|
|
| 258 |
|
} else { |
| 259 |
|
#die ("not a reference!"); |
| 260 |
|
$result = $obj; |
| 261 |
|
|
| 262 |
} |
} |
| 263 |
|
|
| 264 |
# convert all values to utf8 (inside complex struct) |
# convert all values to utf8 (inside complex struct) |
| 265 |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
| 266 |
# var2utf8($result) if ($options->{utf8}); |
# latin_to_utf8($result) if ($options->{utf8}); |
| 267 |
|
|
| 268 |
# destroy persistent object from memory to be sure to get a fresh one next time |
# destroy persistent object from memory to be sure to get a fresh one next time |
| 269 |
#undef $obj if $options->{destroy}; |
#undef $obj if $options->{destroy}; |
| 276 |
|
|
| 277 |
|
|
| 278 |
my @indexstack; |
my @indexstack; |
| 279 |
sub var_traverse_mixin_update { |
sub _var_traverse_mixin_update_old { |
| 280 |
my $obj = shift; |
my $obj = shift; |
| 281 |
my $data = shift; |
my $data = shift; |
| 282 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
| 320 |
#return $result; |
#return $result; |
| 321 |
} |
} |
| 322 |
|
|
| 323 |
sub object2hash { |
sub merge_to { |
| 324 |
my $object = shift; |
_hash2object(@_); |
| 325 |
my $options = shift; |
# TODO: |
| 326 |
#my $deref = var_deref($object); |
# re-implement using CPAN's "Iterate" and/or a modified Hash::Merge. |
|
my $deref = expand($object, $options); |
|
|
#var2utf8($deref) if ($options->{utf8}); |
|
|
return $deref; |
|
| 327 |
} |
} |
| 328 |
|
|
| 329 |
|
|
| 330 |
# todo: maybe do diff(s) here sometimes!? |
# todo: maybe do diff(s) here sometimes!? |
| 331 |
sub hash2object { |
sub _hash2object { |
| 332 |
my $object = shift; |
my $object = shift; |
| 333 |
my $data = shift; |
my $data = shift; |
| 334 |
my $options = shift; |
my $options = shift; |
| 337 |
|
|
| 338 |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
| 339 |
# this method corrects this |
# this method corrects this |
| 340 |
var_NumericalHashToArray($data) if ($options->{php}); |
numhash2list($data) if ($options->{php}); |
| 341 |
|
|
| 342 |
# utf8-conversion/-encoding (essential for I18N) |
# utf8-conversion/-encoding (essential for I18N) |
| 343 |
var_utf2iso($data) if ($options->{utf8}); |
utf8_to_latin($data) if ($options->{utf8}); |
| 344 |
|
|
| 345 |
# get fresh object from database |
# get fresh object from database |
| 346 |
# todo: |
# todo: |
| 349 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
| 350 |
|
|
| 351 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
| 352 |
hash2object_traverse_mixin($object, $data, 0, $options); |
_hash2object_traverse_mixin($object, $data, 0, $options); |
| 353 |
|
|
| 354 |
# done in core mixin function? |
# done in core mixin function? |
| 355 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
| 372 |
#my @indexstack; |
#my @indexstack; |
| 373 |
|
|
| 374 |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
| 375 |
sub hash2object_traverse_mixin { |
sub _hash2object_traverse_mixin { |
| 376 |
my $object = shift; |
my $object = shift; |
| 377 |
my $data = shift; |
my $data = shift; |
| 378 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
| 390 |
# what's exactly done here? hmmm.... it works ;) |
# what's exactly done here? hmmm.... it works ;) |
| 391 |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
| 392 |
# extend! check! |
# extend! check! |
| 393 |
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
|
| 394 |
|
#print attributes::reftype($object), "\n"; |
| 395 |
|
|
| 396 |
|
# V1 |
| 397 |
|
#if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
| 398 |
|
# V2 |
| 399 |
|
my $reftype = attributes::reftype($object); |
| 400 |
|
#print "reftype: '$reftype'", "\n"; |
| 401 |
|
if ($reftype eq 'HASH') { |
| 402 |
|
|
| 403 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
| 404 |
|
|
| 408 |
|
|
| 409 |
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
| 410 |
@fields = keys %{$data} if $options->{init}; |
@fields = keys %{$data} if $options->{init}; |
| 411 |
|
# @fields = keys %{$data} if $options->{mixin}; |
| 412 |
|
|
| 413 |
foreach (@fields) { |
foreach (@fields) { |
| 414 |
push @indexstack, $_; |
my $field = $_; |
| 415 |
|
push @indexstack, $field; |
| 416 |
|
|
| 417 |
# determine type of object |
# determine type of object |
| 418 |
my $ref = ref $object->{$_}; |
my $ref = ref $object->{$field}; |
| 419 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
| 420 |
if ($ref) { |
if ($ref) { |
| 421 |
hash2object_traverse_mixin($object->{$_}, $data, 1, $options); |
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
| 422 |
} else { |
} else { |
| 423 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = ref_slot($data, \@indexstack); |
| 424 |
$object->{$_} = $val; |
|
| 425 |
|
#print Dumper($options); |
| 426 |
|
my $field_target = $field; |
| 427 |
|
if (my $pattern = $options->{pattern_strip_key}) { |
| 428 |
|
print "pattern: $pattern", "\n"; |
| 429 |
|
$field_target =~ s/$pattern//; |
| 430 |
|
print "field: $field_target", "\n"; |
| 431 |
|
} |
| 432 |
|
|
| 433 |
|
$object->{$field_target} = $val if defined $val; |
| 434 |
} |
} |
| 435 |
pop @indexstack; |
pop @indexstack; |
| 436 |
} |
} |
| 461 |
push @indexstack, $i; |
push @indexstack, $i; |
| 462 |
my $ref = ref $_; |
my $ref = ref $_; |
| 463 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
| 464 |
if ($ref && $_) { |
# if ($ref && $_) { |
| 465 |
hash2object_traverse_mixin($_, $data, 1, $options); |
if ($ref) { |
| 466 |
|
_hash2object_traverse_mixin($_, $data, 1, $options); |
| 467 |
} else { |
} else { |
| 468 |
$object->[$i] = $_; |
$object->[$i] = $_ if defined $_; |
| 469 |
} |
} |
| 470 |
pop @indexstack; |
pop @indexstack; |
| 471 |
$i++; |
$i++; |
| 472 |
} |
} |
| 473 |
|
|
| 474 |
|
=pod |
| 475 |
|
# object? |
| 476 |
|
} else { |
| 477 |
|
print "reference: ", ref $object, "\n"; |
| 478 |
|
print "reftype: ", $reftype, "\n"; |
| 479 |
|
die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!"); |
| 480 |
|
=cut |
| 481 |
|
|
| 482 |
} |
} |
| 483 |
|
|
| 484 |
} |
} |
| 485 |
|
|
| 486 |
|
|
| 487 |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
| 488 |
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
# on the website: |
| 489 |
sub getStructSlotByStringyAddress { |
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
| 490 |
my $var = shift; |
# Further distribution or use is not permitted. |
| 491 |
my $indexstack_ref = shift; |
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
| 492 |
my @indexstack = @{$indexstack_ref}; |
sub deep_copy1 { |
| 493 |
my $joiner = '->'; |
my $this = shift; |
| 494 |
my @evlist; |
if (not ref $this) { |
| 495 |
foreach (@indexstack) { |
$this; |
| 496 |
my $elem; |
} elsif (ref $this eq "ARRAY") { |
| 497 |
if ($_ =~ m/\d+/) { $elem = "[$_]"; } |
[map deep_copy1($_), @$this]; |
| 498 |
if ($_ =~ m/\D+/) { $elem = "{$_}"; } |
} elsif (ref $this eq "HASH") { |
| 499 |
push @evlist, $elem; |
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
| 500 |
} |
} elsif (ref $this eq "CODE") { |
| 501 |
my $evstring = join($joiner, @evlist); |
$this; |
| 502 |
$evstring = 'return $var->' . $evstring . ';'; |
} else { die "deep_copy1 asks: what type is $this?" } |
| 503 |
#print "ev: $evstring\n"; |
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
|
return eval($evstring); |
|
| 504 |
} |
} |
| 505 |
|
|
| 506 |
|
# ACK's go to Andrew Pitonyak |
| 507 |
sub refexpr2perlref { |
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
| 508 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
| 509 |
my $obj = shift; |
sub deep_copy { |
| 510 |
my $expr = shift; |
Pitonyak::DeepCopy::deep_copy(@_); |
|
my $callbackMap = shift; |
|
|
|
|
|
my $value; |
|
|
# detect for callback (code-reference) |
|
|
if (ref($expr) eq 'CODE') { |
|
|
$value = &$expr($obj); |
|
|
|
|
|
} elsif ($expr =~ m/->/) { |
|
|
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
|
|
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr); |
|
|
#print "\n", "expr: $expr"; |
|
|
#print "\n", "objPerlRefString: $objPerlRefString"; |
|
|
$value = eval('$obj' . '->' . $objPerlRefString); |
|
|
|
|
|
# if value isn't set, try to "fallback" to callbackMap |
|
|
# callbacks are applied this way: |
|
|
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
|
|
if (!$value && $callbackMap) { |
|
|
#print Dumper($callbackMap); |
|
|
|
|
|
# prepare needle |
|
|
my @parts = @$parts; |
|
|
my $count = $#parts; |
|
|
my $needle = $parts[$count]; |
|
|
|
|
|
# prepare haystack |
|
|
my @haystack = keys %$callbackMap; |
|
|
if (grep($needle, @haystack)) { |
|
|
$value = $callbackMap->{$needle}->(); |
|
|
#print "value: ", $value, "\n"; |
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
# use expr as simple scalar key (attributename) |
|
|
$value = $obj->{$expr}; |
|
|
} |
|
|
|
|
|
return $value; |
|
|
|
|
| 511 |
} |
} |
| 512 |
|
|
| 513 |
|
|
|
sub refexpr2perlref_parts { |
|
|
my $expr = shift; |
|
|
|
|
|
# split expression by dereference operators first |
|
|
my @parts_pure = split(/->/, $expr); |
|
|
|
|
|
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
|
|
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
|
|
my @parts_capsule = @parts_pure; |
|
|
map { |
|
|
m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}"); |
|
|
} @parts_capsule; |
|
|
|
|
|
# join parts with dereference operators together again and return built string |
|
|
return (join('->', @parts_capsule), \@parts_pure); |
|
|
} |
|
|
|
|
| 514 |
1; |
1; |
| 515 |
|
__END__ |