Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Dynamic Dereferencing

by markong (Pilgrim)
on Dec 07, 2018 at 12:10 UTC ( [id://1226879]=note: print w/replies, xml ) Need Help??


in reply to Dynamic Dereferencing

# Convert a . to }->{ in order for meta.created to look like meta}->{created $element =~ s/\./}->{/g; # This works for all elements except of meta}->{created $deref = $item->{$element}; print "Convert: \$item->{" . $element . "}\t\t\t" . $deref . "\n"; }

I think you are trying to achieve something with a sort of "meta-coding" which is quite dangerous: when you write $item->{$element} you're try to access the hash $item value with a key like "...}->{..." which is why you then get that error, because there isn't such a key!

If you really want to do such a sort of meta-coding, then you need to write for instance  "$item->{".$element."}" and then you can try to eval() that thing. Why instead not trying one of the modules recommended by haukex ?

Replies are listed 'Best First'.
Re^2: Dynamic Dereferencing
by Anonymous Monk on Dec 07, 2018 at 12:25 UTC
    String eval is not to be used . especially not as a substitute for functions and loops for dereference

      ?

      I was replying to the OP on why he had that error and the only reasonable way to correct that code. I never recommended eval-ing concatenated strings as a good practice. The opposite as a matter of facts!

      Eval()-ing has, of course, its use cases, but as I said, it's dangerous in this particular case. One of the reasons of why I encouraged using some of the mentioned CPAN alternatives, if possible, to navigate nested structures.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226879]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-03-28 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found