--- nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2003/02/20 20:48:00 1.10 +++ nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2003/03/27 15:17:07 1.17 @@ -1,7 +1,29 @@ ## --------------------------------------------------------------------------- -## $Id: Deep.pm,v 1.10 2003/02/20 20:48:00 joko Exp $ +## $Id: Deep.pm,v 1.17 2003/03/27 15:17:07 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: Deep.pm,v $ +## Revision 1.17 2003/03/27 15:17:07 joko +## namespace fixes for Data::Mungle::* +## +## Revision 1.16 2003/03/27 15:04:52 joko +## minor update: comment +## +## Revision 1.15 2003/02/27 14:39:48 jonen +## + fixed bug at _hash2object() +## +## Revision 1.14 2003/02/22 17:13:55 jonen +## + added function 'childObject2string()' to encode 'child'-references to option related string +## + use new option at 'expand()' for replacing 'childObject2string' +## +## Revision 1.13 2003/02/21 01:48:50 joko +## renamed core function +## +## Revision 1.12 2003/02/20 22:45:19 joko +## fix regarding new deep_copy +## +## Revision 1.11 2003/02/20 21:13:54 joko +## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) +## ## Revision 1.10 2003/02/20 20:48:00 joko ## - refactored lots of code to Data::Code::Ref ## + alternative 'deep_copy' implementation @@ -40,7 +62,7 @@ ## --------------------------------------------------------------------------- -package Data::Transform::Deep; +package Data::Mungle::Transform::Deep; use strict; use warnings; @@ -58,8 +80,9 @@ use Data::Dumper; use Iterate; -use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); -use Data::Code::Ref qw( ref_slot ); +use Pitonyak::DeepCopy; +use Data::Mungle::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); +use Data::Mungle::Code::Ref qw( ref_slot ); sub numhash2list { my $vref = shift; @@ -129,6 +152,8 @@ } +# convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML +# but still using the known latin locale stuff sub expand { my $obj = shift; @@ -143,8 +168,15 @@ my $item; # if current item is a reference ... if (ref $_[0]) { - # ... expand structure recursively - $item = expand($_[0], $options); + $item = $_[0]; + # if $options->{childObj2string} is TRUE or STRING don't expand referenced object, + # instead replace it by per option choosed string (default: o___ ) + if ($item && $options->{childObj2string}) { + $item = childObj2string($item, $options->{childObj2string}); + } else { + # ... expand structure recursively + $item = expand($_[0], $options); + } # destroy item via seperate callback method (a POST) if requested #$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; @@ -173,8 +205,15 @@ # if current item is a reference ... if (ref $_[1]) { - # ... expand structure recursively - $item = expand($_[1], $options); + $item = $_[1]; + # if $options->{childObj2string} is TRUE or STRING don't expand referenced object, + # instead replace it by per option choosed string (default: o___ ) + if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) { + $item = childObj2string($item, $options->{childObj2string}); + } else { + # ... expand structure recursively + $item = expand($_[1], $options); + } # destroy item via seperate callback method (a POST) if requested #$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; @@ -198,7 +237,7 @@ # convert all values to utf8 (inside complex struct) # now done in core-item-callbacks via Greg London's "Iterate" from CPAN - # var2utf8($result) if ($options->{utf8}); + # latin_to_utf8($result) if ($options->{utf8}); # destroy persistent object from memory to be sure to get a fresh one next time #undef $obj if $options->{destroy}; @@ -258,7 +297,7 @@ sub merge_to { _hash2object(@_); # TODO: - # re-implement using CPAN's "Iterate". + # re-implement using CPAN's "Iterate" and/or a modified Hash::Merge. } @@ -275,7 +314,7 @@ numhash2list($data) if ($options->{php}); # utf8-conversion/-encoding (essential for I18N) - var_utf2iso($data) if ($options->{utf8}); + utf8_to_latin($data) if ($options->{utf8}); # get fresh object from database # todo: @@ -441,58 +480,22 @@ # ACK's go to Andrew Pitonyak # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html -sub deep_copy2 { - - # if not defined then return it - return undef if $#_ < 0 || !defined( $_[0] ); - - # if not a reference then return the parameter - return $_[0] if !ref( $_[0] ); - my $obj = shift; - if ( UNIVERSAL::isa( $obj, 'SCALAR' ) ) { - my $temp = deep_copy2($$obj); - return \$temp; - } - elsif ( UNIVERSAL::isa( $obj, 'HASH' ) ) { - my $temp_hash = {}; - foreach my $key ( keys %$obj ) { - if ( !defined( $obj->{$key} ) || !ref( $obj->{$key} ) ) { - $temp_hash->{$key} = $obj->{$key}; - } - else { - $temp_hash->{$key} = deep_copy2( $obj->{$key} ); - } - } - return $temp_hash; - } - elsif ( UNIVERSAL::isa( $obj, 'ARRAY' ) ) { - my $temp_array = []; - foreach my $array_val (@$obj) { - if ( !defined($array_val) || !ref($array_val) ) { - push ( @$temp_array, $array_val ); - } - else { - push ( @$temp_array, deep_copy2($array_val) ); - } - } - return $temp_array; - } +sub deep_copy { + Pitonyak::DeepCopy::deep_copy(@_); +} - # ?? I am uncertain about this one - elsif ( UNIVERSAL::isa( $obj, 'REF' ) ) { - my $temp = deep_copy2($$obj); - return \$temp; - } - # I guess that it is either CODE, GLOB or LVALUE - else { - return $obj; - } +sub childObj2string { + my $obj = shift; + my $option = shift; + my $classname = ref $obj; + + if($option == 1) { + my $string = "o_" . $classname . "_" .$obj->{guid}; + return $string; + } } -sub deep_copy { - deep_copy2(@_); -} 1; __END__