/[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.9 by joko, Tue Feb 18 19:35:56 2003 UTC revision 1.10 by joko, Thu Feb 20 20:48:00 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.10  2003/02/20 20:48:00  joko
6    ##  - refactored lots of code to Data::Code::Ref
7    ##  + alternative 'deep_copy' implementation
8    ##
9  ##  Revision 1.9  2003/02/18 19:35:56  joko  ##  Revision 1.9  2003/02/18 19:35:56  joko
10  ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too  ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
11  ##  ##
# Line 44  use warnings; Line 48  use warnings;
48  require Exporter;  require Exporter;
49  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
50  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
   &var_NumericalHashToArray  
   &var_mixin  
   &object2hash  
   &hash2object  
   &refexpr2perlref  
51    &expand    &expand
52    &deep_copy    &deep_copy
53    &merge    &merge_to
54  );  );
55  #  &var_deref  
 #  &getStructSlotByStringyAddress  
56    
57  use attributes;  use attributes;
58  use Data::Dumper;  use Data::Dumper;
 use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 );  
   
 use Hash::Merge qw( merge );  
59  use Iterate;  use Iterate;
60    
61  sub var_NumericalHashToArray {  use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar );
62    use Data::Code::Ref qw( ref_slot );
63    
64    sub numhash2list {
65    my $vref = shift;    my $vref = shift;
66    #my $context = shift;    #my $context = shift;
67    if (ref $vref eq 'HASH') {    if (ref $vref eq 'HASH') {
# Line 79  sub var_NumericalHashToArray { Line 77  sub var_NumericalHashToArray {
77                }                }
78                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
79              }              }
80              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
81            }            }
82  #      } else {  #      } else {
83  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 92  sub var_NumericalHashToArray { Line 90  sub var_NumericalHashToArray {
90  # FIXME: could this be refactored using expand?  # FIXME: could this be refactored using expand?
91  # btw: "expand": look at scripts@CPAN (System Administration):  # btw: "expand": look at scripts@CPAN (System Administration):
92  # there is a perl make with perl  # there is a perl make with perl
93  sub var_deref {  sub _var_deref_test {
94    my $obj = shift;    my $obj = shift;
95    my $options = shift;    my $options = shift;
96    my $result;    my $result;
# Line 137  sub expand { Line 135  sub expand {
135    my $options = shift;    my $options = shift;
136    my $result;    my $result;
137    
138    if ((ref $obj) eq 'ARRAY') {    #print "ref: ", ref $obj, "\n";
139    
140      if (ref $obj eq 'ARRAY') {
141    
142      IterArray @$obj, sub {      IterArray @$obj, sub {
143        my $item;        my $item;
# Line 153  sub expand { Line 153  sub expand {
153          #$item = deep_copy($_[0]);          #$item = deep_copy($_[0]);
154          $item = $_[0];          $item = $_[0];
155          # conversions/encodings          # conversions/encodings
156          $item = scalar2utf8($item) if ($item && $options->{utf8});          $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
157            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
158        }        }
159        #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)
160        push(@{$result}, $item);                 # use item in any case        push(@{$result}, $item);                 # use item in any case
161    
162      }      }
163    
164      } elsif (ref $obj eq 'CODE') {
165        #print Dumper($obj);
166        #exit;
167    
168    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
169    } else {    } elsif (ref $obj) {
170    
171      IterHash %$obj, sub {      IterHash %$obj, sub {
172        my $item;        my $item;
# Line 178  sub expand { Line 183  sub expand {
183          #$item = deep_copy($_[1]);          #$item = deep_copy($_[1]);
184          $item = $_[1];          $item = $_[1];
185          # conversions/encodings          # conversions/encodings
186          $item = scalar2utf8($item) if ($item && $options->{utf8});          $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
187            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
188        }        }
189        #$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)
190        $result->{$_[0]} = $item;               # use item in any case        $result->{$_[0]} = $item;               # use item in any case
191      }      }
192    
193      } else {
194        #die ("not a reference!");
195        $result = $obj;
196    
197    }    }
198    
199    # convert all values to utf8 (inside complex struct)    # convert all values to utf8 (inside complex struct)
# Line 201  sub expand { Line 211  sub expand {
211    
212    
213  my @indexstack;  my @indexstack;
214  sub var_traverse_mixin_update {  sub _var_traverse_mixin_update_old {
215    my $obj = shift;    my $obj = shift;
216    my $data = shift;    my $data = shift;
217    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 245  sub var_traverse_mixin_update { Line 255  sub var_traverse_mixin_update {
255    #return $result;    #return $result;
256  }  }
257    
258  sub object2hash {  sub merge_to {
259    my $object = shift;    _hash2object(@_);
260    my $options = shift;    # TODO:
261    #my $deref = var_deref($object);    # re-implement using CPAN's "Iterate".
   my $deref = expand($object, $options);  
   #var2utf8($deref) if ($options->{utf8});  
   return $deref;  
262  }  }
263    
264    
265  # todo: maybe do diff(s) here sometimes!?  # todo: maybe do diff(s) here sometimes!?
266  sub hash2object {  sub _hash2object {
267    my $object = shift;    my $object = shift;
268    my $data = shift;    my $data = shift;
269    my $options = shift;    my $options = shift;
# Line 264  sub hash2object { Line 272  sub hash2object {
272        
273    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
274    # this method corrects this    # this method corrects this
275    var_NumericalHashToArray($data) if ($options->{php});    numhash2list($data) if ($options->{php});
276    
277    # utf8-conversion/-encoding (essential for I18N)    # utf8-conversion/-encoding (essential for I18N)
278    var_utf2iso($data) if ($options->{utf8});    var_utf2iso($data) if ($options->{utf8});
# Line 276  sub hash2object { Line 284  sub hash2object {
284    #my $obj = $self->getObject($oid);    #my $obj = $self->getObject($oid);
285    
286    # mix changes into fresh object and save it back    # mix changes into fresh object and save it back
287    hash2object_traverse_mixin($object, $data, 0, $options);    _hash2object_traverse_mixin($object, $data, 0, $options);
288    
289    # done in core mixin function?    # done in core mixin function?
290    #$self->{storage}->update($obj);    #$self->{storage}->update($obj);
# Line 299  sub hash2object { Line 307  sub hash2object {
307  #my @indexstack;  #my @indexstack;
308    
309  # 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
310  sub hash2object_traverse_mixin {  sub _hash2object_traverse_mixin {
311    my $object = shift;    my $object = shift;
312    my $data = shift;    my $data = shift;
313    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 317  sub hash2object_traverse_mixin { Line 325  sub hash2object_traverse_mixin {
325    # what's exactly done here? hmmm.... it works ;)    # what's exactly done here? hmmm.... it works ;)
326    # 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!!!
327    # extend! check!    # extend! check!
328    if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {    
329      #print attributes::reftype($object), "\n";
330      
331      # V1
332      #if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
333      # V2
334      my $reftype = attributes::reftype($object);
335      #print "reftype: '$reftype'", "\n";
336      if ($reftype eq 'HASH') {
337    
338  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
339                    
# Line 337  sub hash2object_traverse_mixin { Line 353  sub hash2object_traverse_mixin {
353        my $ref = ref $object->{$field};        my $ref = ref $object->{$field};
354  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
355        if ($ref) {        if ($ref) {
356          hash2object_traverse_mixin($object->{$field}, $data, 1, $options);          _hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
357        } else {        } else {
358          my $val = getStructSlotByStringyAddress($data, \@indexstack);          my $val = ref_slot($data, \@indexstack);
359                    
360  #print Dumper($options);  #print Dumper($options);
361          my $field_target = $field;          my $field_target = $field;
# Line 382  sub hash2object_traverse_mixin { Line 398  sub hash2object_traverse_mixin {
398  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
399  #      if ($ref && $_) {  #      if ($ref && $_) {
400        if ($ref) {        if ($ref) {
401          hash2object_traverse_mixin($_, $data, 1, $options);          _hash2object_traverse_mixin($_, $data, 1, $options);
402        } else {        } else {
403          $object->[$i] = $_ if defined $_;          $object->[$i] = $_ if defined $_;
404        }        }
405        pop @indexstack;        pop @indexstack;
406        $i++;        $i++;
407      }      }
408    
409    =pod
410      # object?
411      } else {
412        print "reference: ", ref $object, "\n";
413        print "reftype: ", $reftype, "\n";
414        die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!");
415    =cut
416      
417    }    }
418    
419  }  }
420    
421    
422  # this function seems to do similar stuff like these below (refexpr2perlref & co.)  # ACK's go to Randal L. Schwartz <merlyn@stonehenge.com>
423  # TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ?   ;-)  # on the website:
424  sub getStructSlotByStringyAddress {  # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
425    my $var = shift;  # Further distribution or use is not permitted.
426    my $indexstack_ref = shift;  # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
427    my @indexstack = @{$indexstack_ref};  sub deep_copy1 {
428    my $joiner = '->';    my $this = shift;
429    my @evlist;    if (not ref $this) {
430    foreach (@indexstack) {      $this;
431      my $elem;    } elsif (ref $this eq "ARRAY") {
432      if ($_ =~ m/\d+/) { $elem = "[$_]"; }      [map deep_copy1($_), @$this];
433      if ($_ =~ m/\D+/) { $elem = "{$_}"; }    } elsif (ref $this eq "HASH") {
434      push @evlist, $elem;      +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
435    }    } elsif (ref $this eq "CODE") {
436    my $evstring = join($joiner, @evlist);      $this;
437    $evstring = 'return $var->' . $evstring . ';';    } else { die "deep_copy1 asks: what type is $this?" }
438    #print "ev: $evstring\n";    #} else { print "deep_copy asks: what type is $this?", "\n"; }
   return eval($evstring);  
439  }  }
440    
441    # ACK's go to Andrew Pitonyak
442  sub refexpr2perlref {  # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)
443    # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html
444    my $obj = shift;  sub deep_copy2 {
445    my $expr = shift;  
446    my $values = shift;      # if not defined then return it
447    my $delimiter = shift;      return undef if $#_ < 0 || !defined( $_[0] );
448    
449    $delimiter ||= '->';      # if not a reference then return the parameter
450        return $_[0] if !ref( $_[0] );
451    my $value;      my $obj = shift;
452    # detect for callback (code-reference)      if ( UNIVERSAL::isa( $obj, 'SCALAR' ) ) {
453    if (ref($expr) eq 'CODE') {          my $temp = deep_copy2($$obj);
454      $value = &$expr($obj);          return \$temp;
455        }
456    } elsif ($expr =~ m/$delimiter/) {      elsif ( UNIVERSAL::isa( $obj, 'HASH' ) ) {
457      # use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute)          my $temp_hash = {};
458      (my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr, $delimiter);          foreach my $key ( keys %$obj ) {
459                    if ( !defined( $obj->{$key} ) || !ref( $obj->{$key} ) ) {
460      # trace                  $temp_hash->{$key} = $obj->{$key};
461        #print "expr: $expr", "\n";              }
462        #print "objPerlRefString: $objPerlRefString", "\n";              else {
463                        $temp_hash->{$key} = deep_copy2( $obj->{$key} );
464      my $evstr = '$obj' . '->' . $objPerlRefString;              }
465                }
466      # trace          return $temp_hash;
       #print "eval: $evstr", "\n";  
       
     $value = eval($evstr);  
     die ($@) if $@;  
       
     # if value isn't set, try to set it  
     my $callbackMap;  
     if ($values) {  
       #if (ref $values eq 'HASH') {  
       if (ref $values eq 'Data::Map') {  
         $callbackMap = $values->getAttributes();  
       } else {  
         # apply $values as single value to referenced slot  
         #print "APPLY!", "\n";  
         eval('$obj' . '->' . $objPerlRefString . ' = $values;');  
         die ($@) if $@;  
       }  
467      }      }
468            elsif ( UNIVERSAL::isa( $obj, 'ARRAY' ) ) {
469      # determine $values - if it's a hashref, use it as a "valueMap"          my $temp_array = [];
470      # "fallback" to callbackMap          foreach my $array_val (@$obj) {
471      # callbacks are applied this way:              if ( !defined($array_val) || !ref($array_val) ) {
472      #   take the last element of the expression parts and check if this string exists as a key in the callback-map                  push ( @$temp_array, $array_val );
473      if (!$value && $callbackMap) {              }
474        #print Dumper($callbackMap);              else {
475                          push ( @$temp_array, deep_copy2($array_val) );
476        # prepare needle              }
477        my @parts = @$parts;          }
478        my $count = $#parts;          return $temp_array;
       my $needle = $parts[$count];  
   
       # prepare haystack  
       my @haystack = keys %$callbackMap;  
       if (grep($needle, @haystack)) {  
         $value = $callbackMap->{$needle}->();  
         #print "value: ", $value, "\n";  
       }  
479      }      }
480    
481    } else {      # ?? I am uncertain about this one
482      # use expr as simple scalar key (attributename)      elsif ( UNIVERSAL::isa( $obj, 'REF' ) ) {
483      $value = $obj->{$expr};          my $temp = deep_copy2($$obj);
484    }          return \$temp;
485                  }
   return $value;  
     
 }  
   
   
 sub refexpr2perlref_parts {  
   my $expr = shift;  
   my $delimiter = shift;  
   
   $delimiter = quotemeta($delimiter);  
   
   # split expression by dereference operators first  
   my @parts_pure = split(/$delimiter/, $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;  
486    
487    # join parts with dereference operators together again and return built string      # I guess that it is either CODE, GLOB or LVALUE
488    return (join('->', @parts_capsule), \@parts_pure);      else {
489            return $obj;
490        }
491  }  }
492    
 # ACK's go to ...  
493  sub deep_copy {  sub deep_copy {
494    my $this = shift;    deep_copy2(@_);
   if (not ref $this) {  
     $this;  
   } elsif (ref $this eq "ARRAY") {  
     [map deep_copy($_), @$this];  
   } elsif (ref $this eq "HASH") {  
     +{map { $_ => deep_copy($this->{$_}) } keys %$this};  
   } elsif (ref $this eq "CODE") {  
     $this;  
   #} else { die "deep_copy asks: what type is $this?" }  
   } else { print "deep_copy asks: what type is $this?", "\n"; }  
495  }  }
496    
497  1;  1;
498    __END__

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

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