Perl6 - I / O Modules

    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

    And so, after a long break I returned from studying sixth pearl. This time I decided to consider how it is possible to conduct a dialogue with the user, and how to work with files, as well as how to divide the entire script into various 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 feed after printing all the 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 $ * OUT object is used
    $*OUT.say: 'Hello';

    The $ * IN object is used to read text from the keyboard.
    The methods of this object get and lines:
    my @mas = $*IN.lines;
    say @mas.elems;

    In this example, all lines are read from the keyboard, the end of input is ^ z.
    The following example reads only one row
    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: r is selected, the script execution will fail if the specified file file does not exist.
    The above named parameters are not unique. For example, you can specify: chomp (False), and the characters of the end of the line will not be deleted from the read lines. You can read about other parameters in the documentation.
    The IO () class contains several additional methods, such as obtaining the line number in a file, etc. If necessary, you can also see them in the documentation.

    Creating modules
    To create your module, use the module keyword:
    module MyModule1;
    module MyModule2 {...};

    The following is export construct is used to indicate which functions will be available in the script that connects this module:
    module MyModule;
    sub HiddenSub {...};
    sub ExportedSub is export {...};

    As a result, if you connect this module, you can only call the ExportedSub function.
    The module is connected with the use moduleName command;

    You can also take into account the conditions when 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 no matter what 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 compilation error will occur - the Test2 function will be re-imported

    If you specify
    class MyClass is export
        method MyMethod {...}

    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 that connects modules while the script is running, only when the execution thread reaches this instruction, but for some reason I didn’t manage to run a single example.

    Also popular now: