[Moscow.pm] Странности и неожиданности Perl

Andrew Shitov andy на shitov.ru
Вс Мар 2 00:06:38 PST 2008


Несколько неожиданно про контексты:

perl510 -E"say 1 if qw(3 4) ~~ 3"           ничего не выводит

perl510 -E"@a = qw(3 4); say 1 if @a ~~ 3"  печатает единицу

Соответственно, qw(3 4) ~~ 2 (длина массива) -- тоже false,
а qw(3 4) ~~ 4 -- true.


Еще неожиданнее, если вместо qw(3 4) написать (3, 4):

perl510 -E"say 1 if (3, 4) ~~ 2"      false
perl510 -E"say 1 if (3, 4) ~~ 3"      false
perl510 -E"say 1 if (3, 4) ~~ 4"      true

perl510 -E"say 1 if [3, 4] ~~ 2"      false
perl510 -E"say 1 if [3, 4] ~~ 3"      true
perl510 -E"say 1 if [3, 4] ~~ 4"      true


Неожиданно, но верно :-)

Binary "," is the comma operator. In scalar context it evaluates
its left argument, throws that value away, then evaluates its
right argument and returns that value. This is just like C's
comma operator.

In list context, it's just the list argument separator, and
inserts both its arguments into the list. These arguments are
also evaluated from left to right.


--
Андрей Шитов
______________________________________________________________________
andy на shitov.ru | http://www.shitov.ru



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