-
Notifications
You must be signed in to change notification settings - Fork 577
Defined-or operator assigns key in hash too early #14023
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
Comments
From [email protected]If I use the defined-or operator in the manner shown in the example, the value 1 gets assigned to $hello{key}, instead of the expected value 0: $ perl -E 'my %hello; $hello{key} //= keys %hello; say $hello{key}' This is especially surprising, considering that the equivalent using the ternary operator does not behave this way, but behaves as expected: $ perl -E 'my %hello; $hello{key} = defined($hello{key}) ? $hello{key} : keys %hello; say $hello{key}' Flags: Site configuration information for perl 5.20.0: Configured by davidl at Thu Aug 7 09:55:02 BST 2014. Summary of my perl5 (revision 5 version 20 subversion 0) configuration: @INC for perl 5.20.0: Environment for perl 5.20.0: |
From @cpansproutOn Tue Aug 12 08:56:11 2014, Flimm wrote:
I’m not sure this is a bug. The equivalent using the ternary operator is not absolutely equivalent, because it evaluates the $hello{key} twice, once in rvalue context (not vivifying the element), and once in lvalue context (vivifying). //= only evalutes its lhs once, and in lvalue context at that, so it vivifies. I don’t see any other way this could work. -- Father Chrysostomos |
The RT System itself - Status changed from 'new' to 'open' |
From [email protected]
It could work the way that I expect it :) Simply evaluating $hello{key} should not cause the key "key" to exist in the hash %hello, that's not how autovivifaction is documented to work, or works in practise: $ perl -E 'my %hello; say $hello{key}; say scalar(keys %hello);' 0 Autovivification should only create new references to hashes or arrays, according to perlref, which is clearly not what is created here, $hello{key} is never a reference. I don't see any documentation that says that autovivifaction works differently when it's the left-hand-side of a defined-or operator. It certainly doesn't behave that way when it's the left-hand side of the assignment operator: $ perl -E 'my %hello; $hello{key} = keys %hello; say $hello{key}' |
From @ap* David D. Lowe via RT <perlbug-followup@perl.org> [2014-08-13 11:35]:
What relevance does your example have here? There is no autovivification
Obviously. That doesn’t need to evaluate the LHS to know whether to $ perl -E 'my %hello; $_ = keys %hello for $hello{key}; say $hello{key}' And while it might seem obvious that $hello{key} //= EXPR should behave $hash{ pop @entry } //= EXPR where you can’t just evaluate the LHS expression twice and expect it to And because you *might* need to store a value, and you need an lvalue The only way around this fact would be a deep change to the way that The way it works now is really the only way it can sanely work. That way may not be satisfying for some simple “obvious” cases where it Regards, |
From @epaThis is consistent with some other assignment-modification operators: % perl -E 'my %hello; $hello{key} ||= keys %hello; say $hello{key}' It is not specific to defined-or. |
From @jkeenanOn Wed Aug 13 03:26:43 2014, aristotle wrote:
Reviewing this discussion, my sense is that we're saying this is not a bug. If that's correct, I'd like to close the ticket. Any disagreement with that? Thank you very much. -- |
From @ikegamiTwo observations: 1. The operand evaluation order for C<< $x //= $y >> is not documented, so 2. A hash element as a parameter in a sub call is replaced with a magical
|
From [email protected]On Mon, Sep 15, 2014 at 5:26 AM, Eric Brine <ikegami@adaelis.com> wrote:
Doesn't the documentation imply it, though? The defined-or operator is documented to be short-circuiting, so the LHO And the assignment operators are documented to be "without duplicating How could that possibly work in any other order? Eirik |
From @ikegamiOn Mon, Sep 15, 2014 at 6:43 AM, Eirik Berg Hanssen <
The second observation I posted shows how it's not only possible, but it's |
From [email protected]On Mon, Sep 15, 2014 at 1:59 PM, Eric Brine <ikegami@adaelis.com> wrote:
It shows how one could get around the problem, but it doesn't change the Well, okay, I guess it delays the hash lookup when the element does not my %j; :-P Okay, I yield. :) Eirik |
@cpansprout - Status changed from 'open' to 'rejected' |
Migrated from rt.perl.org#122512 (status was 'rejected')
Searchable as RT122512$
The text was updated successfully, but these errors were encountered: