28.09.2021 -- #

Raku

Larry Wall’s Stranger than Fact Talk got me excited about perl6 / raku! I first tried perl a few years ago and liked it. For me it fits in between Bash and python. Bash is great for simple shell scripting but terse syntax and not being a full blown programming language limit its usefulness. I found perl’s shell interop powerful for scripting tasks that benefit from error handling and clear types. I’ve even landed a perl script in my work code base; it handles running e2e tests on local. Oh the joy of JavaScript developer’s reaction when they see perl, 😃.

Raku is the next step in that. It is Perl6 with a new name, keeping the good parts of perl and removing warts to make it more accessible. After spending two days with the language, it delivers on that promise. Before I get into what I built, general thoughts on Raku:

  • the openness and kindness stemming from Larry and the other contributors is awesome
  • The perl motto “There’s More Than One Way To Do It” connects with me. I think to often us programmers get caught up in object oriented vs functional vs procedural division when mixing all three is the spice of life.
  • nice functional programming elements like pipes make it easy to have fun with functions
  • classes are more clear then in perl while maintaining the ability to bless things ❤️
  • unfortunately I ran into one of the problems I had in perl: How the hell do I import modules from another file 😦. The compiler needs to know where to look for modules so: RAKULIB='./helpers/post_analysis/lib' raku ./helpers/post_analysis/bin/post_analysis.raku worked in the end. Still don’t know why it can’t just look next to the existing file I’m trying to run like node.js.

code example

Here is an example showing Reduction metaoperators, similar to a reduce statement. Might seem magic but unlike Bash it worked smoothly for me.

sub draft_posts { $^a + ($^b{'is_draft'}  ?? 1 !! 0) };
my $draft_post_total = [[&draft_posts]] @posts;
  • @posts is a list of post hash objects
  • ?? !! is a ternary
  • sub is subroutin aka function
  • $^a is the first argument
  • $^b{'is_draft'} is accessing the value of ‘is_draft’ key in the second argument which is a hash map in this case

raku learning resources

CLI tool: Hugo Post Analysis

repo: https://git.sr.ht/~travisshears/hugo-post-analysis

To get to know Raku I set out to answer open questions about my personal site.

  • How many drafts do I have?
  • What percent of my English posts have been translated into German?

Ended up with a script scans over the files/folders and finds the answers.

Example output:

$ post_analysis
Which sections do you want draft count for:
snippets
blog
tags
notes
tutorials
=== OR ===
all
blog
=== Site Analysis ===
English Posts: 69
German  Posts: 17
Percentage translated: 24.637681%
Draft Posts:   9
=== Drafts ===
kampenwand
s3 bucket for site plugins
diy temperature monitor
life clock dev log
first six months: coral
Mt Journey

reflecting on script output

25% of my English posts have been translated to German, not bad! In the coming months I hope to raise that number while still releasing new post.

My future with Raku

Might be the new factor but as of right now I’m enjoying Raku. I plan to rewrite my personal site deploy tool in it and create a brew module as the next challenge. I hope you are inspired to give it a try. 👋

\- [ tech, raku ]