Re:In Praise of Perl 5
by kbenson on Thursday February 27, 2014 @10:44PM
(#7317126)
No, it's a combination of allowing hash refs to be automatically created on access or assignment for complex data structures where it's implied.
# False, key doesn't exist, doesn't print and doesn't create anything at that key location
my $h;
say "foo" if $h->{foo};
# Creates an empty hash ref at $h->{foo}, so $h->{foo} now evaluates to true in boolean context.
$h->{foo}{bar}
# True, prints "foo";
say "foo" if $h->{foo}
# creates empty hash refs at bar and baz.
say "what does this do?" if $h->{foo}{bar}{baz}{quux};
Works with array refs as well.
Personally, I find once you understand it, it's fairly easy to guard against (even if it requires more code. e.g. say "foo" if $h->{foo} and $h->{foo}{bar}; # Assuming we know $h is a hash ref already.
Re:In Praise of Perl 5
by __david__ on Friday February 28, 2014 @12:19AM
(#7317371)
I almost got a patch pushed through to Perl to add a new operator that would help that problem (and the similar problem of nested objects who might be undef). Someday I'll have to dust it off and try again.
Personally, I find once you understand it, it's fairly easy to guard against (even if it requires more code. e.g. say "foo" if $h->{foo} and $h->{foo}{bar}; # Assuming we know $h is a hash ref already.
Edit: Typo and code block formatting