G'day Bill,

That's certainly a valid way to go. Consider the following:

$ perl -e ' use strict; use warnings; use Test::More tests => 8; my $h = { b => {} }; my ($f, $f2, $f3, $f4); $f = $h->{b}{c}{d} if exists($h->{b}{c}) and exists($h->{b}{c}{d}) +; is_deeply( $h, { b => {} }, "no autovivification" ); is( $f, undef, "no value assigned" ); $f2 = $h->{b}{c}{d} if exists $h->{b}{c}; is_deeply $h, { b => {} }, "f2: no autovivification"; is $f2, undef, "f2: no value assigned"; $f3 = $h->{b}{c}{d}{x}{y}{z} if exists $h->{b}{c} and exists $h->{b}{c}{d} and exists $h->{b}{c}{d}{x} and exists $h->{b}{c}{d}{x}{y}; is_deeply $h, { b => {} }, "f3: no autovivification"; is $f3, undef, "f3: no value assigned"; $f4 = $h->{b}{c}{d}{x}{y}{z}; delete $h->{b}{c}; is_deeply $h, { b => {} }, "f4: autovivification removed"; is $f4, undef, "f4: no value assigned"; '

Update: The code above is a modification of the original. Something was niggling me about what I first wrote, but I couldn't see the problem. ++hv's response to your post alerted me to the issue. My first, less-than-good effort is in the spoiler below. Note that the output is unchanged.

Output:

1..8 ok 1 - no autovivification ok 2 - no value assigned ok 3 - f2: no autovivification ok 4 - f2: no value assigned ok 5 - f3: no autovivification ok 6 - f3: no value assigned ok 7 - f4: autovivification removed ok 8 - f4: no value assigned

I think each has its merits and probably comes down to best choice on a case by case basis.

— Ken


In reply to Re^3: Arrow Operator Question by kcott
in thread Arrow Operator Question by sectokia

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.