📜 ⬆️ ⬇️

Perl6 - Variable operations, anonymous blocks

1. Features of working with variables and literals in Perl6
In 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; #False 


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:


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; # $b=1; 

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(); 

Source: https://habr.com/ru/post/153875/


All Articles