I had a little problem while working on a site for a client. The structure was defined using a field that contained the item's parents seperated by colons. For example, "A:B:C", meant that the root was A which had a child B which had a child C (which is the parent of the item).
After starting doing something far too complicated I came up with an easier way. Why not use eval? You end up with something a simple as:
my $hash = join '', map "{$_}", split
/:/, $path;
eval "\$tree->$hash ||= 1";
This relies on the Perl's autovivification. Remember, Data::Dumper is your friend.
remarkably similar (Score:2)
Don't do this with eval! (Score:2)
Eval means you're firing up the compiler. Fun fun. Slow slow.
Eval also means that you have to be very careful about the data. It'd be trivial to launch arbitrary code by selecting the proper data here.
There's really no need. You can run a lot faster and a lot safer with:
(This may not be precisely the code, but this seemed t
Re:Don't do this with eval! (Score:1)
Something like that lets you end with a HASHref instead of the SCALARref. Unfortunately, you can't create a reference to a hash element's value part.
But adding something like a _value key solves that:
Tied hash (Score:1)
Re:Tied hash (Score:1)
$href = $hash{"A:B"}; $value = $href->{"C:D"};(Note: the way it is now, only the top level hash is tied).