πŸ“œ ⬆️ ⬇️

Perl6 - Working with Functions

1. Features of working with variables and literals in Perl6
2. Perl6 - Variable operations, anonymous blocks
3. Perl6 - Conditional Operators, Loops

It is time to consider working with functions. On this topic in Perl6 there are a few changes relative to the fifth, such as named parameters, or the ability to create a main function in a script, but let's start in order:


')
In Perl6, functions are declared as constructs.
sub SubName($a, $r, $g, $s) { //SomeWork } 

It is possible to specify the types of parameters obtained.
 sub SubName(Int $a) { SomeWork(); } 

however, if the function is called with an argument of a different type, the execution of the script will be stopped with an error. Also, the number of arguments passed must match the number of arguments when a function is declared.

The function call is done using constructs.
 f(10, True, "str", {say "closure";}); f 20, False, "aaa", 9.999; 


As in C ++, we can overload functions, but for each function it will be necessary to add the word 'multi':
 multi sub f(Int $a) {say 'Int';}; multi sub f(Str $a) {say 'Str';}; f("string"); f(100500); 

As a result, it will be displayed on the screen:
 Str Int 


To indicate the scope of functions, the words my and our are used - respectively, the visibility in the area where the function was declared and the global scope.
You can specify the type of value returned by the function:
 my Int sub func($a) {return $a+1;} 

If the type and return type specified during the declaration does not match during operation, the script will be stopped with a type mismatch error.
The function parameters can optionally indicate whether these parameters can be changed within the function:
 sub f($a is rw) { $a*=10; } my $z = 5; f($z); say $z; 

As a result, the number 50 will be displayed.
Also in the synopsis, it is stated that the function can return an lvalue value:
 my $b = 10; sub f() is rw { return $b; } f() = 5; say $b; 

As a result, the number 5 should be displayed on the screen, but in the latest version (12.09) the possibility of using lvalue values ​​is not yet ready, and in my case an error was generated during compilation.
Perl6 has the ability to use named parameters:
 my sub func(:$name1, :$name2, :$name3) { say $name1, ' ', $name2, ' ', $name3; } my $a = 10; func :name2<text2>, :name1<1+2+$a>, name3=>$a; my %hash = {name3=>'text3', name2=>'text2', name1=>'text1'}; func |%hash; 

As a result, the screen will display:
 1+2+$a text2 10 text1 text2 text3 

If the parameter is not passed to |% hash but simply% hash, then the function will take it as a positional argument - in our case, the function has no positional elements, so the script will be completed with an error.
You can also use a structure to specify a named argument: arg ($ value). The difference between this option and: arg <$ value> is that in the second case the value of the variable will not be substituted, and as a result in the first case we get the value of the $ value variable, and in the second case we get the string '$ value'.
In addition, you can use optional parameters:
 sub HaveArgument($arg?) { if $arg.defined { say 'yes'; } else { say 'no'; } } HaveArgument; HaveArgument 10; 

As a result, we get the lines
 no yes 

To create functions with a variable number of arguments, as was the case in perl5, you can use the construction:
 sub Counter(*@arg) { say @arg.elems; } Counter(1, 2, 3); Counter; 

As a result, two numbers will be displayed: 3 and 0
Before * @ args you can specify positional elements:
 sub Counter($a, *@arg) {...} 

MAIN function:
In Perl6, the ability to create a function MAIN, as in C ++, has appeared, however, there are several differences. Narpimer outside the function body, you can make calls to other functions, and these calls will be made before the MAIN function starts:
 sub MAIN() { say "MAIN!"; say "Hello!"; } say "123!"; 

As a result, on the screen we will see the lines
 123! MAIN! Hello! 

Also for the MAIN function, you can set the input parameters, which will be required parameters when calling the script for execution:
If the contents of the script:
 sub MAIN($arg1, $arg2) { say "MAIN!"; say $arg1, ' ', $arg2; } say "123!"; 

And call the script without parameters, as a result we will get the text on the screen:
 123! Usage: /home/warfair/ /script.p6 <arg1> <arg2> 

Notice that in this case the say β€œ123!” Function was called, which is outside the body of any function, and only after that, the MAIN call error occurred.
The function reference can be saved to a variable using the construct
 sub MySub() {...}; my $var = &MySub; 


You can create anonymous functions and store references to them in scalar variables.
 my $r = sub ($a) { return $a*2; } say $r(2); 

As a result, we will see 4

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


All Articles