/[cvs]/nfo/perl/libs/Data/Mungle/Transform/Deep.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Mungle/Transform/Deep.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by joko, Sun Feb 9 05:10:56 2003 UTC revision 1.12 by joko, Thu Feb 20 22:45:19 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.12  2003/02/20 22:45:19  joko
6    ##  fix regarding new deep_copy
7    ##
8    ##  Revision 1.11  2003/02/20 21:13:54  joko
9    ##  - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository)
10    ##
11    ##  Revision 1.10  2003/02/20 20:48:00  joko
12    ##  - refactored lots of code to Data::Code::Ref
13    ##  + alternative 'deep_copy' implementation
14    ##
15    ##  Revision 1.9  2003/02/18 19:35:56  joko
16    ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
17    ##
18  ##  Revision 1.8  2003/02/09 05:10:56  joko  ##  Revision 1.8  2003/02/09 05:10:56  joko
19  ##  + minor update  ##  + minor update
20  ##  ##
# Line 41  use warnings; Line 54  use warnings;
54  require Exporter;  require Exporter;
55  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
56  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
   &var_NumericalHashToArray  
   &var_mixin  
   &object2hash  
   &hash2object  
   &refexpr2perlref  
57    &expand    &expand
58    &deep_copy    &deep_copy
59    &merge    &merge_to
60  );  );
61  #  &var_deref  
 #  &getStructSlotByStringyAddress  
62    
63  use attributes;  use attributes;
64  use Data::Dumper;  use Data::Dumper;
 use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 );  
   
 use Hash::Merge qw( merge );  
65  use Iterate;  use Iterate;
66    
67  sub var_NumericalHashToArray {  use Pitonyak::DeepCopy;
68    use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar );
69    use Data::Code::Ref qw( ref_slot );
70    
71    sub numhash2list {
72    my $vref = shift;    my $vref = shift;
73    #my $context = shift;    #my $context = shift;
74    if (ref $vref eq 'HASH') {    if (ref $vref eq 'HASH') {
# Line 76  sub var_NumericalHashToArray { Line 84  sub var_NumericalHashToArray {
84                }                }
85                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
86              }              }
87              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
88            }            }
89  #      } else {  #      } else {
90  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 86  sub var_NumericalHashToArray { Line 94  sub var_NumericalHashToArray {
94  }  }
95    
96    
97  sub var_deref {  # FIXME: could this be refactored using expand?
98    # btw: "expand": look at scripts@CPAN (System Administration):
99    # there is a perl make with perl
100    sub _var_deref_test {
101    my $obj = shift;    my $obj = shift;
102    my $options = shift;    my $options = shift;
103    my $result;    my $result;
# Line 131  sub expand { Line 142  sub expand {
142    my $options = shift;    my $options = shift;
143    my $result;    my $result;
144    
145    if ((ref $obj) eq 'ARRAY') {    #print "ref: ", ref $obj, "\n";
146    
147      if (ref $obj eq 'ARRAY') {
148    
149      IterArray @$obj, sub {      IterArray @$obj, sub {
150        my $item;        my $item;
# Line 147  sub expand { Line 160  sub expand {
160          #$item = deep_copy($_[0]);          #$item = deep_copy($_[0]);
161          $item = $_[0];          $item = $_[0];
162          # conversions/encodings          # conversions/encodings
163          $item = scalar2utf8($item) if ($item && $options->{utf8});          $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
164            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
165        }        }
166        #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)
167        push(@{$result}, $item);                 # use item in any case        push(@{$result}, $item);                 # use item in any case
168    
169      }      }
170    
171      } elsif (ref $obj eq 'CODE') {
172        #print Dumper($obj);
173        #exit;
174    
175    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
176    } else {    } elsif (ref $obj) {
177    
178      IterHash %$obj, sub {      IterHash %$obj, sub {
179        my $item;        my $item;
# Line 172  sub expand { Line 190  sub expand {
190          #$item = deep_copy($_[1]);          #$item = deep_copy($_[1]);
191          $item = $_[1];          $item = $_[1];
192          # conversions/encodings          # conversions/encodings
193          $item = scalar2utf8($item) if ($item && $options->{utf8});          $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
194            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
195        }        }
196        #$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)
197        $result->{$_[0]} = $item;               # use item in any case        $result->{$_[0]} = $item;               # use item in any case
198      }      }
199    
200      } else {
201        #die ("not a reference!");
202        $result = $obj;
203    
204    }    }
205    
206    # convert all values to utf8 (inside complex struct)    # convert all values to utf8 (inside complex struct)
# Line 195  sub expand { Line 218  sub expand {
218    
219    
220  my @indexstack;  my @indexstack;
221  sub var_traverse_mixin_update {  sub _var_traverse_mixin_update_old {
222    my $obj = shift;    my $obj = shift;
223    my $data = shift;    my $data = shift;
224    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 239  sub var_traverse_mixin_update { Line 262  sub var_traverse_mixin_update {
262    #return $result;    #return $result;
263  }  }
264    
265  sub object2hash {  sub merge_to {
266    my $object = shift;    _hash2object(@_);
267    my $options = shift;    # TODO:
268    #my $deref = var_deref($object);    # re-implement using CPAN's "Iterate".
   my $deref = expand($object, $options);  
   #var2utf8($deref) if ($options->{utf8});  
   return $deref;  
269  }  }
270    
271    
272  # todo: maybe do diff(s) here sometimes!?  # todo: maybe do diff(s) here sometimes!?
273  sub hash2object {  sub _hash2object {
274    my $object = shift;    my $object = shift;
275    my $data = shift;    my $data = shift;
276    my $options = shift;    my $options = shift;
# Line 258  sub hash2object { Line 279  sub hash2object {
279        
280    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
281    # this method corrects this    # this method corrects this
282    var_NumericalHashToArray($data) if ($options->{php});    numhash2list($data) if ($options->{php});
283    
284    # utf8-conversion/-encoding (essential for I18N)    # utf8-conversion/-encoding (essential for I18N)
285    var_utf2iso($data) if ($options->{utf8});    var_utf2iso($data) if ($options->{utf8});
# Line 270  sub hash2object { Line 291  sub hash2object {
291    #my $obj = $self->getObject($oid);    #my $obj = $self->getObject($oid);
292    
293    # mix changes into fresh object and save it back    # mix changes into fresh object and save it back
294    hash2object_traverse_mixin($object, $data, 0, $options);    _hash2object_traverse_mixin($object, $data, 0, $options);
295    
296    # done in core mixin function?    # done in core mixin function?
297    #$self->{storage}->update($obj);    #$self->{storage}->update($obj);
# Line 293  sub hash2object { Line 314  sub hash2object {
314  #my @indexstack;  #my @indexstack;
315    
316  # 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
317  sub hash2object_traverse_mixin {  sub _hash2object_traverse_mixin {
318    my $object = shift;    my $object = shift;
319    my $data = shift;    my $data = shift;
320    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 311  sub hash2object_traverse_mixin { Line 332  sub hash2object_traverse_mixin {
332    # what's exactly done here? hmmm.... it works ;)    # what's exactly done here? hmmm.... it works ;)
333    # 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!!!
334    # extend! check!    # extend! check!
335    if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {    
336      #print attributes::reftype($object), "\n";
337      
338      # V1
339      #if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
340      # V2
341      my $reftype = attributes::reftype($object);
342      #print "reftype: '$reftype'", "\n";
343      if ($reftype eq 'HASH') {
344    
345  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
346                    
# Line 331  sub hash2object_traverse_mixin { Line 360  sub hash2object_traverse_mixin {
360        my $ref = ref $object->{$field};        my $ref = ref $object->{$field};
361  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
362        if ($ref) {        if ($ref) {
363          hash2object_traverse_mixin($object->{$field}, $data, 1, $options);          _hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
364        } else {        } else {
365          my $val = getStructSlotByStringyAddress($data, \@indexstack);          my $val = ref_slot($data, \@indexstack);
366                    
367  #print Dumper($options);  #print Dumper($options);
368          my $field_target = $field;          my $field_target = $field;
# Line 376  sub hash2object_traverse_mixin { Line 405  sub hash2object_traverse_mixin {
405  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
406  #      if ($ref && $_) {  #      if ($ref && $_) {
407        if ($ref) {        if ($ref) {
408          hash2object_traverse_mixin($_, $data, 1, $options);          _hash2object_traverse_mixin($_, $data, 1, $options);
409        } else {        } else {
410          $object->[$i] = $_ if defined $_;          $object->[$i] = $_ if defined $_;
411        }        }
412        pop @indexstack;        pop @indexstack;
413        $i++;        $i++;
414      }      }
   }  
   
 }  
   
   
 # this function seems to do similar stuff like these below (refexpr2perlref & co.)  
 # TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ?   ;-)  
 sub getStructSlotByStringyAddress {  
   my $var = shift;  
   my $indexstack_ref = shift;  
   my @indexstack = @{$indexstack_ref};  
   my $joiner = '->';  
   my @evlist;  
   foreach (@indexstack) {  
     my $elem;  
     if ($_ =~ m/\d+/) { $elem = "[$_]"; }  
     if ($_ =~ m/\D+/) { $elem = "{$_}"; }  
     push @evlist, $elem;  
   }  
   my $evstring = join($joiner, @evlist);  
   $evstring = 'return $var->' . $evstring . ';';  
   #print "ev: $evstring\n";  
   return eval($evstring);  
 }  
   
   
 sub refexpr2perlref {  
   
   my $obj = shift;  
   my $expr = shift;  
   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";  
       }  
     }  
415    
416    =pod
417      # object?
418    } else {    } else {
419      # use expr as simple scalar key (attributename)      print "reference: ", ref $object, "\n";
420      $value = $obj->{$expr};      print "reftype: ", $reftype, "\n";
421    }      die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!");
422              =cut
   return $value;  
423        
424  }    }
   
   
 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;  
425    
   # join parts with dereference operators together again and return built string  
   return (join('->', @parts_capsule), \@parts_pure);  
426  }  }
427    
428  # ACK's go to ...  
429  sub deep_copy {  # ACK's go to Randal L. Schwartz <merlyn@stonehenge.com>
430    # on the website:
431    # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
432    # Further distribution or use is not permitted.
433    # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
434    sub deep_copy1 {
435    my $this = shift;    my $this = shift;
436    if (not ref $this) {    if (not ref $this) {
437      $this;      $this;
438    } elsif (ref $this eq "ARRAY") {    } elsif (ref $this eq "ARRAY") {
439      [map deep_copy($_), @$this];      [map deep_copy1($_), @$this];
440    } elsif (ref $this eq "HASH") {    } elsif (ref $this eq "HASH") {
441      +{map { $_ => deep_copy($this->{$_}) } keys %$this};      +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
442    } elsif (ref $this eq "CODE") {    } elsif (ref $this eq "CODE") {
443      $this;      $this;
444    #} else { die "deep_copy asks: what type is $this?" }    } else { die "deep_copy1 asks: what type is $this?" }
445    } else { print "deep_copy asks: what type is $this?", "\n"; }    #} else { print "deep_copy asks: what type is $this?", "\n"; }
446  }  }
447    
448    # ACK's go to Andrew Pitonyak
449    # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)
450    # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html
451    sub deep_copy {
452      Pitonyak::DeepCopy::deep_copy(@_);
453    }
454    
455    
456  1;  1;
457    __END__

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.12

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed