Course of Raku / Essentials / The MAIN function / Exercises / Perimeter of a rectangle

Solution: Perimeter of a rectangle

This program must be able to take either one or two command-line arguments. A new trick is shown in the solution here. The default value of the second variable is set to the value of the first variable: sub MAIN($a, $b = $a). So, instead of creating the two multi-functions, we have a general function that sets the second side’s size if the figure is a square.


Here is the solution:

sub MAIN($a, $b = $a) {
    my $perimeter = 2 * ($a + $b);

    my $shape = $a == $b ?? 'square' !! 'rectangle';
    say "The perimeter of a $shape is $perimeter.";

🦋 Find the program in the file perimeter.raku.


Try different input values to test both squares and rectangles.

$ raku exercises/the-main-function/perimeter.raku 1    
The perimeter of a square is 4.

$ raku exercises/the-main-function/perimeter.raku 1 2
The perimeter of a rectangle is 6.

Note that there is the third case which should also be tested. If two equal numbers are passed, the program should still understand that this was a square:

$ raku exercises/the-main-function/perimeter.raku 2 2
The perimeter of a square is 8.

Course navigation

Built-in functions for printing / note   |