One question, hv: The // operator you use here: $config->{$field} //= {}; ...

It's a fair question, I glossed over some stuff there.

The difference between || and // lies in what they are equivalent to: $a ||= $b is equivalent to $a = b unless $a, while $a //= $b is equivalent to $a = $b unless defined $a. In both cases the equivalence is exact, except that the expression for $a is evaluated only once.

So that particular line says "initialize this value to an empty hashref if there is no defined value already there". But in essence, the line was actually a lazy shorthand for "insert your preferred error-handling behaviour here".

The assumption is a) that there will never be a mismatch - you'll never have a string stored at $config{address} before trying to read or write $config{address}{virtual} - and b) that if for some reason a mismatch occurs, a fatal error is a good enough way to signal it.

A tighter (and clearer) version might say something like:

use Scalar::Util qw{ reftype }; $config->{$field} //= {}; die "invalid field '$field' at '$name'" unless reftype($config->{$fiel +d}) eq 'HASH';

A looser, "just get it done" sort of approach might simply overwrite it:

$config->{$field} = {} unless reftype($config->{$field}) eq 'HASH';

Using $config->{$field} ||= {} would instead give you mixed behaviour - it will silently overwrite any value that's false in perl, like (0, 0.00, ""), but leave others to cause an error. For something like configuration values, it seemed unlikely to me that you would want to have it endemically treat those values differently from others.

As AnomalousMonk says, this "defined-or" or "defaulting" operator // has been available since v5.10; since then, I find I use it far more often than I use ||.


In reply to Re^3: Dynamic addressing in a hash by hv
in thread Dynamic addressing in a hash by rpaskudniak

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.