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

5

Comments

  1. BEGIN { 2 + 3 } runs during compilation and yields 5. Used as an expression, a BEGIN block hands back its value, which is assigned to $compiled.

  2. The computation happens only once, at compile time; by run time $compiled simply holds the constant 5. This is how BEGIN is used to precompute values, not just to print early.

Course navigation

Begin first   |   The order of ENDs