Skip to content

Commit 3b865ae

Browse files
author
Tadeusz Sośnierz
committed
some additions to smartmatching
1 parent afd5fc5 commit 3b865ae

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

misc/perl6advent-2010/articles/smartmatching.pod

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ C<m/.../> and C<.method_call>.
4141
Here is what the smart match operator does with some matchers on the
4242
right-hand side:
4343

44-
matcher operation
45-
--------------------------
46-
type object type check
47-
Numeric numeric equality
48-
Str string equality
49-
Regex $_ matches regex?
50-
Range $_ is inside the range
51-
Code closure is called with $_ as argument
44+
# is it of type Str?
45+
$foo ~~ Str
46+
# is it equal to 6?
47+
$foo ~~ 6
48+
# or is it "bar"?
49+
$foo ~~ "bar"
50+
# does it match some pattern?
51+
$foo ~~ / \w+ '-' \d+ /
52+
# Is it between 15 and 25?
53+
$foo ~~ (15..25)
54+
# call a closure
55+
$foo ~~ -> $x { say 'ok' if 5 < $x < 25 }
56+
# is it an array of 6 elems in which every odd element is 1?
57+
$foo ~~ [1, *, 1, *, 1, *]
58+
59+
Notice how, unlikely in Perl 5, in Perl 6 smartmathing is the only way
60+
to match a regex, it has no special operator.
61+
62+
TODO: Show how 'something foobar asd' ~~ /.oo.a./ does not return
63+
Bool::True and why

0 commit comments

Comments
 (0)