So yeah, this is shorter $_ //= '' for values %foo; than $foo{$_} //= '' for keys %foo;, and sure, you can say its more DRY, but DRY doesn't care about it, its the same one line talking about the same one variable %foo, nothing that can get out of whack because of any duplicationActually, it can easily get out of whack:
Now the maintenance programmer must ask: is this really the intent ... or is it a typo?$foob{$_} //= '' for keys %foo;
Using the name once only makes the intent clearer, that is:
makes it clear, at a glance, that the intent is to update a single hash, while:$_ //= '' for values %number_of_immortal_perl_monks
gives the maintenance programmer a headache.$number_of_immoral_perl_monks{$_} //= '' for keys %number_of_immortal_ +perl_monks
Finally, with:
to rename the foo hash to a better name you must change it in two places, rather than one, so there is (an admittedly small) chance of error when you are doing search-and-replace in your editor (code refactoring IDEs help here).$foo{$_} //= '' for keys %foo;
In reply to Re^5: Convert undef to empty string in a hash
by eyepopslikeamosquito
in thread Convert undef to empty string in a hash
by ibm1620
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |