Raku Programming/Basic Operations
Operators
editIn 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
editThe 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:
my Int $x = 5;
$y = -$x # Now, $y is -5
There is also a prefix +
operator which doesn't invert the sign.
Arithmetic Binary Operators
editRaku has a number of arithmetic operators, like other programming languages.
my $x = 12;
my $y = 3;
my $z;
$z = $x + $y; # $z is 15
$z = $x - $y; # $z is 9
$z = $y - $x; # $z is -9
$z = $x * $y; # $z is 36
$z = $x / $y; # $z is 4
$z = $x % $y; # $z is 0 (remainder)
Arithmetic operators expect numerical arguments, so arguments will be converted to numbers automatically as if we used the contextualizer +( )
.
my Str $x = "123";
my Str $y = "456";
my Int $z = $x + $y; # 579
my Int $w = +($x) + +($y); # Same, but more verbose
Strings and ~
edit
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
editWe 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
editTwo strings can be joined together to produce a single new string:
my Str $x = "hello " ~ "world!";
The ~
operator automatically stringifies any arguments that aren't strings. So we can write this:
my Int $foo = 5;
my Str $bar = "I have " ~ $foo ~ " chapters to write";
print $bar;
Which will print out the string: I have 5 chapters to write
.
In most cases it's probably easier to use interpolation, which we will talk about next.
Interpolation
editWe 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:
my Int $x = 5;
my Str $foo = "The value is $x"; # The value is 5
my Str $bar = 'The value is $x'; # The value is $x
Increment and Decrement
editThe 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.
my Int $x = 5;
$x++; # 6 (increment is done after)
++$x; # 7 (increment is done before)
$x--; # 6 (as above)
--$x; # 5 (as above)
The two forms, prefix and suffix forms, appear to generally do the same thing in the example code above. The code ++$x
and $x++
both perform the same action, but it's the time that the action happens that's different. Let's demonstrate this with some examples:
my Int $x = 5;
my Int $y;
$y = $x++; # $y is 5, $x is 6
$y = ++$x; # $y is 7, $x is 7
$y = $x--; # $y is 7, $x is 6
$y = --$x; # $y is 5, $x is 5
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.