Sorry, I was too brief. What I meant was in the absence of assigning value to the key _val: (note the change with comments)
use strict; use warnings; use Data::Dumper qw( Dumper ); sub samtregar { my $p = shift; my $val = pop; for my $item (@_) { $p->{$item} ||= {}; $p = $p->{$item}; } ### $p->{_val} = $val; } sub repellent { my $p = \shift; my $val = pop; $p = \$$p->{$_} for @_; ### $$p->{_val} = $val; } { samtregar(my $samtregar={}, qw( a b c d ), 'foo'); repellent(my $repellent, qw( a b c d ), 'foo'); { local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; print("samtregar: ", Dumper($samtregar), "\n"); print("repellent: ", Dumper($repellent), "\n"); } } __END__ samtregar: {"a" => {"b" => {"c" => {"d" => {}}}}} repellent: {"a" => {"b" => {"c" => {"d" => undef}}}}

while the nested hashes are being built, it doesn't leave an empty {} around. This behavior could be desirable.

I didn't mean to say that mine doesn't vivify. It's almost the same, just subtly different as shown above.

Hmmm ... as for the fetchers, shouldn't it be more consistent with:
use strict; use warnings; use Data::Dumper qw( Dumper ); sub samtregar_fetcher { my $p = shift; for my $item (@_) { ### last if !$p->{$item}; $p->{$item} ||= {}; $p = $p->{$item}; } return $p->{_val} } sub repellent_fetcher { my $p = \shift; ### my $val = pop; $p = \$$p->{$_} for @_; return $$p->{_val}; } { samtregar_fetcher(my $samtregar={}, qw( a b c d )); repellent_fetcher(my $repellent, qw( a b c d )); { local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; print("samtregar: ", Dumper($samtregar), "\n"); print("repellent: ", Dumper($repellent), "\n"); } } __END__ samtregar: {"a" => {"b" => {"c" => {"d" => {}}}}} repellent: {"a" => {"b" => {"c" => {"d" => {}}}}}


In reply to Re^4: Building an arbitrary-depth, multi-level hash by repellent
in thread Building an arbitrary-depth, multi-level hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.