in reply to RE: Nesting... or something.
in thread Nesting... or something.

This is almost what I wanted. I wanted the builder sub to actually build on the hash though. This seems to replace it. Each successive call to the builder should add to the contents of the hash. BTW, I was the original poster, but my cookies were foob'd.

Replies are listed 'Best First'.
RE: RE: RE: Nesting... or something.
by mdillon (Priest) on Jun 03, 2000 at 22:16 UTC
    what you want is not possible, because you can't have a single hash key retrieve both a plain scalar and a hash reference.
    my %hash; $hash{foo} = 'bar'; # the following replaces the value of $hash{foo} with a hash reference $hash{foo}{bar} = 'baz'; # the following is equivalent $hash{foo} = { bar => 'baz' };
    so, if someone did: builder('baz', [ 'foo', 'bar' ]); it would wipe out the effect of: builder('bar', [ 'foo' ]);
      What I wanted was possible.
      sub build_args() {  # Based on fastolfe's answer
          my $data = shift;
          my $ptr = \%The_Big_Hash;
          my $me  = pop @_;
          foreach my $host (@_) {
              $ptr = $ptr->{$host}{deps};
          }
          $ptr->{$me}{args} = [ @{ $data } ];
          $ptr->{$me}{deps} = {};
      }
      
RE: RE: RE: Nesting... or something.
by Fastolfe (Vicar) on Jun 05, 2000 at 00:01 UTC
    Pass the hash as a reference instead of using %hash, and on the line where it assigns the $ptr->{$key} = {} add something like "unless ref($ptr->{$key}) eq 'HASH'" or something.. that will probably wipe out any existing values you put in there though.. e.g.:
    builder(\%test, 'abc', 1, 2); # $test{1}{2} = 'abc' builder(\%test, 'def', 1, 2, 3); # $test{1}{2} is now a hashref, not +'abc'