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
Autovivification strangeness with returning undef from a function #13896
Comments
From @demerphqWe at $work have been scratching our head about some autovivification Prior to this I *thought* I had understood chapter and verse of the $ perl -wle'use strict; sub thing { return }; my $subthing= thing()->{foo};' I contend it should do the same as this: $ perl -wle'use strict; sub thing { return }; my $tmp= thing(); my Or this: $ perl -wle'use strict; my $subthing= (my $x)->{key};' And that the following two also should do the same thing: $ perl -wle'use strict; my $subthing= (undef)->{key};' $ perl -wle'use strict; my $subthing= (my $x= undef)->{key};' IMO all of them should autovivify, and/or be more consistent. Even if my $ perl -wle'use strict; my $subthing= (my $x= undef)->{key};' $ perl -wle'use strict; my $subthing= (my $x)->{key};' Should definitely do the same thing (whatever it is). This affects all perls we have tested on going back to 5.14. $ ./perl -v This is perl 5, version 21, subversion 1 (v5.21.1 (v5.21.0-262-g728396c*)) Copyright 1987-2014, Larry Wall Perl may be copied only under the terms of either the Artistic License or Complete documentation for Perl, including FAQ lists, should be found on -- |
From @ikegamiOn Mon, Jun 2, 2014 at 10:58 AM, yves orton <perlbug-followup@perl.org>
thing()->{foo} tries to autovivify an rvalue. It should throw an error. Two surprises for me. 1. $ perl -wle'use strict; (my $x)->{key} = "abc"; print "ok"' $ perl -wle'use strict; (my $x = undef)->{key} = "abc"; print "ok"' $ perl -wle'use strict; (my $x = undef) = "abc"; print "ok"' 2. $ perl -wle'use strict; sub f :lvalue { my $x } f() = "abc"; print "ok"' $ perl -wle'use strict; sub f :lvalue { my $x } f()->{x} = "abc"; print
|
The RT System itself - Status changed from 'new' to 'open' |
From @cpansproutOn Mon Jun 02 09:16:12 2014, ikegami@adaelis.com wrote:
Fixing this is not straightforward. It is the ‘my $x’ itself that does the autovivification if it has been flagged to do so at compile time. We can’t just flag it here, because the scalar assignment (= undef) executes after it, so any vivified value would be stomped on forthwith. I have often wondered whether it would make things simpler if rv2hv (implied by ->{...}) could do the vivification. But I can’t see how easily to get from point A to point B, since the ‘$foo->{bar}’ in $foo->{bar}{baz} returns PL_sv_undef when it does not vivify and the element does not exist. rv2hv can’t just write a hash reference to PL_sv_undef. (There is a little bit of handwaving in the preceding paragraph. I haven’t checked my facts lately, but I think that is the basic reason why vivification happens where it does.) Maybe splitting the vivification behaviour ($foo->{bar} should vivify the element and the later ->{baz} should assign a hash reference to it) is the solution, but I fear it will be a can of worms. (Not that that has deterred me in the past, considering COW and op slabs. :-) Maybe doing vivification in two places is the solution. But would that slow things down through extra checks? (Though how expensive is an extra SvOK?)
I thought I had that one working. In fact, I do: $ perl5.14.4 -wle'use strict; sub f :lvalue { my $x } f()->{x} = "abc"; print "ok"' -- Father Chrysostomos |
Migrated from rt.perl.org#122017 (status was 'open')
Searchable as RT122017$
The text was updated successfully, but these errors were encountered: