Course of Raku / Advanced / Control flow / Phasers / Exercises / Begin first
Solution: Begin first
Here is a possible solution to the task.
Code
my $compiled = BEGIN { 2 + 3 };
say $compiled;🦋 You can find the source code in the file begin-first.raku.
Output
5Comments
BEGIN { 2 + 3 }runs during compilation and yields5. Used as an expression, aBEGINblock hands back its value, which is assigned to$compiled.The computation happens only once, at compile time; by run time
$compiledsimply holds the constant5. This is howBEGINis used to precompute values, not just to print early.
Course navigation
← Begin first | The order of ENDs →