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
Interpolating the undefined
$nameinto the string normally triggers a “use of uninitialized value” warning. Wrapping the expression inquietlysuppresses it, so only the greeting is printed.Like
do,quietlyreturns the value of its block, so the assembled string (with the missing name leaving an empty gap) is stored in$greeting.quietlyonly 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$nameis undefined, so"Hello, " ~ ($name // 'friend') ~ "!"printsHello, friend!with no warning at all.
Course navigation
← Silence a warning | Subroutines →