Description
Here's a Raku puzzle: which of the following statements refer to the same block as statement 1 and which create new blocks?
{ dd &?BLOCK; # 1
if 1 > 0 { dd &?BLOCK}; # 2
dd &?BLOCK if 2 > 1; # 3
for (1) { dd &?BLOCK}; # 4
dd &?BLOCK for (1)} # 5
Based on the current documentation and my understanding that {...}
creates a new block, I strongly expected 1, 3, and 5 to refer to the same block and 2 and 4 to refer to new blocks. However, this is not the case.
Instead, 1, 2, and 3 refer to the same block, while 4 and 5 create a new block.
However, the same rules that apply to creating blocks for the purpose of &?BLOCK
do not apply to creating lexical scope blocks:
{ my $a = 1;
if 1 > 0 {
my $a = 2;
say $a; # OUTPUT «2»
say $OUTER::a # OUTPUT «1»
};
say $a # OUTPUT «1»
}
The existing docs do not distinguish between lexical scope and blocks, but apparently the two concepts are distinct at least in this case. The docs should make a clearer distinction here.
Suggestions
I am not sure when blocks are created and when they are not, but the documentation should be updated to reflect that blocks are not created whenever {...}
appears. Is the rule that blocks are created whenever the topic variable is changed?