Catégories
Uncategorized

The next iteration of Perl is now a different language called Raku (previously known as Perl6)

Image result for history of raku programming language

It became raku in 2019. I recently became interested in the language after seeing too many short solutions on stackoverflow to ordinary scripting problems I face everyday in my daily work.I usually resolve to bash, awk and sed, but sometimes the solution in those tools is rather cumbersome and awkward, even for simple problems like printing the last line of a file. Sure, you can do that with tail, but why add another process in the pipeline when you can solve all your problems in one process and be efficient?

About the Raku Name

RAKU WARE | History-Roots of Raku ware

The name Raku probably comes from the special kind of vessel used in Japanese tea ceremony.

Why the interest in Raku ?

The starting point was the short perl scripting solutions as mentioned earlier, but what piqued my interest is this particular quote from Lary Walll in a /. interview I found while searching for perl content on lobste.rs. (There’s a summary of it in next paragraph if you don’t want to read it completely). When asked :

It seems that most back-ends are now being written in any number of next-gen scripting languages like Python, JavaScript (NodeJS), and Swift. I don’t see the advantages of these, but it’s often hard to explain to colleagues, CTOs, managers, etc. the value of Perl over the newest trends. And Perl « 6 » is meaningless because to everyone else it’s still Perl. Why should we choose Perl 6 over the new establishment?

He simply responded

Because it’s the next generation after all those languages you mentioned.

He answered similarly to another question asked by user drakaa :

Perl used to be central to so many things (the ‘glue’ language for the internet), but seems to be slowly falling out of use in deference to JavaScript, Java, Python, VBScript/PowerShell, etc. It’s the language I used in my first job as a system administrator (back around the time you gave your first interview), and I loved it.

With so many years between the announcement of Perl 6 and it’s completion, many people moved on to other solutions or technologies. Perl 6 is here now, but why should I use it?

with :

Because it’s the next generation after all those languages you mentioned. Or maybe it’s several generations beyond that, but showed up early. 🙂

Elements of interest

While reading the interview and a couple other links, I took notes of what would possibly encourage me to learn more about this new language :

  • Supports multiple paradigms like OO, FP and CP. This means that you don’t have to switch language if you want to switch paradigms.
  • It allows you to solve your problem in more than one way, it doesn’t enforce you to use a certain way, there’s no cultural acceptance about what’s the proper way to do it. For example, python’s culture enforces you to use list comprehensions as the right way to process your lists. Perl offers you more than one way to process a list.
  • python has a global interpreter lock, perl 6 has a concurrency model that can scale up to multicore which avoids bottlenecks. It borrows from languages such as haskell, c#, go and erlang.
  • python’s object model just gives you hooks to its internal representation of classes and objects, raku gives a coherent meta-object model. For example, there is no simple way to create a singleton in python (see borgs).
  • python doesn’t provide the benefits of lexical scoping, closures, laziness nor does it encourage you to think that way.
  • Raku is concise
  • very good and early unicode support compared to competitors (I’m looking at you python2 !)
  • event loop aware scheduler
  • continuations

Elements of confusion

It is true that Raku has some elements of seduction, but it also has its own quirks, like any other language I guess. While skimming through documentation and various links, there are a few things that looked a little odd and that needed clarification / proper learning. For example, consider this line of code :

f() = 42;

It seems like you are calling a function f, which returns something, then assign 42 to that, but you don’t store that value anywhere so it’s immediately thrown away… « Normal » code would look like this :

x = f();

x = 42;

But even then, what’s the point of assigning x the result of f() if you are immediately overriding it with 42?

So I went to IRC, joined #raku and asked

[16:17] <ychaouche> can't understand this yet : f() = 42;
[16:18] <lizmat> that function f() returns a container
[...]
[16:19] <lizmat> m: my $bar; sub foo() is raw { $bar }; foo() = 42; say $bar
[16:19] <camelia> rakudo-moar ec4522404: OUTPUT: «42␤»
[16:19] <lizmat> the "is raw" on the sub makes sure it doesn't de-containerize (which is the default)

So what happens in lizmat code is that the variable $bar is created outside function foo(), and also modified outside of foo() (there is no code modifying $bar inside of foo).

The next line of code f()=42; will modify $bar as shown in camelia's output (it’s the bot that executes code you give to it when you start your sentence with m: as lizmat did) . Isn’t that interesting ?

is raw and is rw, same or different ?

Searching for the concept of raku containers, I saw the docs use is rw instead of is raw used in lizmat’s example. Consider this code :

my $x = 23;

sub f() is rw { $x };

f() = 42;

say $x;         # OUTPUT: «42␤»

Are those two the same thing with different spellings or are they two different things ? according to lizmat, they’re different, and so it is something I need to add to my to-learn list.

To-learn list

In addition to the is rw and is raw keywords(?) that I need to learn, the intro page warns the newcomer of 3 concepts that raku seem to do differently than the competition, these are :

  1. signatures
  2. containers
  3. classes and roles

It also enumerate a few traps to avoid in a dedicated page. All of which I need to add to my to-learn list.

End of part 1

I hope I’ll write more blog posts about my journey in learning this language, I do not expect to write more than this page to be honest, I’ll soon have to swtich tasks and switching tasks often leads to switching interests. What inspired me to write this blog post is two things :

  1. Raku’s community page encourages new language learners to share their experience
  2. the hope to get feedback that would accelerate my learning, put me on the right tracks and avoiding wrong routes.

Coincidentally, a tweet appeared on my timeline in about the same time, from @Ehabcoder, who is about to work in his new blog

Let’s see where this leads.