while ( my $hashref = $query->fetchrow_hashref() ) { my $tmpref = $noderef{ $$hashref{pid} }; #reference to the parent node my $cid = $$hashref{cid}; #CID will be used several times my $nodeone = Tree::DAG_Node->new( { name => $cid, #CID as node name mother => $$tmpref, #mother is the parent } ); $noderef{ $cid } = \$nodeone; #add this node ref to the hash $comm_hashref{ $cid } = $hashref; }