"Perl 6 Operators"
Speaker: Juerd
Duration: 45 minutes
Level: Advanced
Het Nederlandse origineel is hier.
Introduction
- Expressive strength is in operators
- »~&=«
Summary
- Some things change
- A lot is added
- More line noise + more ease
== more Perl
Perl 5 operators
-> ++ -- ** ! ~ \ and u+ u- =~ !~ * / % x + - . << >> < > <= >= lt gt le ge
== != <=> eq ne cmp & | ^ && || .. ... ?: = op= , => not and or xor
Operators that stay the same
! != % && * ** + ++ - -- .. / < <= <=> == > >= \ and and cmp eq ge gt le
lt ne not op= or xor ||
Changing operators
-> . ~ ^ ?: x => & | ^ << >>
New operators
<> «» »« | & ^ := ::= =:= ^^ // ? + ~ true err ¥ : ...
The big shift
- -> becomes .
- . becomes ~
- ~ becomes ^
- ^ becomes +^, ~^ or ?^
More changes
- ?: becomes ??::
- x becomes x and xx
- => will construct pairs
Bitwise ops
| & | | | ^ | < | > |
+ | +& | +| | +^ | +< | +> | numeric
|
---|
~ | ~& | ~| | ~^ | ~< | ~> | stringy
|
---|
? | ?& | ?| | ?^ | | | boolean
|
Linenoise explained
(»~&=«)
$foo ~&= " "
is just
$foo = $foo ~& " "
»« explained later on
Smart matching
- =~ goes away
- ~~ does a smart match
- !~ does too, but negated
- compare anything to anything
Examples with ~~
if $string ~~ $regex { ... }
if @array ~~ $element { ... } # grep
if $string ~~ $substring { ... } # eq
if $class ~~ SuperClass { ... } # isa
Filler
- and has &&
- or has ||
- xor gets ^^
Defined-or
my $foo = $input || 'default';
my $foo = $input // 'default';
err is the low precedence version
Word lists
- < foo bar baz > is the new qw
- « foo $bar baz » interpolates
Pairs
- foo => "bar"
- :foo("bar")
- :foo<bar>
- :foo«bar»
:foo means
:foo(1)
Hash subscripts
- %hash{foo} is %hash{ foo() }
- %hash<foo> is %hash{ 'foo' }
Binding
- $bar := $foo
- $bar ::= $foo
- $foo =:= $bar
(p5: \$foo == \$bar)
Forcing context
- + numeric
- ~ string
- ? boolean, with low prec. true
- (! boolean, with low prec. not)
Zipping
¥ or
Y or
zip()
@foo ¥ @bar
# is actually
@foo[0], @bar[0],
@foo[1], @bar[1],
@foo[2], @bar[2], ...
Hyper
@quux = map { $_ + 3 } @foo
@quux = @foo »+ 3;
@quux = map { @foo[$_] + @bar[$_] }
0 .. min(@foo.last, @bar.last);
@quux = @foo »+« @bar;
Line noise explained
@ips »~&=« @netmasks
Junctions!!!
if $foo == 'foo' | 'bar' { ... }
if $foo & $bar > 3 { ... }
Kinds of junctions
- |, any()
- &, all()
- ^, none()
- one() is any() with a limitation
Ease
if $a != $c
and $a != $d
and $b != $c
and $b != $d { ... }
becomes
if $a & $b == $c ^ $d { ... }
More ease
if $a != $d and $a != $e and $a != $f
and $b != $d and $b != $e and $b != $f
and $c != $d and $c != $e and $c != $f { ... }
becomes
if all($a, $b, $c)
== none($d, $e, $f) { ... }
Real power
if all(@foo) != none(@bar)
{ ... }
Junctions as normal values
my $junc = 11 | 20;
$junc += 5;
# $junc is now 16 | 25
$j2 = sqrt($junc);
# $j2 is now 4 | 5
Not explained
- -> ("pointy sub")
- * ("splat")
- ** ("steam roller")
- <== and ==> ("pipes")
Books
- Perl 6 and Parrot Essentials, 2nd ed.
By Allison Randal, Dan Sugalski, Leopold Tötsch
ISBN: 059600737X
- Perl 6 Now: Core Ideas Illustrated With Perl 5
By Scott Walters
ISBN: 1590593952
The end