[Moscow.pm] keys() и разыменование undef

Ilya Chesnokov chesnokov.ilya на gmail.com
Вт Ноя 19 02:35:25 PST 2013


19 ноября 2013 г., 14:28 пользователь Denis Ibaev <dionys на gmail.com>написал:

> >> Почему разыменование переменной со значением 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
> moscow-pm на pm.org | http://moscow.pm.org
>



-- 
Best regards,
Ilya Chesnokov
----------- следущая часть -----------
Вложение в формате HTML было извлечено…
URL: <http://mail.pm.org/pipermail/moscow-pm/attachments/20131119/9cb22e7a/attachment.html>


Подробная информация о списке рассылки Moscow-pm