Course of Raku / Essentials

The MAIN function

A Raku program does not need any boilerplate ritual, so you can immediately start writing useful instructions. This part of the program, which is located on the level above any code block (including functions, for example), is called mainline.

If the program contains a function with the special name MAIN, Raku executes this function after it compiles everything and after the mainline code has been executed.

say '1. Mainline code';

sub MAIN {
    say '3. MAIN called';
}

say '2. Also mainline';

This program first executes the top-level says, and then calls MAIN:

$ raku t.raku
1. Mainline code
2. Also mainline
3. MAIN called

Of course, it is also possible to have a program with the MAIN function and no other mainline code.

Exercises

This section contains 3 exercises. Examine all the topics of this section before going to the coding practice.

  1. Hello, World!
  2. Print the sum
  3. Perimeter of a rectangle

Course navigation

Built-in functions for printing / note   |   Reading command-line arguments