📜 ⬆️ ⬇️

Perl6 - Comments, whitespace, parentheses

A long time ago, in a distant galaxy far on Habré a set of articles appeared on the sixth pearl
1. Features of working with variables and literals in Perl6
2. Perl6 - Operations on Variables, Anonymous Blocks
3. Perl6 - Conditional Operators, Loops
4. Perl6 - Working with Functions
5. Perl6 - Classes
6. Perl6 - I / O Modules
which talked about some aspects of Perl6. After the release of the last article, I started writing the next one, in which it was planned to create two classes, arranging them in different modules, and connecting them one at the compilation stage, the other at the execution stage - in other words, to consolidate the material already passed. But imagine my surprise when I started to get different compilation errors depending on the number of empty lines between the definitions of two functions or the number of spaces inside the condition ... And correcting one error, I got the second one, which I fixed before the first =) As a result, writing the next article I broke down, and I safely left it for later, and then naturally and there was no free time.
But now I decided to continue the study, and since we are talking about whitespace characters, then we will begin a new article with them.


And all the same, before starting, I would like to note that the documentation states that you can use unicode characters as identifiers, such as Δ or π, but it is possible that, as before, this part is not yet dopilina, or it is a feature of running the script under Windows, but the Russian language symbols I could only use inside the comments. Cyrillic and other Unicode characters could not be used as values ​​or identifiers. But back to the beginning.
Comments in Perl6, as before, begin with the '#' character and everything that comes after it to the end of the line is a comment, but this is not the only way to create comments.
For those who do not like to make comments in the size of one long line, and do not like to put ten '#' characters in a column, they invented the following construction:
#`(comment) 

The features of this type of comment is that it is a multi-line comment. which is limited to brackets, and instead of the characters '(' and ')' a set of any pair of unicode characters can be used (provided that the file is written in unicode encoding), such as a set copied from the documentation:
 #`「 test abcd  」 say "123"; 

You can also use, for example, the characters '[', '<<', '{' and the corresponding closing characters or in general all at once:
 #`[{<< Comment[index] >>}] 

As a result, the closing ']' bracket inside the comment will be ignored despite the fact that the comment starts with the opening '[' bracket, and the comment itself will end only on the set '>>}]'.
Another feature of the comments is that if you write a construct
 #`{comment {some text} comment } 

then the number of brackets '{' and '}' inside the comment will be counted, and as a result, the comment will end exactly where it was intended.

It is also worth noting that comments are plable characters, and if you write the following construct
 "text"#`(comment).say 

the compiler will interpret this as follows:
 "text" .say 

and, accordingly, a compilation error will be received, since the postfix method call should not be separated by any whitespace characters. In order to allow this way of commenting, however, the command for escaping whitespace characters was introduced:
 "text"\ #`(comment) .say; 

The backslash before a sequence of whitespace characters (there may be several of them, and consist of comments, spaces, tabs and anything else that may fit the category of whitespace characters) "cuts out" them from the string perceived by the compiler, resulting in
 "text".say 

')
Enough about the comments, let's talk about more interesting things.
Everyone is familiar with such things as quoting (qq / "text" 123 /), let's take a closer look at this thing (add some data from the directory):

The general form of such expressions is
 Q :verb /data/ 

where as: verb can be one of the following names:


However, good old versions of such commands as q / test test test / or qw / test test test / are still available to us.
You can also combine them, for example
 my $result = Q :exec :double /$command/; my $sameResult = qqx/$command/; 

At the beginning, the string is interpolated, then executed. It should also be noted that the order of the keys is important, although I did not force me to perform the following line as I need
 my @results = Q :x :w /ver ver ver/ 

As planned, the string “ver ver ver” should have been divided into 3 words, and each of these words should be executed as an cmd interpreter command, but at the output all I saw was an array of 3 words “ver”. Perhaps you should try this method yourself, but I will go further.

However, back to the topic of brackets - in the case of the above commands, we are not limited to only slashes, in fact, we can use instead everything that we used in restricting comments:
 my $result = $somePath ~~ rx!/etc/init.d/!; my $anotherResult = $somePath ~~ rx<</etc/init.d/>>; 


Well, that's probably enough for today. I hope the continuation will not take long.

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


All Articles