🎥 The Pearls of Raku, Issue 1

Let me demonstrate a few interesting things that I found by looking at the Raku solutions of last week’s Perl Weekly Challenge 1. All of them are notable as they reflect how differently people think, how unexpectedly they approach the same problems, and how big Raku is. So big that you forget or never meet some of its corners. In this issue: method operatos, $*USAGE, and :skip-empty.

Let me demonstrate a few interesting things that I found by looking at the Raku solutions of last week’s Perl Weekly Challenge 1. All of them are notable as they reflect how differently people think, how unexpectedly they approach the same problems, and how big Raku is. So big that you forget or never meet some of its corners.

Methodops

The so-called method operators, when you can use an existing function and call it as a method on an object(!). Just add an &.

sub myfunc($x) {
    say "[$x]";
}

'abc'.&myfunc;

$*USAGE

If you have a MAIN function (one or more) in the code, you can read $*USAGE, where you’ll find a text description of how to use your program that Raku created based on the signature(s) of MAIN. It may be useful if you want to create an extended message and include the pre-generated part to it.

sub MAIN() {
    say "This is how to use my program:\n" ~ $*USAGE;
}

:skip-empty

The split routine has a parameter that prevents adding empty strings before and after the delimiter. This is how it was used in one of the solutions:

my $s = 'abc';
dd $s.split('', :skip-empty); # ("a", "b", "c").Seq

Compare with the result that you get if you omit the flag:

my $s = 'abc';
dd $s.split(''); # ("", "a", "b", "c", "").Seq

Actually, there is a simpler way to get the same correct splitting by using comb without any parameters:

my $s = 'abc';
dd $s.comb; # ("a", "b", "c").Seq

Raku Challenge 069-1 review

→ Navigation to the Raku challenges post series

Leave a Reply

Your email address will not be published. Required fields are marked *