Not long ago, I decided to start learning Perl6, even despite the fact that there is still no fully working compiler. I thought that it was possible to watch
Synopsis , to see which of what was written in them was already working, and to study exactly how it worked with various examples. I started doing this, writing down the results of various manipulations with variables to myself in my notebook.
And as my first post I decided to share my knowledge: what the authors of the material usually leave to an independent “study” - answers to questions like “what will happen if ...” or “and what is this in the language ".
In this article I will describe what basic data types there 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 my keyword is used to declare a local variable, our is to declare a global variable (These are not the only ways to declare variables, but for now it will be enough).
- Scalars
Scalars are variables that can contain a single value: Number, string, boolean value, reference.
Scalar variables are declared using the '$' sign. The variables a, b, and c are declared as $ a, $ b, and $ c, respectively. Variables can be immediately initialized with a value.
for example
my $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, such as a number, then a string or any other data type can be assigned to the variable at any time.
for example
my $scalarVar; $scalarVar = ''; $scalarVar = 25; $scalarVar = True;
Basic data types:
- Whole numbers.
Integers are represented by the type Int (). If the number of bits exceeds the maximum number of digits of type Int (), then it is automatically converted to type Num (), which can hold an integer of any length without rounding (I assume that it is of any length, because once for the sake of experiment I got a number of 300 thousand numbers). To write too large numbers, you can use the '_' symbol to visually separate several digits in the number. For example, 1_000_000, 1_0_0_0; 1_00_0
1_000_000, 1_0_0_0; 1_00_0
, but it is better not to use the last two options;
- Fractional numbers
The fractional number is represented by the type Rat () or Num () depending on their value. Numbers can have the form 42.01, 10e-5
. When assigning a variable too long numbers, perl6 may round the fractional part, but it keeps the whole part unchanged, no matter how long it is (Again, I'm not sure about this, but my experiments suggest that this will be the case). If a very large fractional number is obtained as a result of the calculations, the result can be returned as an Inf value, which is a value of type Num ().
- Strings
The string can be any set of uncode-characters, delimited by single quotes (') or double ("). The difference between them is that in the first case, variable substitution as well as special characters (such as \ n, \ t ). If the line is written as 'f \n 1'
then all these characters will be displayed without changes when outputting. If you write "f \n 1"
then two lines will appear on the screen: 'f' and the line '1' .
'\ n' This is a newline character.
- Boolean value
These values are True and False (Required with a capital letter!), Having the type Bool ().
- Links
A variable of this type indicates an existing object. Using the link, you can perform exactly the same actions as with the object itself, to which it points (for example, accessing an element of an array).
The most important thing:
$a = 10; $b = \$a;
In this case, the $ b variable is assigned a reference to the $ a variable. If you display the value of the variable $ b, then the reference will automatically take the value of the variable $ a, BUT if you assign the new value of $b=5;
variable $ b $b=5;
then the value 5 will be written instead of a reference to the variable $ a, and not to the variable $ a itself. Those. Now $ b will contain not a link, but just a number.
- 'Key-value' pair
This type has the name Pair (). The pair constructor is '=>'. For example (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 converted to the string type), and the values for the key can be any scalars.
- Arrays
In Perl6, dynamic arrays, i.e. during their initialization, it is not necessary to specify the maximum size, since memory is allocated as new items are added. The array elements are any scalars and scalar variables. In arrays, the element index depends on the position in the declaration. The array constructor is a comma. For example, the expression ('a', 502, "str")
constructs an array of 3 elements in size (parentheses are optional). Element indices start from zero. those. the zero element of the array is 'a', under the index 1 is the number 502, under the index 2 there is a “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 reference to a new array, which can be assigned to a scalar variable.
The array name must begin with a '@' character. for example @mass, @colors, @names
. Moreover, $ names and @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);
To refer to the elements of an array, square brackets are used:
For example, the expression @ arr2 [0] returns 10, and @ arr2 [3] returns not the $ scalarVar variable but its value, which was at the time of the array creation. To get the value of the $ scalarVar variable to write to the array, you need to reference 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 the 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 non-initialized (The type of this value is Any ()).
- Hashes
Hashes are a table of correspondences, when each key is assigned one value in accordance with one. The values of the hash elements are any scalars and the keys are strings.
The name of the hashes must begin with the character '%':
my %hash = ('key1'=>'value1', '2'=>200, 'key10'=>True);
If you assign an array to a hash, then the array elements will make up a hash element in pairs:
my @mas = ('a, 1, 'b', 2); my %hash = @mas;
As a result, the variable% hash will be stored ('a' => 1, 'b' => 2);
To refer to hash elements, curly braces are used:
%hash{'a'} = 'new value';
To add a new hash element, simply assign the value to a new key:
%hash{'new elem'} = 255;
If the key is to use the number
%hash{1000} = 20;
then the number is converted to a string, so if there is already an element in the hash with the key '1000', then if the value is entered using the numeric key, the existing value will be overwritten.
You can also construct a hash from pairs:
$a = ('a'=>1); $b = ('b'=>2); %hash = $a, $b, 'c'=>3;
ContextsWe now turn to contexts.
Perl 6 is context sensitive, which means that under different conditions of using a variable, different values may be returned. First of all, the context is determined by which variable will be assigned a value:
$ a = ... - sets the scalar context, @a = ... sets the list context, etc.
The following contexts are available:
- Scalar context
The scalar context is defined using the $ () construct, where an expression is specified inside the parentheses, the result of which will be interpreted in scalar context.
When using arrays in 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, using the operation of obtaining an array element).
When using hashes in scalar context, a reference to the hash is returned.
More specific kinds of contexts are:
- Numeric context
The numeric context is set using the + () construct. If a non-numerical result is obtained as a result of an expression interpreted in this context, then the conversion will be performed to a numeric type, and if an error results in an error, the script stops working.
- String context
The string context is specified using the ~ () construct. Usually, the string type can be reduced to any value, so there will be no problems with this context.
- Logical context
The logical context is set using the? () Construct. The result is given according to the rules of casting to a logical type.
- List context
The list context is specified 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 the result is a one-dimensional array @b, which will look like (1, 2, 3, 4, 5, 6)
, rather than a two-dimensional array that looks like ([1, 2], [3, 4], [5, 6])
. It turns out that the list context "merges" all the arrays and hashes transferred to it into one large array. To obtain a multidimensional array, it is necessary to transfer to the constructor not the arrays themselves, but references to them:
@b = (\@a1, \@a2, \@a3)
If a scalar value is used in list context, the result is a single-element array consisting of this scalar.
If the hash context is used in the list context, then all keys and values from the hash will be copied into the array in pairwise form:
%a = (1=>'a', 2=>'b'); @a = %a;
It is important not to confuse the difference between the result of the expression (1, 2)
and the result of the expression [1, 2]
:
in the first case, it turns out an array. If you assign it to the scalar variable $ a = (1, 2), then as a result there will be a link 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 link to the array, but @a = [1, 2] the variable @a will be an array consisting of one element - a link 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 context of a hash 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, 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 the even elements of the array (under the indices 0, 2, 4), and the values are the odd elements of the array (under the subscripts 1, 3, 5)
To set the hash context, use the% () construction
In case you write
$a = [1, 2];
then there will be an error! - because the hash is assigned a scalar value - a reference, and it turns out that the number of keys does not match the number of values.
If you write:
$a = [1, 2]; %a = %($a);
That turns out to be a chain: the reference to the $ a array is used in hash context, so the very value of the array 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, on this my little experience of manipulating variables is so far limited. I hope that you here saw something new for you, and interesting. Have a good day!