📘 Default values in Perl 6 subs

Perl 6 also allows specifying the default values of the sub’s arguments. Syntactically, this looks like an assignment. sub i-live-in(Str $city = “Moscow”) {       say “I live in $city.”;             } i-live-in(‘Saint Petersburg’); i-live-in(); # The default city It is also possible to pass values that are not known at the compile phase. When the … Continue reading “📘 Default values in Perl 6 subs”

📘 Subroutines, or subs in Perl 6

For a sub, which takes no arguments, its definition and the call are very straightforward and easy. sub call-me {     say “I’m called” } call-me; The syntax for declaring a sub’s parameters is similar to what other languages (including Perl 5.20 and higher) provide. sub cube($x) {     return $x ** 3; } say … Continue reading “📘 Subroutines, or subs in Perl 6”

📘 Anonymous code blocks in Perl 6

Perl 6 introduces the concept of so-called pointy blocks (or pointy arrow blocks). These are anonymous closure blocks, which return a reference to the function and can take arguments. The syntax of defining pointy blocks is an arrow -> followed by the argument list and a block of code. my $cube = -> $x {$x … Continue reading “📘 Anonymous code blocks in Perl 6”

📘 Non-value argument passing in Perl 6 subs

By default, you pass the arguments by their values. Despite that, it is not possible to modify them inside the sub. To pass a variable by reference, add the is rw trait. (Note that formally this is not a reference but a mutable argument.) sub inc($x is rw) {     $x++; } my $value = … Continue reading “📘 Non-value argument passing in Perl 6 subs”

📘 Anonymous subs in Perl 6

Let’s look at the creation of anonymous subs. One of the options (there are more than one) is to use syntax similar to what you often see in JavaScript. say sub ($x, $y) {$x ~ ‘ ‘ ~ $y}(“Perl”, 6); The first pair of parentheses contains the list of formal arguments of the anonymous sub; … Continue reading “📘 Anonymous subs in Perl 6”

📘 Modules in Perl 6

Basically, the Perl 6 modules are the files on disk containing the Perl 6 code. Modules are kept in files with the .pm extension. The disk hierarchy reflects the namespace enclosure, which means that a module named X::Y corresponds to the file X/Y.pm, which will be searched for in one of the predefined catalogues or … Continue reading “📘 Modules in Perl 6”

📘 Typed arguments in Perl 6 subs

Similarly to the above-described typed variables, it is possible to indicate that the sub’s parameters are typed. To do so, add a type name before the name of the parameter. sub say-hi(Str $name) {     say “Hi, $name!”; } If the types of the expected and the actual parameters do not match, a compile-time error … Continue reading “📘 Typed arguments in Perl 6 subs”

📘 Optional parameters in Perl 6 subs

Optional parameters are marked with a question mark after their names. The defined built-in function helps to tell if the parameter was really passed: sub send-mail(Str $to, Str $bcc?) {     if defined $bcc {         # . . .         say “Sent to $to with a bcc to $bcc.”;     }     else { … Continue reading “📘 Optional parameters in Perl 6 subs”

📘 Slurpy parameters and flattening in Perl 6

Perl 6 allows passing scalars, arrays, hashes, or objects of any other type as the arguments to a sub. There are no restrictions regarding the combination and its order in a sub declaration. For example, the first argument may be an array, and the second one may be a scalar. Perl 6 will pass the … Continue reading “📘 Slurpy parameters and flattening in Perl 6”