5 ms·
> and still wear the scars from a particularly tough hashref autovivification bug As an infrequent perl programmer[1], I'm curious about the bug. Googling tel
by eieio 13y ago
> and still wear the scars from a particularly tough hashref autovivification bug
As an infrequent perl programmer[1], I'm curious about the bug. Googling tells me that perl if($foo->{bar}) will always pass, which is pretty goofy to me. Was it something like that?
[1] Well really, a bad perl programmer. I've occasional had to make minor edits to legacy perl. I don't write perl. And, as the mistake in this post shows, that's probably a good thing :)
- seeken 13y agoThat will not always pass. if (exists $foo->{bar}){ .. will not run .. } if ($foo->{bar}) {...} if (exists $foo->{bar}){ .. will run .. }
- eieio 13y agoI see, thank you. I am certainly a perl novice.
- pflanze 13y agoThat doesn't do any autovivification. You meant (note the referencing down a deeper level than the exists test): if (exists $foo->{bar}){ .. will not run .. } if ($foo->{bar}->{baz}) {...} if (exists $foo->{bar}){ .. will run .. }
- seeken 13y agoCan't believe I've had that wrong for so long. Must have misinterpreted it right when I learned perl and have always been careful about it.
- mmaunder 13y agoYes, you auto create variables in a hash when you reference then, so autovivification is just a fancy word meaning automatically declaring and setting a null value. So $myvar->{speling} would create that in a hashref silently. Creates horrible hard to find bugs.
- duskwuff 13y agoProbably worth noting that there is a Perl module which fixes this wart! no autovivification; http://search.cpan.org/~vpit/autovivification/lib/autovivification.pm http://search.cpan.org/~vpit/autovivification/lib/autovivifi... One of the things that's always impressed me the most about Perl is the degree to which modules can play with the language. Modules can add control structures (Try::Tiny), introduce new metaprogramming syntax (Moose), add operator overloading (overload)... it's very flexible.
- einhverfr 13y ago> One of the things that's always impressed me the most about Perl is the degree to which modules can play with the language. That's exactly the point of the article, btw. > Modules can add control structures (Try::Tiny), introduce new metaprogramming syntax (Moose), add operator overloading (overload)... it's very flexible. Technically they add functions, and the language allows you to use imported functions as control structures. This isn't so much to correct you as to clarify it for less familiar readers.
- kbenson 13y agoNo, 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. Edit: Typo and code block formatting
- __david__ 13y agoI 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.