19 ноября 2013 г., 14:28 пользователь Denis Ibaev <[email protected]>написал:
> >> Почему разыменование переменной со значением undef в хеш не вызывает > >> ошибки с случае keys() и values()? > > > > Видимо, keys и пр. автовивифицируют $v: > > > > perl -Mstrict -E 'my $v; say "keys: ", keys %$v; say "v: ", $v' > > keys: > > v: HASH(0x96bde8) > > Но это нигде не описано, похоже. > > Согласен - даже наоборот, похоже на баг ибо (из http://perldoc.perl.org/perl5140delta.html): · When strict "refs" mode is off, "%{...}" in rvalue context returns "undef" if its argument is undefined. An optimisation introduced in Perl 5.12.0 to make "keys %{...}" faster when used as a boolean did not take this into account, causing "keys %{+undef}" (and "keys %$foo" when $foo is undefined) to be an error, ___***which it should be so in strict mode***___ only [perl #81750]. И ещё нашёл в perlfunc: undef $ref; if (exists $ref->{"Some key"}) { } print $ref; # prints HASH(0x80d3d5c) This surprising autovivification in what does not at first--or even second--glance appear to be an lvalue context may be fixed in a future release. Как правильно заметил Андрей Фёдоров, нижеследующий текст не об этом случае: > > Starting with Perl 5.14, keys can take a scalar EXPR, which must > contain a reference to an unblessed hash or array. The argument will > be dereferenced automatically. This aspect of keys is considered > highly experimental. The exact behaviour may change in a future > version of Perl. > > -- > Денис Ибаев > -- > Moscow.pm mailing list > [email protected] | http://moscow.pm.org > -- Best regards, Ilya Chesnokov
-- Moscow.pm mailing list [email protected] | http://moscow.pm.org
