I understand that there are only two options
In core Perl, yes (Update: actually, autovivification isn't even in the core). But choroba showed one possibility, and writing a single-purpose routine isn't difficult either (I've admittedly compacted it a bit):
use warnings;
use strict;
sub dive { my $r = shift;
$r = ref $r eq 'HASH' && exists $$r{$_} ? $$r{$_} : return for @_;
return $r }
use Test::More;
my $h = { a => { b => { c => { d => 'e' } } } };
is dive($h, qw/ a /), $h->{a};
is dive($h, qw/ a b /), $h->{a}{b};
is dive($h, qw/ a b c /), $h->{a}{b}{c};
is dive($h, qw/ a b c d /), 'e';
is dive($h, qw/ a b c d e /), undef;
is dive($h, qw/ a x /), undef;
is dive($h, qw/ a b x /), undef;
is dive($h, qw/ a x y /), undef;
is dive($h, qw/ a b x y /), undef;
is dive($h, qw/ x /), undef;
is dive($h, qw/ x y /), undef;
is dive($h, qw/ x y z /), undef;
is_deeply $h, { a => { b => { c => { d => 'e' } } } };
done_testing;
Update: Both of the links in your post appear to be broken. I assume you were trying to link to perl5280delta and RT#127712.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.