in reply to Re^3: Invoke sub whose name is value of scalar
in thread Invoke sub whose name is value of scalar
I don't know if you have understand correctly my snippet. Here is a working example to clarify:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Invoke sub whose name is value of scalar
by dragonchild (Archbishop) on Apr 14, 2008 at 15:23 UTC | |
by citromatik (Curate) on Apr 14, 2008 at 15:58 UTC | |
by dragonchild (Archbishop) on Apr 14, 2008 at 16:02 UTC | |
by citromatik (Curate) on Apr 14, 2008 at 16:27 UTC | |
by dragonchild (Archbishop) on Apr 14, 2008 at 16:47 UTC | |
by Arunbear (Prior) on Apr 14, 2008 at 16:42 UTC | |
by Jenda (Abbot) on Apr 14, 2008 at 19:42 UTC |