use strict;
use warnings;
use Data::Dumper;
_create_node ("p1","pp1","first");
_create_node ("pp1","ppp1","second");
_create_node ("ppp1","pppp1","third");
_create_node ("pppp1","ppppp1","root");
my $infos = _go_through("p1");
print Dumper $infos;
sub _create_node
{
my ($node,$parent,$node_info) = @_;
no strict 'refs';
@{$node}{qw/parent info/} = ($parent,$node_info);
}
sub _go_through
{
no strict 'refs';
my ($node) = @_;
return "" unless defined ${$node}{"info"};
my @info_nodes;
while (${$node}{"info"} ne "root"){
push @info_nodes, (${$node}{"info"});
$node = ${$node}{"parent"};
}
return \@info_nodes;
}
Outputs:
$VAR1 = [
'first',
'second',
'third'
];
We can argue if this use of symbolic references is a correct strategy or not, but I hope that we agree that they ARE symbolic references
Your subroutine using the arrow notation works as well, excepts that you need to switch off strict-ness:
sub _go_through {
no strict 'refs';
my ($node) = @_;
return unless defined $node->{info};
my @info_nodes;
while ( $node->{info} ne 'root' ) {
push @info_nodes, $node->{info};
$node = $node->{parent};
}
return \@info_nodes;
}
Or you will get pretty
Can't use string ("p1") as a HASH ref while "strict refs" in use at ..
+.
citromatik |