Raku Programming/Basic Operations

Operators
In the past few chapters we've been looking at Raku data, and the variables that hold that data. Now we'll explore what kinds of things we can do with that data once we have it. Perl has a series of normal arithmetic operators that can be applied to integer and floating-point number values, and even a few operators for other data types too. Once we learn about all the normal operators, we can start to look at meta operators which take those same concepts and apply them to different contexts.

An operator works on its operands -- that quantity on which the operation is being performed. To understand any operator, you have to know its arity (how many operands it takes). Its called a unary operator if it takes one operand, binary if two, and ternary if three.

Arithmetic Unary Operators
The simplest arithmetic operators are the unary sign operators  and. These get applied to the front of a numerical value to affect the sign of that number:

There is also a prefix  operator which doesn't invert the sign.

Arithmetic Binary Operators
Raku has a number of arithmetic operators, like other programming languages.

Arithmetic operators expect numerical arguments, so arguments will be converted to numbers automatically as if we used the contextualizer.

Strings and
In Raku, the symbol  is always associated with strings. When used as a prefix, it turns whatever variable it was used on into a string if it wasn't before. When used as an operator between two strings, it joins the strings together end-to-end in a process called concatenation.

Stringification
We already talked about using  as the string context specifier. This is known as stringification. Stringifying converts a variable from other types into a string representation.

Concatenation
Two strings can be joined together to produce a single new string:

The  operator automatically stringifies any arguments that aren't strings. So we can write this:

Which will print out the string:.

In most cases it's probably easier to use interpolation, which we will talk about next.

Interpolation
We showed briefly before that there are three basic types of strings: double-quoted strings, single-quoted strings, and heredocs. Single-quoted and double-quoted strings may look similar, but they behave differently from each other. The difference is interpolation.

Double-quoted strings are interpolated. Variable names that appear inside the string are converted to their string value and included in the string. single-quoted strings do not have this behavior:

Increment and Decrement
The operations by which a variable is increased or decreased by one is common enough to warrant specific operators. The  and   operators can be used as prefixes or suffixes to a scalar variable. These two different locations have subtle differences.

The two forms, prefix and suffix forms, appear to generally do the same thing in the example code above. The code  and   both perform the same action, but it's the time that the action happens that's different. Let's demonstrate this with some examples:

The prefix version performs the increment or decrement before the variable is used in the statement. The postfix version performs the increment or decrement after the variable is used.