Why does Autovivification occur with keys() and not %{..}?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 03:56:41

Note that keys can indeed be an lvalue (setting the hash's expected number of elements).

But even if keys itself isn't used in an lvalue context, it has a side effect of resetting a hash's iterator.

So it does modify the hash and so gives the dereference an lvalue context, which makes it autovivify.

After some research and asking around, I found that this has to do with the fact that $d->{cd} was passed to a subroutine, not the fact that it was keys. For instance,

% perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; sub foo {}; my @e = foo(%{$d->{cd}});'

This will also autovivify; this is because internally perl needs to be able to set an alias to function arguments.

Inside foo(), we have an alias set $_[0] = $d->{cd} But that means $d->{cd} needs to be lvalue-able, since subroutines in perl assume you can do something like $_[0] = "123"; so the autovivification needs to happen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!