Hello.
You return nothing at recursive call. But, at first, you will see lots of warnings if you put use strict and use warnings for your code...
It maybe like this.
Moose has "Recipe 3" for Binary Tree. It is far more advanced.{ package bin_tree; use strict; use warnings; sub new{ my $class=shift; my $self={value=>undef, a=>undef, b=>undef}; #make our object as a +n anonymous hash with keywords but no values yet, that way the a an $self->{value}=0; bless $self, $class; #make object return $self; #return object } sub populate{ my($self, $depth)=@_; return if(! $depth); $self->{a} = populate( bin_tree->new() , $depth -1 ); $self->{b} = populate( bin_tree->new() , $depth -1 ); return $self; } 1; } #end bin_tree package main; use strict; use warnings; #try to create a three node deep binary tree my $bintree = bin_tree->new(); $bintree->populate(3); use Data::Dumper; print Dumper $bintree;
In reply to Re: recursive creation of attached objects?
by remiah
in thread recursive creation of attached objects?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |