in reply to Re^4: Convert undef to empty string in a hash
in thread Convert undef to empty string in a hash
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Convert undef to empty string in a hash
by Anonymous Monk on Jan 16, 2015 at 10:22 UTC | |
by eyepopslikeamosquito (Archbishop) on Jan 16, 2015 at 10:52 UTC | |
by Anonymous Monk on Jan 16, 2015 at 11:23 UTC | |
by eyepopslikeamosquito (Archbishop) on Jan 16, 2015 at 12:12 UTC | |
by Anonymous Monk on Jan 16, 2015 at 12:17 UTC | |
by LanX (Saint) on Jan 16, 2015 at 13:49 UTC |