|
Perl 6
Perl 6 Operator Tablet: Revision 32
"Overview"[Perl 6 Tablets] - _Chapter:_ "0:History"[Perl 6 History Tablet], "1:Design"[Perl 6 Language Design Tablet], "2:Basics"[Perl 6 Basics Tablet], "3:Var"[Perl 6 Variable Tablet], *"4:Op"[Perl 6 Operator Tablet]*, "5:IO"[Perl 6 IO Tablet], "6:{}"[Perl 6 Block Tablet], "7:Sub"[Perl 6 Subroutine Tablet], "8:OOP"[Perl 6 OOP Tablet], "9:Rx"[Perl 6 Regex Tablet], "10:Meta"[Perl 6 Meta Tablet]
"Intro"[Perl 6 Intro Tablet] - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], "C:Cook"[Perl 6 Cookbook Tablet], "D:Delta"[Perl 6 Delta Tablet], "E:Best of"[Best Of Perl 6 Tablet], "F:Links"[Perl 6 Links Tablet] ---- {toc: } _Perl folklore: Perl is an operator based language._ Perl 6 has a huge amount of operators, because they support 2 of the main design goals: they offer *dense* and *readable* code. _{{2 + 3}}_ is certainly shorter and easier to understand then _{{add(2,3)}}_, since "pictograms"{link: [perl 6 language design tablet] visual metaphor} can be picked up faster than words. (Fortran made his whole carrier on that). But because they are so many, they had to be sorted by a rule named "huffman coding"{link: [perl 6 language design tablet] huffman coding}, which was applied here more than in any other part of the syntax. To understand an operator you have to know his *arity* (how many parameters he takes - usually one (!) or two (+) ). The *"precedence"{link: [perl 6 lookup tablet]table of precedence}* tells which operator to prefer in case of conflict, when no braces are used (round braces are only used for grouping and managing precedence). It allows _{{2 + 3 * 5}}_ to return 17, not 25, which would upset your math teacher. Behind that link is a table which also tells you also the *"associativity"{link: [perl 6 lookup tablet]Operator Associativity}* of every operator. This tells you after which rule to resolve precedence if one operator is used several times like in _{{2 * 3 * 7}}_. ^ Comparison ^^ Smartmatch This is the most mighty (much more mighty than its backported Perl 5 twin) of all Perl 6 operators. It can be called the {{"compare-this-with-that-operator"}}. If the left side of that infix op matches somehow the content of the right side, it returns Bool::True, otherwise Bool::False. The negated form *!~* naturally works the other way around. The exact comparison operation depends on the "data types"{link: [perl 6 variable tablet] data types} of the values on both sides. Just look into that "large table"{link: [perl 6 lookup tablet] smartmatch} to check your specific case. Smartmatching was originally invented to make "matching with regex"{link: [perl 6 regex tablet]Matching} semantically sane. > ~~ !~ ^^ Equality > eqv eq == === > != !== ^^ Traversing Sequence > ++ -- succ pred "sequence generation"{link: Sequence Operator} ^^ Generic Comparison > before after cmp ^^ Numerical Comparison > < == > <=> <= >= ^^ String Comparison > lt eq gt leg le ge ^^ joined comparison > 3 < $a == $a < 7 is not the same as > 3 < $a < 7 because latter is evaled at once and the first in 2 steps (left to right) ^^ Junctions > | & ^ ! > any all one none ^^ Ranges > .. ^ ^^ logical selection > && - and > || - or > // - err > ^^ - xor see also "numerical selection"{link: numerical selection} ^^ ternary > ?? !! ^^ flipflop > ff fff ^^ file test "table"{link: [Perl 6 Lookup Tablet]Filetest Ops} ^^ yadda > ... > ??? > !!! ^ context forcing scalar ops ^^ bool context > ? ! > ?& ?| ?^ ^^ numeric context > + - * ** / % %% > +& +| +^ +< +> > mod exp sqrt sin cos tan log log10 ^^^ numerical selection > min max minmax ^^ String context > ~ x > ~& ~| ~^ ~< ~> ^ Ops for arrays/lists ^^ List generation The simplest way to create a list is by repeating some values: > 'munch' xx 3 # results in 'munch', 'munch', 'munch' > ('hallo', 'echo') xx 2 --> 'hallo', 'echo', 'hallo', 'echo' In list context the range operator produces lists: > @ 2 .. 7 --> 2,3,4,5,6,7 ^^^ Sequence Operator > ... "traversing sequence"{link: Traversing Sequence} ^^ Zip > Z ^^ Combinators > X ^^ Hyperops > << >> ^^ Reduce > [ ] ^^ Triangle > {{[\ ]}} ^^ Feed Ops > <== ==> > <<== ==>> ^ Assignment ops ^^ self assigning ops ^ precedence table "is in Appendix B"{link: [Perl 6 Lookup Tablet]Table of precedence} ^ intentionally not existing ops ^ making own operators ---- "Overview"[Perl 6 Tablets] - _Chapter:_ "0:History"[Perl 6 History Tablet], "1:Design"[Perl 6 Language Design Tablet], "2:Basics"[Perl 6 Basics Tablet], "3:Var"[Perl 6 Variable Tablet], *"4:Op"[Perl 6 Operator Tablet]*, "5:IO"[Perl 6 IO Tablet], "6:{}"[Perl 6 Block Tablet], "7:Sub"[Perl 6 Subroutine Tablet], "8:OOP"[Perl 6 OOP Tablet], "9:Rx"[Perl 6 Regex Tablet], "10:Meta"[Perl 6 Meta Tablet] "Intro"[Perl 6 Intro Tablet] - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], "C:Cook"[Perl 6 Cookbook Tablet], "D:Delta"[Perl 6 Delta Tablet], "E:Best of"[Best Of Perl 6 Tablet], "F:Links"[Perl 6 Links Tablet] ---- |