cvs commit: p5ee/App-Context/lib/App Reference.pm

[email protected] (Stephen Adkins)
Newsgroups perl.cvs.p5ee
Message-ID <[email protected]>
cvsuser     05/01/07 05:08:02

  Modified:    App-Context/lib/App Reference.pm
  Log:
  implemented overlay() more completely
  
  Revision  Changes    Path
  1.4       +28 -4     p5ee/App-Context/lib/App/Reference.pm
  
  Index: Reference.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Context/lib/App/Reference.pm,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Reference.pm	22 Mar 2003 04:04:34 -0000	1.3
  +++ Reference.pm	7 Jan 2005 13:08:02 -0000	1.4
  @@ -1,10 +1,10 @@
   
   #############################################################################
  -## $Id: Reference.pm,v 1.3 2003/03/22 04:04:34 spadkins Exp $
  +## $Id: Reference.pm,v 1.4 2005/01/07 13:08:02 spadkins Exp $
   #############################################################################
   
   package App::Reference;
  -$VERSION = do { my @r=(q$Revision: 1.3 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  +$VERSION = do { my @r=(q$Revision: 1.4 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
   
   use strict;
   
  @@ -326,6 +326,7 @@
   =cut
   
   sub overlay {
  +    &App::sub_entry if ($App::trace);
       my $self = shift;
       my ($ref1, $ref2, $key);
       return if ($#_ < 0 || $#_ > 1);
  @@ -337,9 +338,32 @@
           $ref1 = $_[0];
           $ref2 = $_[1];
       }
  -    foreach $key (keys %$ref2) {
  -        $ref1->{$key} = $ref2->{$key} if (!exists $ref1->{$key});
  +    my $ref1type = ref($ref1);
  +    my $ref2type = ref($ref2);
  +    if ($ref1type eq "ARRAY" && $ref1type eq $ref2type) {
  +        # array: nothing to do
       }
  +    elsif ($ref1type eq "" && $ref1type eq $ref2type) {
  +        # scalar: nothing to do
  +    }
  +    else {
  +        # hash
  +        foreach $key (keys %$ref2) {
  +            if (!exists $ref1->{$key}) {
  +                $ref1->{$key} = $ref2->{$key};
  +            }
  +            else {
  +                $ref1type = ref($ref1->{$key});
  +                if ($ref1type && $ref1type ne "ARRAY") {
  +                    $ref2type = ref($ref2->{$key});
  +                    if ($ref1type eq $ref2type) {
  +                        $self->overlay($ref1->{$key}, $ref2->{$key});
  +                    }
  +                }
  +            }
  +        }
  +    }
  +    &App::sub_exit() if ($App::trace);
   }
   
   #############################################################################
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.