New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hash dereference of undefined value #14271
Comments
From perl-bug@ton.iguana.beCreated by perl-bug@ton.iguana.beperl -wle 'use strict; print if %$a; print $a' perl -wle 'use strict; print if keys %$a; print $a' Until I stumbled into this I thought that both ways of testing if a HASH has elements were equivalent. Perl Info
|
From @jkeenanOn Mon Nov 24 05:22:38 2014, thospel wrote:
I don't think this is a bug in Perl, nor do I think that it should be approved as a feature request. In the first place, your example is somewhat confusing in that '$a' (along with '$b') is a special variable in Perl's sort function. If instead of '$a' you had used, say, '$c', you would have gotten this: ##### Since strictures are not the real problem here, let's (for once) drop the 'use strict'. Now we get: ##### But all this says is that at the conclusion of this statement a hash exists at that location in memory. It does *not* tell us if that hash has any elements in it or not. The hash has been autovivified by using the 'keys' function, but it doesn't have any elements. We can see that with this: ##### No elements in the hash. If your objective is to determine whether a hash has elements or not, you can simply say: ##### # Ensure that hash has at least 1 element: In a boolean context, 'keys %hash' is the way to determine whether a hash has elements or not. In contrast, '%$c' does not cause a hash to be autovivified. The warning you got is an accurate description of what is happening.
-- |
The RT System itself - Status changed from 'new' to 'open' |
From @demerphqOn 25 November 2014 at 04:12, James E Keenan via RT <
I agree. However not for the reasons you explained. I think Ton is getting confused A raw %$hash where $hash is undef will not autovivify: perl -wle'use strict; my $hash; my $k = %$hash' However, when one hash-derefs an undef var in the *arguments* to a normal perl -wle'use strict; sub P {print for @_}; my $hash; P(%$hash); print Which is why keys(%$hash) behaves differently than a simple (%$hash) I am under the impression this is because the arguments to a sub call are perl -wle'use strict; sub P {$_[1]="P";}; my $hash={a=>0}; P(%$hash); print Which if I understand things correctly is why %$hash is treated differently So, this is either not-a-bug, or it is a can't-fix/won't-fix due to wide perl -wle'use strict; my $hash; print %$hash' Anyway, I think one could argue that the inconsistency is a bug somewhere, cheers, |
From zefram@fysh.orgWe can't change default autovivification behaviour for backcompat reasons. -zefram |
@iabyn - Status changed from 'open' to 'rejected' |
Migrated from rt.perl.org#123292 (status was 'rejected')
Searchable as RT123292$
The text was updated successfully, but these errors were encountered: