Course of Raku / Advanced / Control flow / Statement prefixes 🆕 / Exercises / Silence a warning

Solution: Silence a warning

Here is a possible solution to the task.

Code

my $name;

my $greeting = quietly { "Hello, " ~ $name ~ "!" };
say $greeting;

🦋 You can find the source code in the file silence-warning.raku.

Output

Hello, !

Comments

  1. Interpolating the undefined $name into the string normally triggers a “use of uninitialized value” warning. Wrapping the expression in quietly suppresses it, so only the greeting is printed.

  2. Like do, quietly returns the value of its block, so the assembled string (with the missing name leaving an empty gap) is stored in $greeting.

  3. quietly only hides the warning — the value is still undefined. If instead you want to deal with the missing value, supply a default with the defined-or operator //: $name // 'friend' yields 'friend' when $name is undefined, so "Hello, " ~ ($name // 'friend') ~ "!" prints Hello, friend! with no warning at all.

Course navigation

Silence a warning   |   Subroutines