Categories
Posts in this category
- Introduction
- Strings, Arrays, Hashes;
- Types
- Basic Control Structures
- Subroutines and Signatures
- Objects and Classes
- Contexts
- Regexes (also called "rules")
- Junctions
- Comparing and Matching
- Containers and Values
- Where we are now - an update
- Changes to Perl 5 Operators
- Laziness
- Custom Operators
- The MAIN sub
- Twigils
- Enums
- Unicode
- Scoping
- Regexes strike back
- A grammar for (pseudo) XML
- Subset Types
- The State of the implementations
- Quoting and Parsing
- The Reduction Meta Operator
- The Cross Meta Operator
- Exceptions and control exceptions
- Common Perl 6 data processing idioms
- Currying
Fri, 17 Oct 2008
Changes to Perl 5 Operators
Permanent link
NAME
"Perl 5 to 6" Lesson 11 - Changes to Perl 5 Operators
LAST UPDATED
2015-02-26
SYNOPSIS
# bitwise operators
5 +| 3; # 7
5 +^ 3; # 6
5 +& 3; # 1
"b" ~| "d"; # 'f'
# string concatenation
'a' ~ 'b'; # 'ab'
# file tests
if '/etc/passwd'.path ~~ :e { say "exists" }
# repetition
'a' x 3; # 'aaa'
'a' xx 3; # 'a', 'a', 'a'
# ternary, conditional op
my ($a, $b) = 2, 2;
say $a == $b ?? 2 * $a !! $b - $a;
# chained comparisons
my $angle = 1.41;
if 0 <= $angle < 2 * pi { ... }
DESCRIPTION
All the numeric operators (+, -, /, *, **, %) remain unchanged.
Since |, ^ and & now construct junctions, the bitwise operators have a changed syntax. They now contain a context prefix, so for example +| is bit wise OR with numeric context, and ~^ is one's complement on a string. Bit shift operators changed in the same way, ie +< and +>.
String concatenation is now ~, the dot . is used for method calls.
File tests are now done by smart matching a path object against a simple Pair; Perl 5 -e would now be $_.path ~~ :e.
The repetition operator x is now split into two operators: x replicates strings, xx lists.
The ternary operator, formerly $condition ? $true : $false, is now spelled $condition ?? $true !! $false.
Comparison operators can now be chained, so you can write $a < $b < $c and it does what you mean.
MOTIVATION
Many changes to the operators aim at a better Huffman coding, ie give often used things short names (like . for method calls) and seldom used operators a longer name (like ~& for string bit-wise AND).
The chaining comparison operators are another step towards making the language more natural, and allowing things that are commonly used in mathematical notation.
SEE ALSO
"language/operators" in doc.perl6.org
"language/5to6#Operators" in doc.perl6.org
http://design.perl6.org/S03.html#Changes_to_Perl_5_operators