in reply to Re: Re: A hash slice but not..
in thread A hash slice but not..

Why does mine have to handle cases that his doesn't? It's just another way to do it. Anyway I didn't see his solution until after I posted mine, because I was testing my code and revising my text.

Still, here's a case that mine handles differently from his:

add2hash(\%tree, 1, ('print "Hello world!\n"')); set_nested_value(\%tree, ['print "Hello world!\n"'], 1);
;)

BTW, relying on Perl's autovivify (as in the eval solution) does have the same problems mine has. In the snippet: $href->{a}{b}{c} = 7; If the value of $href->{a} is a string, then Perl will treat it as a symbolic reference and an error will occur under strict refs.

Similarly, if the value of $href->{a}{b}{c} is a reference to another hash, then the above snippet will replace that hash reference with the value 7.

Update: On second thought, just in case someone wants to run the test case, I replaced `rm -rf *` with print "Hello world!\n". :)