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
    Remove the no strict 'refs'; in both subroutines and try the following:
    my $root = {}; _create_node( $root, 'pp1', 'first' ); _create_node( $root->{pp1}, 'ppp1', 'second' ); # And so forth

    You were forcing your code to use symbolic references. Intrinsically, it wasn't doing so. In other words, the code didn't look like it forced the use of symbolic references. It was only your usage of it that was forcing it to do so. And, by doing it my way, you will avoid some really nasty, hard-to-find bugs.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Sorry, I don't get it

      I suppose I'm really wrong seeing the number of downvotes my post is accumulating, but I can't see your solution.

      I'm using this technique in a script that has to process a big number of lists. The symbolic link strategy is significantly faster than the hash-based solution and occupies less system memory.

      But, again, I can't see your solution. could you develop it a little more, please?

      citromatik

        The reason you're using symbolic references is because you're passing a string in. If you pass in a reference to a hash (aka, hashref), then you stop using a symbolic reference and you start using a hash reference.

        As for speed, you're completely and utterly wrong. Symbolic references ARE hashes. The hash being used is the symbol table. It actually uses MORE memory because of how the symbol table is implemented.

        Did you actually try my suggestions? Or, did you just read them and go "I don't get it."?


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

        I think you should show us an example of those lists and what do you need to do with them and I bet we can suggest a solution that will use significantly less memory and will be quicker than your symbolic references. Do not use symbolic references! Ever.