1. Features of working with variables and literals in Perl6In the previous article, I described the features of working with variables, and also touched contexts. In this article, I want to share the results of my experiments and searches on the topic of using variables, and also briefly describe the features of anonymous code blocks.
Let's get started
In Perl6, you can specify the type of a variable when declaring.
For example:
my Int $var;
With this declaration, the $ var variable can only be assigned a number. If a variable is attempted to be assigned to a string or another type, then no automatic type casts will be performed - the script will be stopped with an error about type checking. The type can be any existing data type, even custom classes.
You can convert from this type to others without any special restrictions.
With such a declaration of a variable, its initial value will be Int (), and this value cannot be used - this happens because the constructor does not automatically invoke and the variable gets an undefined value of the specified type. To create a new instance of an object when declaring a variable, you can use the construction
my SomeClass $x .= new(a,r,g,s); my $x = SomeClass.new(a,r,g,s);
You can find out if a variable has any value using the defined method:
my Int $x; say $x.defined;
Another way to specify a type is to construct
my $x is Int;
However, in the last, at the moment (09/12) version of Rakudo-star, this method does not work yet.
The following way to specify the type is also possible:
my Array of Array of Int @x;
However, in (12.09) it also does not work, so I can not say anything about the features of this method.
')
Let us turn to the main operations - naturally this list is not complete, but I will give a list of the main operations that we will use:
- Operations with numbers:
- Typical binary and unary operations
These are operations +, -, *, /
In addition to the above, you can use the operation of taking the remainder of the division (%, mod), raising to a power (**), increment and decrement (++, -), taking the integer part of the division (div)
- Bitwise operations: Bitwise “AND” (+ &), bitwise “OR” (+ |), bitwise shift (+ <, +>), bitwise “XOR” (+ ^)
- Comparison operations: ==,! =, <,>,> =, <=.
- String operations:
Gluing two strings
('a'~'b')
Repeat line n times ('a' x 10)
Comparison of lines: Strings are equal (eq), unequal (ne), alphabetically line 1 goes before line 2 (lt, before), later (gt, after), earlier or matches (le), later or matches (gt)
- -Operations with a logical type:
And (? &), OR (? |), XOR (? ^), NOT (!)
- -Using junction ()
Junction is a special type, for group comparison - if you write
$a == any(1, 2, 3)
then the condition will be satisfied if the variable $ a is equal to at least one of the listed values.
Possible operators
- any (
$a==1 or $a==2 or $a==3
) - all (
$a==1 and $a==2 and $a==3
) - one (The variable $ a equals only one of the specified values),
- none ($ a! = 1 and $ a! = 2 and $ a! = 3)
- Determination of maximum (max) or minimum (min) number
$a = 10 min 4; $b = 20 max 99;
When strings are used as operands, the comparison is in alphabetical order.
- ternary operator
$ifValue ?? "yes" !! "no"
The first operand is converted to a Bulin type, and if its value is true, then the second operand is returned, otherwise the third one.
-Use of a meta-operator
It is possible to apply a specific operation for each element of the array:
@a = (1, 2, 3); @b = @a X* 2; # 2, @c = @a X** 2; #
-Change all items
In order to perform an operation for all elements of an array, use the construction >> operator >>
@a = 1, 2, 3; @b = @a>>**>>2, 100, 500;
In this expression, all the elements of the array @a are squared, creating a new array, two numbers are added to the resulting array: 100 and 500
As a result, the @b array contains (1, 4, 9, 100, 500)
-Operator zip
used for pairwise selection of elements from two arrays:
@a = (1, 2, 3); @b = (4, 5, 6); @c = @a Z @b;
Abbreviated form of operator record
In order to add all the numbers, you can use the record [+]
@a = 1, 2, 3; $a = [+] @a;
The result is $ a = 1 + 2 + 3;
Creating an alias
For a variable, you can create an alias that will be considered the second variable name. Variables point to the same area in memory, so changes through one variable name will be visible through the second variable name. Creating an alias is denoted as ': ='
$a = 1; $b := $a;
Create an immutable variable
To create an immutable variable, use the operator ':: =' for example
my $a ::= "aaa";
However, in the current version (12.09) such an announcement produces a compilation error.
Getting variable reference
You can get a reference to a variable using '\':
$a = 10; $b = \$a;
Now about the blocks:
Blocks in Perl 6 can be used as data: individual blocks can be assigned to scalar variables and then executed:
$b = 10; $a = { $b = 1; }; $a(); say $b;
When a block is called for execution, the result of the call becomes the last calculated value in this block. In this example, the calculated value is a constant 1. Inside the blocks, the return statement cannot be used to end the block: it will terminate the function in which the block is called.
Blocks can be used as arguments for function calls:
func($arg1, $arg2, { say 'this is a block'; });
You can declare variables inside blocks, or override existing ones: the block sets its scope to variables, and changes to variables declared inside the block do not affect the variables of the same name outside this block.
In the following code
my $a = ' '; my $closure = { say $a; }; { my $a = ' '; $closure(); }
the string 'Initial value of the variable' will be displayed, because when creating the $ closure variable, the variable declared in the global scope was used, and even after defining a variable with the same name $ a inside the block, the first one will be used.
In the following code, the $ a variable local to the block will be used, even if its visibility is no longer available from the call site.
my $a = ' '; my $closure; { my $a = ' '; $closure = { say $a; }; } $closure();