Perl 5 to Perl 6 guide - operators
Operators in Perl 5 to Perl 6: equivalencies and variations
DESCRIPTION
A (hopefully) comprehensive list of Perl 5 operators with their Perl 6 equivalents with notes on variations between them where necessary.
NOTE
This document does not explain the operators in detail. This document is an attempt to guide you from the operators in Perl 5's perlop
document to their equivalents in Perl 6. For full documentation on the Perl 6 equivalents, please see the Perl 6 documentation.
Operator precedence and associativity
The operator precedence table is somewhat different in Perl 6 than it is in Perl 5, so it will not be detailed here. If you need to know the precedence and associativity of a given operator in Perl 6, refer to Operator Precedence.
Terms and list operators
The things listed in Perl 5's perlop
document as unary and list operators in this section tend to be things that can also be thought of as functions, such as print
and chdir
. As such, you can find information about them in the functions guide. Parentheses are still used for grouping. There is one caveat: in Perl 6, it's the ,
(comma) that creates lists, not parentheses. So:
my = 1,2,3,4,5; # no parentheses needed.say for 1,2,3,4,5; # also no parenthesesmy = (1); # *not* a list, as there is no commamy = (1,); # a List in a scalar container
The arrow operator
As you typically will not be using references in Perl 6, the arrow is probably less useful as a dereferencing operator. If you do need to dereference something, however, the arrow is the dot. It is also the dot for method calls. So, Perl 5's $arrayref->[7]
becomes $arrayref.[7]
in Perl 6 and, similarly, $user->name
becomes $user.name
. The =>
arrow is used for constructing Pairs, see Pair term documentation.
Auto-increment and auto-decrement
Work as in Perl 5. The one possible caveat is that they function by calling the succ
method for ++
and the pred
method for --
. For builtin numeric types, this is unlikely to do something unusual, but custom types can define their own succ
and pred
methods, so in those cases, you should probably take note of what ++
and --
will actually do.
Exponentiation
Works as you would expect. The caveat in Perl 5's perlop about **
binding more tightly than unary minus (i. e. "-2**4" evaluates as "-(2**4)" rather than "(-2)**4)") is also true for Perl 6.
Symbolic unary operators
As in Perl 5, unary !
and -
do logical and arithmetic negation, respectively. ?^
is used for bitwise logical negation, which the documentation indicates is equivalent to !
. It may be relevant to note that these coerce their arguments to Bool
and Numeric
, respectively.
Unary ~
is the string context operator in Perl 6, so use prefix +^
for bitwise integer negation. Assumes two's complement.
+
does have an effect in Perl 6, coercing its argument to the Numeric type.
Unary \
is no more. If you really want to take a "reference" to an existing named variable, you can use item context, like so: $aref = item(@array)
, or maybe more familiarly by prefixing with a $
: $aref = $@array
. Please note that you're not really getting a reference, but a scalar container with the referenced object in it.
You can get a "reference" to a named subroutine by using the &
sigil: $sref = &foo
. Anonymous arrays, hashes, and subs return the underlying object during creation right away: $sref = sub { }
.
Binding operators
=~
and !~
have been replaced by ~~
and !~~
, respectively. Those of you who consider smartmatching broken in Perl 5 will be happy to hear that it works much better in Perl 6, as the stronger typing means less guesswork. See the smartmatch documentation for a more extensive explanation of how smartmatch works in Perl 6.
Multiplicative operators
Binary *
, /
, and %
do multiplication, division, and modulo, respectively, as in Perl 5.
Binary x
is slightly different in Perl 6, and has a companion. print '-' x 80;
gives you a string of 80 dashes, but for the Perl 5 behavior of @ones = (1) x 80;
giving you a list of 80 "1"s, you would use @ones = 1 xx 80;
.
Additive operators
Binary +
and -
do addition and subtraction, respectively, as you would expect.
As .
is the method call operator, so binary ~
acts as the concatenation operator in Perl 6.
Shift operators
<<
and >>
have been replaced by +<
and +>
.
Named unary operators
As noted above, you'll find these in the functions guide.
Relational operators
These all work as in Perl 5.
Equality operators
==
and !=
both work as in Perl 5.
<=>
and cmp
have different behavior in Perl 6. <=>
does a numeric comparison, but returns Order::Less
, Order::Same
, or Order::More
instead of Perl 5's -1
, 0
, or 1
. To get the Perl 5 behavior (with the change that it returns the Order
objects, rather than integers) of cmp
, you would use the leg
operator.
cmp
does either <=>
or leg
, depending on the existing type of its arguments.
~~
is the smartmatch operator as in Perl 5, but it's also just the match operator in Perl 6, as noted above. For how smartmatching works in Perl 6, see the smartmatch documentation.
Smartmatch operator
See the smartmatch documentation for a more extensive explanation of how smartmatch works in Perl 6.
Bitwise And
Binary &
is +&
in Perl 6.
Bitwise Or and Exclusive Or
Bitwise OR has changed from |
in Perl 5 to +|
in Perl 6. Similarly, bitwise XOR ^
is +^
, except this operates on integers.
C-style Logical And
Unchanged.
C-style Logical Or
Unchanged.
Logical Defined-Or
Remains in Perl 6 as //
. Returns the first defined operand, or else the last operand. Also, there is a low precedence version, called orelse
.
Range operators
In list context, ..
operates as the range operator and should not need to be changed. That said, there are exclusionary range operators that may be useful. These are:
infix
..^
which excludes the endpoint;infix ^.. which excludes the starting point;
infix
^..^
which excludes both the starting and ending points;prefix
^
which starts from zero excluding the endpoint.
The following example shows the effects of all the above range operators (please note parentheses are used only to allow the method call):
(1..^5).list; # (1 2 3 4)(1^..5).list; # (2 3 4 5)(1^..^5).list; # (2 3 4)(^5).list; # (0 1 2 3 4)
In Perl 5, in scalar context, the operators ..
and ...
work as flip-flop operators, even if they are little-known and probably less used. Those operators have been replaced in Perl 6 by ff and fff respectively.
Conditional operator
The conditional operator ? :
has been replaced by ?? !!
:
= ? : ; # Perl 5
= ?? !! ; # Perl 6
Assignment operators
Although not fully documented, S03 indicates that the mathematical and logical assignment operators should work as you would expect. The one noticeable change is that .=
calls a mutating method on the object on the left (which can also be a type-object). This allows for the following useful idiom:
my LongClassName .= new( frobnicate => 42 ); # no need to repeat class name
This ensures that $bar
will only be able to contain a LongClassName
object, as well not having to repeat (and possibly misspell) the class name.
~=
is the string concatenation assignment, as you might expect with the changes in .
and ~
. Also, the bitwise assignment operators are likely not separated into numeric and string versions (&=
, etc., vs. &.=
, etc.), as that feature is currently experimental in Perl 5 itself - although, again, this is not specifically documented.
Comma operator
The comma operator works mostly as expected, but technically it creates Lists) or separates arguments in function calls. Also, there is a :
variant that turns function calls into method calls - see this page.
The =>
operator, or fat arrow, works similarly to the Perl 5 "fat comma" in that it allows an unquoted (ordinary) identifier on its left side, but in Perl 6 constructs Pair objects, rather than just functioning as a separator. If you are trying to just literally translate a line of Perl 5 code to Perl 6, it should behave as expected.
List operators (rightward)
Like the Named Unary Operators, you'll find these discussed under Functions.
Logical Not
The lower precedence version of !
. As with !
, coerces its argument to Bool
.
Logical And
Lower precedence version of &&
as in Perl 5.
Logical or and Exclusive Or
or
is the low precedence version of ||
, and xor
is the low precedence version of ^^
.
Additionally, there is a low precedence version of //
, called orelse
.
Quote and quote-like operators
For all the gory details on quoting constructs, see quoting.
There is a quoting operator that allows absolute literal strings: Q
or 「…」
, although the latter might be difficult to find on your keyboard, depending on your keyboard... Backslash escapes do not apply in Q
quoted strings. E. g. Q{This is still a closing curly brace → \}
renders "This is still a closing curly brace → \".
q
does what you expect, allowing backslash escapes. E. g. q{This is not a closing curly brace → \}, but this is → }
returning "This is not a closing curly brace → }, but this is →". As in Perl 5, you can get this behavior with single quotes.
qq
allows interpolation of variables. However, by default, only scalar variables are interpolated. To get other variables to interpolate, you need to put square brackets after them (the so-called zen-slice) to get them to interpolate. E.g. @a = <1 2 3>; say qq/@a[] example@example.com/;
results in "1 2 3 example@example.com". Hashes interpolate in the same manner: %a = 1 => 2, 3 => 4;say "%a{}";
results in a space separating the pairs and tabs separating the key from the value in each pair (because that's the standard stringification of Pair
s, and a hash acts as list of Pair
s when stringified). You can also interpolate Perl 6 code in strings using curly braces. For all the details, see Interpolation.
qw
works as in Perl 5, and can also be rendered as <...>
. E. g. qw/a b c/
is equivalent to <a b c>
.
There is also a version of qw
that interpolates, which is qqw
. So my $a = 42;say qqw/$a b c/;
gives you "42 b c".
Shell quoting is available through qx
, but you should note that backticks do not do shell quoting as in Perl 5, and Perl variables are not interpolated in qx
strings. If you need to interpolate Perl variables in a shell command string, you can use qqx
instead.
The qr
operator is gone from Perl 6.
tr///
works similarly to how it does in Perl 5. The one caveat is that ranges are specified differently. Instead of using a range "a-z", you would use "a..z", i. e. with Perl's range operator. tr///
has a method version, which is better documented, called .trans
. .trans
uses a list of pairs, as follows: $x.trans(['a'..'c'] => ['A'..'C'], ['d'..'q'] => ['D'..'Q'], ['r'..'z'] => ['R'..'Z']);
A much more extensive description of the uses of .trans
can be found at https://design.perl6.org/S05.html#Transliteration. The y///
equivalent has been done away with.
Heredocs are specified differently in Perl 6. You use :to
with your quoting operator, e. g. q:to/END/;
would start a heredoc ending with "END". Similarly, you get escaping and interpolation based on your quoting operator, i. e. literals with Q
, backslash escapes with q
, and interpolation with qq
.
I/O operators
The full details on Input/Output in Perl 6 can be found at io.
As <...>
is the quote-words construct in Perl 6, <>
is not used for reading lines from a file. You can do that by either making an IO
object from a file name or using an open filehandle and then, in either case, calling .lines
on it. I. e. either my @a = "filename".IO.lines;
or my $fh = open "filename", :r;my @a = $fh.lines;
(In the latter case, we are using :r
to specifically open the file for reading). To do this in an iterative manner, you can use a for
loop this way:
for 'huge-csv'.IO.lines ->
Note the use of ->
there. That's part of the Block syntax, and in Perl 6 is needed for if
, for
, while
, etc.
If you want to slurp the entire file into a scalar, you would - surprise! - use the .slurp
method. For instance
my = "filename".IO.slurp;# ... or ...my = open "filename", :r;my = .slurp;
As noted in the Special Variables guide, the ARGV
magic input filehandle has been replaced by $*ARGFILES
, and the @ARGV
array of command line arguments has been replaced by @*ARGS
.
No-ops
1 while foo();
works in the same way as it does in Perl 5, however it generates a warning. In Perl 6 the idiom is now written as Nil while foo();
instead.
Bitwise string operators
Documented individually above, but to summarize...
Bitwise integer negation is prefix +^
. Bitwise boolean negation is ?^
.
Bitwise and is +&
.
Bitwise integer or is +|
. Bitwise integer xor is infix +^
. Bitwise boolean or is ?|
.
Left shift and right shift are +<
and +>
.