--- nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2003/04/09 07:21:56 1.21 +++ nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2004/06/07 16:44:54 1.24 @@ -1,7 +1,16 @@ ## --------------------------------------------------------------------------- -## $Id: Deep.pm,v 1.21 2003/04/09 07:21:56 joko Exp $ +## $Id: Deep.pm,v 1.24 2004/06/07 16:44:54 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: Deep.pm,v $ +## Revision 1.24 2004/06/07 16:44:54 joko +## sub expand: Now also converts hash-keys to/from utf-8 +## +## Revision 1.23 2003/05/13 07:39:22 joko +## new option 'define' for "sub expand": set value to empty string if desired +## +## Revision 1.22 2003/05/10 17:09:18 jonen +## + added keep of empty arrays/hashes if 'expand' for php +## ## Revision 1.21 2003/04/09 07:21:56 joko ## childObj2string now inside Encode.pm, renamed to 'twingle_reference' ## @@ -166,6 +175,7 @@ # 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 +# TODO: Review: Could this be revamped using Clone.pm? sub expand { my $obj = shift; @@ -175,7 +185,11 @@ #print "ref: ", ref $obj, "\n"; if (ref $obj eq 'ARRAY') { - + + # if we expand for php, keep empty ARRAY + if($#$obj == -1 && $options->{childObj2string}) { + $result = $obj; + } else { IterArray @$obj, sub { my $item; # if current item is a reference ... @@ -200,10 +214,13 @@ $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); } + + $item = '' if $options->{define} and not defined $item; #push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) push(@{$result}, $item); # use item in any case } + } } elsif (ref $obj eq 'CODE') { #print Dumper($obj); @@ -211,16 +228,26 @@ # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? } elsif (ref $obj) { - + + # if we expand for php, keep empty HASH + my @tmp = keys %$obj; + if($#tmp == -1 && $options->{childObj2string}) { + $result = $obj; + } else { IterHash %$obj, sub { + my $key = $_[0]; my $item; - + + # conversions/encodings + $key = latin_to_utf8_scalar($key) if ($key && $options->{utf8}); + $key = utf8_to_latin_scalar($key) if ($key && $options->{to_latin}); + # if current item is a reference ... if (ref $_[1]) { $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")) { + if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH") && !(ref $_[1] eq "Set::Object")) { $item = twingle_reference($item); } else { # ... expand structure recursively @@ -237,9 +264,12 @@ $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); } + + $item = '' if $options->{define} and not defined $item; #$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) - $result->{$_[0]} = $item; # use item in any case + $result->{$key} = $item; # use item in any case } + } } else { #die ("not a reference!");