Course of Raku / Advanced / Operators / Types of Raku operators

Quiz — Prefix or postfix

You have seen ++ written after a variable, as a postfix. It can also be written before it. What category is ++ in ++$x?

my $x = 5;
say ++$x;
1prefix
0postfix
0infix
0postcircumfix

Category is decided purely by position. Written before its single operand, ++$x is a prefix operator — the very same ++ is a postfix when written after, as in $x++. Both forms increment $x; the prefix one returns the new value, so this prints 6.

Course navigation

Quiz — Operator categories   |   Quiz — A word operator