Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Creating flexible method accessor

by puterboy (Scribe)
on Feb 02, 2014 at 03:04 UTC ( #1073023=note: print w/replies, xml ) Need Help??


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

Actually, to clarify -- your suggestion of using ${path} works fine when applied to my original script which allows the multi-level hierarchies I need.

However, it is not obvious (to me) how to make this work in your non-eval script. If I use ${path}, I get all sorts of errors.

Unfortunately, the exercise you leave to the reader is actually the exercise that was the point of my original question :) (though I quite appreciate your provided script).
  • Comment on Re^4: Creating flexible method accessor

Replies are listed 'Best First'.
Re^5: Creating flexible method accessor
by Anonymous Monk on Feb 02, 2014 at 03:50 UTC

    ${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;

      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 $@;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1073023]
help
Chatterbox?
[Corion]: This time it's for the Duktape embedded Javascript engine, which would be nice to have IMO, but doesn't compile on Windows currently
[Corion]: I imagine it would be fun to have WWW::Mechanize:: Duktape, but I haven't seen a web browser written in Javascript yet, except the ones distributed with PhantomJs

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-03-27 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (319 votes). Check out past polls.