Перейти к основному содержанию
Тема: [Perl6] Smart matching / Умное сравнение (Прочитано 1906 раз) предыдущая тема - следующая тема

[Perl6] Smart matching / Умное сравнение

https://perl6advent.wordpress.com/2010/12/12/day-12-%E2%80%93-smart-matching/
http://design.perl6.org/S03.html#Smart_matching
https://docs.perl6.org/language/operators#infix_~~

Оператор ~~
(похож на червяка)
В переводе: Умное сравнение

Примеры в студию!
    # is it of type Str?
    $foo ~~ Str
    # is it equal to 6?
    $foo ~~ 6
    # or is it "bar"?
    $foo ~~ "bar"
    # does it match some pattern?
    $foo ~~ / \w+ '-' \d+ /
    # Is it between 15 and 25?
    $foo ~~ (15..25)
    # call a closure
    $foo ~~ -> $x { say 'ok' if 5 < $x < 25 }
    # is it an array of 6 elems in which every odd element is 1?
    $foo ~~ [1, *, 1, *, 1, *]