On Mon, Aug 31, 2020 at 05:05:53PM -0700, yary wrote:
: I like this better for alpha counter
: 
: raku -e "for (1..4) { say (BEGIN $ = 'AAA')++ }"
: 
: with BEGIN, the assignment of AAA happens once. With the earlier ||= it
: checks each time through the loop.
: -y

Careful with that, though, since BEGIN/INIT happen only once ever (and in the
context of the top-level run), so the state variable acts more like a global:

    $ raku -e "for <a b> { for (1..2) { say (BEGIN $ = 'AAA')++ } }"
    AAA
    AAB
    AAC
    AAD

    $ raku -e "for <a b> { for (1..2) { say (INIT $ = 'AAA')++ } }"
    AAA
    AAB
    AAC
    AAD

If you want to re-initialize a state variable, it's probably better to make
it explicit with the state declarator:

    $ raku -e "for <a b> { for (1..2) { say (state $ = 'AAA')++ } }"
    AAA
    AAB
    AAA
    AAB

Larry

Reply via email to