1. Features of working with variables and literals in Perl62. Perl6 - Variable operations, anonymous blocks3. Perl6 - Conditional Operators, Loops4. Perl6 - Working with Functions5. Perl6 - ClassesAnd so, after a long break, I returned from studying the sixth pearl. This time I decided to consider how you can carry on a dialogue with the user, and how to work with files, as well as how to divide the entire script into different modules.
In Perl6, two functions can be used to display text on the screen:
say 'text'; print "text\n";
The difference between them, as you already understood from the example, is that say automatically adds a line break after the output of all arguments passed to it, while print prints everything on one line.
However, these functions are actually methods of the IO () object, and if the object for which the method is called is not specified, then the object is used $ * OUT
$*OUT.say("Hello"); $*OUT.say: 'Hello';
The $ * IN object is used to read text from the keyboard.
Methods for this object get and lines:
my @mas = $*IN.lines; say @mas.elems;
In this example, all lines are read from the keyboard, ending the input ^ z.
In the following example, only one line is read.
my $str = $*IN.get; say $str;
')
To work with files, the open function is used, which returns an object of the same type as the variables $ * OUT and $ * IN - IO ():
my $file = open 'D:/test.txt', :r;
To select the opening mode, the named arguments are used: r,: w,: a which can be grouped
It is also worth considering that if only the mode is selected: r, then the script execution will end with an error if the specified file does not exist.
The above named parameters are not the only ones. For example, you can specify: chomp (False), and the end of the line will not be deleted on the read lines. About the remaining parameters can be read in the documentation.
The IO () class contains several additional methods, such as getting the line number in a file, etc. If the need arises, they can also be viewed in the documentation.
Creating modules
To create your own module, use the module keyword:
module MyModule1; module MyModule2 {...};
To indicate which functions will be available in the script that connects this module, the following is export construction is used:
module MyModule; sub HiddenSub {...}; sub ExportedSub is export {...};
As a result, if you connect this module, you can only call the function ExportedSub
The module is connected by the use moduleName command;
You can also consider the conditions when the export is necessary:
module ModuleName; sub Test1 is export(:myConst) {...}; sub Test2 is export(:myAnotherConst, :myAdditionalConst) {...}; sub Test3 is export(:myAdditionalConst); sub Test4 is export(:MANDATORY) {...};
You can connect as follows
use ModuleName :myConst, :myAnotherConst;
: MANDATORY exports the object regardless of which parameters were specified.
If the parameter is specified: myConst, then Test1, Test2 and Test4 will be imported
However, if you specify
use ModuleName :myAnotherConst, :myAdditionalConst
then a compile error will occur - the Test2 function will be re-imported
If you specify
class MyClass is export }
then all class methods will be automatically exported.
It is also worth mentioning that use connects modules at compile time. In addition to it, there is a require function, which connects the modules while the script is running, only when the execution flow reaches this instruction, but for some reason I did not manage to run a single example.