The problem here is that I do not understand well the difference between statements and blocks in control flow.
Looking the ternary operator I can use it to assign a variable. But this is an operator, so it is like applying a function--isn't it?
> my $variable = True ?? 34 !! 42;
34
since in the raku documentation says:
if
To conditionally run a block of code, use an
if
followed by a condition. The condition, an expression, will be evaluated immediately after the statement before theif
finishes. The block attached to the condition will only be evaluated if the condition meansTrue
when coerced to Bool. Unlike some languages the condition does not have to be parenthesized, instead the{
and}
around the block are mandatory:
do
The simplest way to run a block where it cannot be a stand-alone statement is by writing
do
before it:
so this should work in both cases:
> my $variable = do {34};
34
> my $variable = if True {34;} else {43;}
===SORRY!===
Word 'if' interpreted as a listop; please use 'do if' to introduce the statement control word
------> my $variable = if⏏ True {34;} else {43;}
Unexpected block in infix position (two terms in a row)
------> my $variable = if True⏏ {34;} else {43;}
as said in the error I need to add the do
:
> my $variable = do if True {34;} else {43;}
34
So the if
really does not run the block...or what is the real problem here?