Tree of user defined objects 
Author Message
 Tree of user defined objects

Hi,

 Following statement
$hashtree->{a}->{b}->{c}->{d} = undef;

will create tree(one branch) of hashobjects with leaf being
undefined. My question is it possible to create tree of user
defined objects say "Node()" maintaining the same syntax as
above. That is following statement

$mytree->{a}->{b}->{c}->{d} = undef;

should give me tree of Node objects. At the creation of each
successive element in the tree perl should call constructor
of object Node. If possibe how can i do this. I tried with
"tie" but not successful.

Thanks a lot,
sreekanth



Tue, 09 Sep 2003 20:19:47 GMT  
 Tree of user defined objects


Quote:

>$mytree->{a}->{b}->{c}->{d} = undef;

>should give me tree of Node objects. At the creation of each
>successive element in the tree perl should call constructor
>of object Node. If possibe how can i do this. I tried with
>"tie" but not successful.

You can do that, but it's tricky.  The FETCH and STORE methods will be
quite complicated.  FETCH will have to look in the structure, and if
the item it is asked to retrieve is undefined, it must create a new
tied hash, inatll it, and return a reference to it.

Here's a limited example.  In this example, the depth of the tree is
fixed in advance.  With a little extra cleverness you can remove this
restriction.  

----------------------------------------------------------------
tie %adder => MultiHash, 2;
print $adder{4}{5}{6}, "\n";    # Print 4*5*6 = 120
print $adder{7}{2}{1}, "\n";    # Print 7*2*1 =  14

package MultiHash;

sub TIEHASH {

  $topval = 1 unless defined $topval;
  my $self = {__depth => $depth, __topval => $topval};
  bless $self => MultiHash;

Quote:
}

sub FETCH {

  return $self->{$key} if exists $self->{$key};
  my $depth = $self->{__depth};
  if ($depth == 0) {
    return $self->{$key} = $key * $self->{__topval};
  }

  my %newhash;
  tie %newhash => MultiHash, $depth-1, $self->{__topval} * $key;
  return $self->{$key} = \%newhash;

Quote:
}

sub STORE {
  require Carp;
  Carp::croak('STORE not allowed');
Quote:
}

----------------------------------------------------------------

Here are some additions so that we can use Data::Dumper to dump out
the tree to prove that a tree structure is really being created:

----------------------------------------------------------------
use Data::Dumper;

tie %adder => MultiHash, 2;
print $adder{4}{5}{6}, "\n";
print $adder{7}{2}{1}, "\n";

print Dumper(\%adder);

package MultiHash;

sub TIEHASH {

  $topval = 1 unless defined $topval;
  my $self = {__depth => $depth, __topval => $topval};
  bless $self => MultiHash;

Quote:
}

sub FETCH {

  return $self->{$key} if exists $self->{$key};
  my $depth = $self->{__depth};
  if ($depth == 0) {
    return $self->{$key} = $key * $self->{__topval};
  }

  my %newhash;
  tie %newhash => MultiHash, $depth-1, $self->{__topval} * $key;
  return $self->{$key} = \%newhash;

Quote:
}

sub STORE {
  require Carp;
  Carp::croak('STORE not allowed');

Quote:
}

sub FIRSTKEY {
  my ($self) = shift;
  keys %$self;
  each %$self;

Quote:
}

sub NEXTKEY {
  my ($self) = shift;
  each %$self;
Quote:
}

----------------------------------------------------------------

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Tue, 09 Sep 2003 22:37:02 GMT  
 Tree of user defined objects

Quote:

> $hashtree->{a}->{b}->{c}->{d} = undef;

> will create tree(one branch) of hashobjects with leaf being
> undefined. My question is it possible to create tree of user
> defined objects say "Node()" maintaining the same syntax as
> above. That is following statement

> $mytree->{a}->{b}->{c}->{d} = undef;

> should give me tree of Node objects.

Yes it is possible.

Quote:
> At the creation of each
> successive element in the tree perl should call constructor
> of object Node. If possibe how can i do this. I tried with
> "tie" but not successful.

I said it was possible, not easy.  I participated in an intersting
thread here a few months back about exactly when you should bless and
tie autovivified things to implement such a hierachical object with hash-like
interface. Unfortunately with the down-grading of Deja I can't find
it.

Basiically the secret is that you can't simply use a contructor
because the hashes are autovivified.  What you need to do is to detect
in your STORE method that the object being stored is the sole
reference to an empty hash and promptly tie (and bless) that thing.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Wed, 10 Sep 2003 19:03:13 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Tree of user defined objects

2. How do I define a tree structure in Perl

3. Determining if a method is defined for an object

4. translating an OS directory recursively into a tree object

5. module for object tree with inheritance

6. Interpolate.pm for user-defined string interpolation semantics

7. Handling user defined tags with CGI.pm

8. User Defined Subroutines.

9. User-defined substitution

10. Would appreciate some input - user-defined function

11. Arrays and Assoc Arrays as user defined magic variables

12. Memory Leak? - Problem of Calling User-Defined Subroutines

 

 
Powered by phpBB® Forum Software