I'm not certain that it is a best practice, but I find that Data::Diver and eval() help out a lot when dealing with hashes and arrays.

#!/usr/bin/env perl use Data::Dumper qw| Dumper |; use Data::Diver qw| Dive DiveVal DiveRef DiveDie DiveError DiveClear | +; my $h = { key => 'value', hoh => { key => 'value' }, hoa => [ 'a', 'b', 'c' ], deep => { values => { are => 'supported' } }, }; Dive($h, 'key') && warn "'key' is there"; !Dive($h, 'nokey') && warn "'nokey' is not there"; warn "notice 'nokey' was not created"; warn Dumper $h; warn "Values are: " . join(', ', eval {@{Dive($h, 'hoa')}}); warn "No errors for non arrays: " . join(', ', eval {@{Dive($h, 'empty +')}}); warn "Deep values: ". Dive($h, qw| deep values are |); !Dive($h, qw| deep values that is not there |) && warn "Missing Deep v +alues are safe"; __END__ 'key' is there at ./test.pl line 12. 'nokey' is not there at ./test.pl line 13. notice 'nokey' was not created at ./test.pl line 14. $VAR1 = { 'deep' => { 'values' => { 'are' => 'supported' } }, 'hoa' => [ 'a', 'b', 'c' ], 'hoh' => { 'key' => 'value' }, 'key' => 'value' }; Values are: a, b, c at ./test.pl line 17. No errors for non arrays: at ./test.pl line 18. Deep values: supported at ./test.pl line 19. Missing Deep values are safe at ./test.pl line 20.

Some of those are still not beautiful ( especially array dereference ), but I find it a clean way to deal with hashes, as it already implements the autovivification and error handling logic.

-- AlexLC

In reply to Re: Best Hash Practices? by alexlc
in thread Best Hash Practices? by DamianKaelGreen

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.