in reply to Re^4: Creating flexible method accessor
in thread Creating flexible method accessor

${path} is really just a different way of writing $path:

my $foo = "xyz"; print $foo."bar"; # prints "xyzbar" print "${foo}bar"; # same

As for making it more flexible: let's say you've got your "path" in an array @path, one way to walk your data structure is: $self = $self->{$_} for @path;

Replies are listed 'Best First'.
Re^6: Creating flexible method accessor
by puterboy (Scribe) on Feb 02, 2014 at 04:08 UTC
    The problem with 'walking the path' is that there is no guarantee that the path will be: ->{hash1}->{hash2}->{hash3}
    It could be any combination of hashes, hashrefs, arrays, arrayrefs -- which is why I wanted to pass a *string* rather than a list (array) of hash keys.

      Then you might want to start looking into CPAN modules, for example a really quick search brings up Data::Path.

      Otherwise, although I would really try to avoid string eval as much as possible, when it is used it's best to limit its scope as much as possible: $self = eval qq{ \$self->$path }; die $@ if $@;