Features of working with variables and literals in Perl6
Not so long ago, I decided to start learning Perl6, even though there is no actually a fully working compiler yet. I thought that you can watch Synopsis , see what written in them already works, and study how it works on various examples. I began to do this, simultaneously writing down the results of various manipulations with variables in my notebook.
And so, as my first post, I decided to share my knowledge: what the authors of the material usually leave for independent “study” - answers to questions like “what will happen if ...” or “what is it like in the language ".
In this article I will describe what the main data types are in this language and partially address the issue of contexts - one of the main features of the Perl language.
Types of variables and types of their possible values.
Perl6 has 3 main types of variables: Scalars, Arrays, Hashes.
To declare a variable, the keyword my is used - the declaration of a local variable, our - to declare a global variable (These are not the only ways to declare variables, but for now, that's enough).
Contexts
Now let's move on to contexts.
Perl 6 is context-sensitive, which means that under different conditions for using a variable, different values may be returned. First of all, the context is determined by which variable the value will be assigned:
$ a = ... - the scalar context is set, @a = ... the list context is set, etc.
The following contexts are available:
Well, this is where my little experience with manipulating variables is still limited. I hope you see something new for you, and interesting. Have a good day!
And so, as my first post, I decided to share my knowledge: what the authors of the material usually leave for independent “study” - answers to questions like “what will happen if ...” or “what is it like in the language ".
In this article I will describe what the main data types are in this language and partially address the issue of contexts - one of the main features of the Perl language.
Types of variables and types of their possible values.
Perl6 has 3 main types of variables: Scalars, Arrays, Hashes.
To declare a variable, the keyword my is used - the declaration of a local variable, our - to declare a global variable (These are not the only ways to declare variables, but for now, that's enough).
- Scalars.
Scalars are variables that can contain a single value: Number, string, Boolean value, link.
Scalar variables are declared using the '$' sign. Variables a, b, and c are declared as $ a, $ b, and $ c, respectively. Also, variables can be immediately initialized with a value.
for instancemy $scalarVar; my ($var1, $var2, $var3); my $newStr = "Это строка"; my $bigNumber = 192958238719732641028368452872938741029834612983412038471293847123641892364; my ($a, $b, $c) = 1, 2, 3; или my ($a, $b, $c) = (1, 2, 3);
If a variable was assigned a value of one type during the declaration, for example, a number, then at any time a string or any other data type can be assigned to the variable.
for instancemy $scalarVar; $scalarVar = 'Строка'; $scalarVar = 25; $scalarVar = True;
Basic data types:- Whole numbers.
Integers are represented by type Int (). If the number by the number of digits exceeds the maximum number of digits of the Int () type, then it is automatically converted to the Num () type, which can hold an integer of any length without rounding (I assume that any length, because once for the sake of the experiment I got a number of length 300 thousand digits). To write numbers that are too large, you can use the '_' character to visually separate several digits in a number. For example1_000_000, 1_0_0_0; 1_00_0
, however, it is better not to use the last two options; - Fractional numbers A
fractional number is represented by the Rat () or Num () type, depending on their value. The numbers may be of the form42.01, 10e-5
. When assigning a variable too long, perl6 can round off the fractional part, but keeps the whole part unchanged, no matter how long it is (Again, I am not sure about this, but my experiments suggest that it will be so). If the result of the calculations is a very large fractional number, then the result can be returned as a value of Inf, which is a value of type Num (). - Strings
A string can be any set of uncode-characters, limited to single quotes (') or double ("). The difference between them is that in the first case there is no substitution of variables as well as special characters (such as \ n, \ t). If a line is written as'f ф \n 1'
it is, all these characters will be displayed unchanged, but if you write"f ф \n 1"
it, two lines will appear on the screen: 'f f' and the line '1'.
'\ n' This is a symbol for switching to a new line. - Boolean.
These are True and False (Mandatory with a capital letter!), Of type Bool (). - References
A variable of this type points to an existing object. Using a link, you can perform exactly the same actions as with the object itself, which it points to (for example, accessing an array element).
The most important thing:$a = 10; $b = \$a;
In this case, the variable $ b is assigned a reference to the variable $ a. If you display the value of the variable $ b, then the value of the variable $ a will be automatically taken from the link, BUT if you assign a new value to the variable $ b,$b=5;
then the value 5 will be written instead of referring to the variable $ a, and not to the variable $ a itself. Those. now $ b will not contain a link, but just a number. - Key-value pair
This type is named Pair (). The constructor for pairs is '=>'. For example, it(1=>'a')
creates a pair, where the key is 1, and the value for this key is 'a'. Moreover, the possible values of the keys are strings (in this case 1 was automatically cast to a string type), and any scalars can be values by key.
- Whole numbers.
- Arrays
In Perl6, arrays are dynamic, i.e. when they are initialized, you do not need to specify the maximum size, because memory is allocated as new items are added. The elements of the array are any scalars and scalar variables. In arrays, the element index depends on the position when declaring. The array constructor is a comma. For example, an expression('a', 502, "str")
constructs an array of 3 elements (parentheses are optional). Indexes on elements start from zero. those. the zero element of the array is 'a', under the index 1 is the number 502, under the index 2 is “str”. Types of scalars do not have to be the same. To generate an array reference, square brackets are used. For example,['a', 502, "str"]
returns a link to a new array that can be assigned to a scalar variable.
The array name must begin with the character '@'. for instance
. And $ names@mass, @colors, @names
@names
are different variables.
Arrays can contain any number of elements of various types.
Examples of using:@arr = ('a', 502, "str"); @arr2 = (10, False, True, $scalarVar);
The square brackets are used to access the elements of the array:
For example, the expression @ arr2 [0] will return 10, and @ arr2 [3] will return not the $ scalarVar variable but its value, which was at the time the array was created. To get the value of the $ scalarVar variable, writing to the array needed a link to this variable:@arr2 = (10, False, True, \$scalarVar);
If the initial size of the array is 2:@arr = (1, 2);
and we want to set the value of the element under index 5 (for example, arr [5] = 'new elem') then the array will be expanded to 6 elements , and all added elements except arr [5] will be uninitialized (The type of this value is Any ()). - Hashes
Hashes are a correspondence table, when each key is assigned a single value in accordance. The values of the hash elements are any scalars and the keys are strings.
The hash name must begin with the character '%':my %hash = ('key1'=>'value1', '2'=>200, 'key10'=>True);
If you assign an array to the hash, then the elements of the array will make up the hash element in pairs:my @mas = ('a, 1, 'b', 2); my %hash = @mas;
As a result of this, the% hash variable will be stored ('a' => 1, 'b' => 2);
Curly brackets are used to access the hash elements:%hash{'a'} = 'new value';
To add a new hash element, simply assign a value with the new key:%hash{'new elem'} = 255;
If you use a number as the key,%hash{1000} = 20;
the number is converted to a string, so if the hash already has an element with the key '1000', then when entering values by numeric key an existing value is overwritten.
A hash can also be constructed from pairs:$a = ('a'=>1); $b = ('b'=>2); %hash = $a, $b, 'c'=>3;
Contexts
Now let's move on to contexts.
Perl 6 is context-sensitive, which means that under different conditions for using a variable, different values may be returned. First of all, the context is determined by which variable the value will be assigned:
$ a = ... - the scalar context is set, @a = ... the list context is set, etc.
The following contexts are available:
- Scalar context The
scalar context is defined using the $ () construct, where the expression is specified inside the brackets, the result of which will be interpreted in the scalar context.
When using arrays in a scalar context, instead of the array itself, a reference to the given array is returned, which can already be used as the name of the array (for example, use the operation to obtain an array element).
When using hashes in a scalar context, a hash reference is returned.
More specific types of contexts are:- Numeric context The
numerical context is defined using the + () construct. If the result of the expression interpreted in this context, the result is not a numerical result, then a numerical type will be converted, and if an error results as a result of the conversion, the script stops working. - String Context
String context is defined using the ~ () construct. Usually, any value can be cast to a string type, therefore, problems with this context are not expected - Logical context The
logical context is defined using the? () Construct. The result is issued according to the rules of casting to a logical type.
- Numeric context The
- List context
The list context is defined using the @ () construct. An example of creating arrays:@a1 = (1, 2); @a2 = (3, 4); @a3 = (5, 6);
However, if you write the following line@b = (@a1, @a2, @a3);
then, as a result, we get a one-dimensional array @b, which will look like(1, 2, 3, 4, 5, 6)
, rather than a two-dimensional array, looking like([1, 2], [3, 4], [5, 6])
. It turns out that the list context merges all arrays and hashes transferred to it into one large array. To get a multidimensional array, it is necessary to pass to the constructor not the arrays themselves, but references to them:@b = (\@a1, \@a2, \@a3)
If a scalar value is used in the list context, then the result is a singleton array consisting of this scalar.
If a hash is used in the list context, then all keys and values from the hash will be copied to the array in pairs:%a = (1=>'a', 2=>'b'); @a = %a; # (1, 'a', 2, 'b')
It is important not to confuse how the result of the expression differs(1, 2)
from the result of the expression[1, 2]
:
in the first case, an array is obtained. If you assign it to the scalar variable $ a = (1, 2), then in the end there will be a reference to the array in this variable, if you write @a = (1, 2) then @a will be an array of two elements.
In the second case, $ a = [1, 2] will also contain a reference to the array, but @a = [1, 2] the @a variable will be an array consisting of one element - a reference to the array (1, 2), t .to. the link is a scalar value, which becomes the only element of the @a array. - Hash
context In the hash context of scalar variables, you can use only pairs, or references to arrays and hashes (about arrays below).
If an array with an odd number of elements is used in the hash context, then the script stops working, because when constructing a hash, the number of keys and the number of values will not match.
If an array with an even number of elements is used, the result is a hash in which the keys are even elements of the array (under indices 0, 2, 4), and the values are odd elements of the array (under indices 1, 3, 5)
To set the hash context the% () construct is used
if you write$a = [1, 2]; %a = $a;
then the result will be a mistake! - because the scalar value is assigned to the hash - the link, and it turns out that the number of keys does not match the number of values.
If you write:$a = [1, 2]; %a = %($a);
This turns out to be a chain: the reference to the array $ a is used in a hash context, therefore the value of the array itself is taken. When casting an array type to a hash type, a new hash is created (1 => 2), and it is already assigned to the variable% a;
Well, this is where my little experience with manipulating variables is still limited. I hope you see something new for you, and interesting. Have a good day!