In the previous chapters, we discussed some examples interleaved with the Raku mechanics that make them work. Here I want to summarize and deepen the Raku knowledge that we’ve touched on so far, removed from the original examples.
8.1 Variables and Scoping
In Raku, variable names are made of a sigil, $, @, %, or &, followed by an identifier. The sigil implies a type constraint, where $ is the most general one (no restriction by default), @ is for arrays, % is for hashes (associative arrays/maps), and & is for code objects.
Identifiers can contain - and ' characters, as long as the character after it is a letter. Identifiers must start with a letter or underscore.
Subroutines and variables declared with my are lexically scoped. They are visible from the point of the declaration to the end of the current {}-enclosed block (or the current file, in case the declaration is outside a block). Subroutine parameters are visible in the signature and block of the subroutine.
An optional twigil between the sigil and identifier can influence the scoping. The * twigil marks a dynamically scoped variable; thus, lookup is performed in the current call stack. ! marks attributes, that is, a per-instance variable that’s attached to an object.
8.2 Subroutines
The variables used in the signature are called parameters, whereas we call the values that you pass in arguments.
because there’s no ambiguity here.
When you have a sub called MAIN, Raku uses its signature to parse the command-line arguments and pass those command-line arguments to MAIN.
multi subs are several subroutines with the same name but different signatures. The compiler decides at runtime which of the candidates it calls based on the best match between arguments and parameters.
8.3 Classes and Objects
When you declare attributes with the dot twigil, you can also initialize the attributes from the constructor through named arguments, as in OutputCapture.new( lines => [42] ).
Private methods start with a ! and can only be called from inside the class body as self!private-method.
Methods are basically subroutines, with two differences. The first is that they get an implicit parameter called self, which contains the object the method is called on (which we call the invocant). The second is that if you call a subroutine, the compiler searches for this subroutine in the current lexical scope as well as the outer scopes. On the other hand, method calls are looked up only in the class of the object and its superclasses.
The subroutine lookup can happen at compile time, because lexical scopes are immutable at runtime, so the compiler has knowledge of all lexical symbols. However, even in the presence of type constraints, the compiler can’t know if the type of an object is possibly a subtype of a type constraint, which means method lookups must be deferred to runtime.
8.4 Concurrency
Raku provides high-level primitives for concurrency and parallel execution. Instead of explicitly spawning new threads, you are encouraged to run a computation with start, which returns a Promise.1 This is an object that promises a future computation will yield a result. The status can thus be Planned, Kept, or Broken. You can chain promises, combine them, and wait for them.
In the background, a scheduler distributes such computations to operating system–level threads. The default scheduler is a thread pool scheduler with an upper limit to the number of threads available for use.
Communication between parallel computations should happen through thread-safe data structures. Foremost among them are Channel2 (a thread-safe queue) and Supply3 (Raku's implementation of the Observer Pattern4). Supplies are very powerful, because you can transform them with methods such as map, grep, throttle, or delayed and use their actor semantic5 to ensure that a consumer is run in only one thread at a time.
8.5 Outlook
When you understand the topics discussed in this chapter, and dig a bit into the built-in types, you should be familiar with the basics of Raku and be able to write your own programs.
Next we will look into one of the strengths of Raku: parsing, via regexes and grammars.