Polka is a small web-app I wrote using Dancer as an exercise in minimalism. It is inspired by werc. Polka recursively reads a directory filled with markdown files, convert and serve them using HTTP, with a nice design and a navigation menu.
My friend Marc asked me to port it to Rakudo Perl6.
It took me a while to figure out all the new tricks, as it is my first Perl6 script. The code may not be perfect for this reason (and I am open to code reviews) but it gave me a first feeling of the so long awaited language.
I’m trying to give a feedback by comparing small blocs of code in the 2 idioms.
First, the dependencies.
Polka depends on Dancer (a web framework) and Text::MarkDown (markdown to html converter).
1 2 3 4
Rakudo Star bundles a clone of Dancer called Bailador. It is far from complete, but ok for our case.
No need to specify use utf8, Perl6 allows utf8 source code by default.
Text::Markdown in Perl6 is not complete either: I found it on Karl Masak github, it makes use of Perl6 Grammars to parse Markdown, but no HTML converter was done. Althrough it may be easy to code, the grammar itself is incomplete. Please contribute to Text::Markdown if you have some free time left.
1 2 3
My first good surprise
Perl6 has a builting slurp function (slurp means reading a file in a single time), so the following line vanished:
Yet another goodie
In Polka, this recursive function walk through subdirectories and build a hash that I use later to generate the menu HTML markup.
1 2 3 4 5 6 7 8 9 10 11 12
Thanks to Perl6, you don’t need to open and close directories no more. There is a dir() function that does this for you. This function also filters the current “.” and parent “..” directories. The resulting code is a lot prettier.
1 2 3 4 5
- New function signatures
- ?? and !! is the new ternary operator wich replaces ? and :
- .IO.d means $.IO.d_
- No need to escape the hash, Perl6 passes references to structures
The menu sub
This recursive sub takes the preceding hash as argument and build a classic know ul > li menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Now the Perl6 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Let’s look at it in details.
The new function signature forced me to specify a default value for my optionnal argument $unfolded.
%tree is not casted to an array here and behave more like an hashref.
Next chunk. One of the ways to loop over keys and values of a hash.
I first tried something like this:
But .key and .value cannot be interpolated in regexes, so I has to rename them. The .kv method does the trick here. It flattens the hash, and give as much elements as we ask for each loop. In this case 2.
The is copy thing took me a while to understand. It looks like Perl6 for creates read only variables. We have to tell the interpreter that we want copies instead. I don’t know the reasons. But for now I dislike this thing…
Another interesing thing is this:
In this code, I embeds the result of split (wish is a list) in an arrayref, to cast it to an array so pop can pop it. I always hated this in Perl5. I was happy to find that Perl6 is smarter than is predecessor in this case, I was able to rewrite it like the following:
And finally in a more idiomatic way:
The web framework callback
This callback captures the entire path after / in the url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
And here is the new code in Perl6 using Bailador :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Most of this code is not interesting, however, translating the following line gave me a headacke:
First, I had to understand that the $path created by Bailador is read only. The is copy fixed this first issue.
I thought that the following would work,
but it did not, because an empty $path was evaluated to True!
To understand why, I had to debug like this:
wich is the new way to dump.
This gave me the reply. $path was an instance of the Match() class wich evaluates to True if the regex matches, wich is always the case, either way we would never have entered the callback.
The fix was to cast $path to a string. An empty string evaluates as False and that allowed me to test it and replace it if empty.
Conclusions on the resulting Perl6 app
- The code is smaller.
- It is prettier in most cases.
- Perl6 is fast enought to power webapps.
- The error messages are more precise with a nice stacktrace, except when the interpretor gets confused.
- Perl6 still lacks mainstream libs.
- Too much types and classes makes the language tricky. This is my opinion.