Hmm.. the statement my @a; is still redeclaring the @a variable so there is no accumulation happening in my testing of your suggestion. So this means it is still executed as a block? The my @a is persistent across input lines in the one liner program. This is the difference I can't understand. I'm testing on Rakudo 2018.12 if that makes any difference.
– Ruben WesterbergJan 25 at 22:10

@RubenWesterberg I use the absolute latest version of Rakudo. I update daily. There were some optimizations that may cause some differences.
– Brad GilbertJan 26 at 2:43

I finally got a chance to look at this again. I can replicate the command line behaviour now, using the postfix form of the 'for' statement as you suggested @BradGilbert. (#code goes here) for lines(); The block phasers don't execute as it isn't a block. Interestingly if I assign an initial value to the @a variable when declared, the accumulation again stops. i.e my @a=(0,0); vs my @a;.
– Ruben WesterbergJan 29 at 4:48

1

@RubenWesterberg my @a = 0,0 is effectively two separate things. A declaration my @a and an assignment = 0,0. The declaration adds the variable to the symbol table of the given scope at compile-time. The assignment is code that is run each time that line is reached.
– Brad GilbertJan 29 at 14:34

The ENTER and LEAVE Phasers are executed each time for a block. Running a for statement around the one-liner parts causes the phasers to run for each line as is the declaration of the variable @a. <br/> for $*ARGFILES.lines { my @a; .split(",").kv.map: {say $^v;@a[$^k]+=$v}; say @a; ENTER {say "ENTER"}; BEGIN {say "BEGIN"}; LEAVE {say "LEAVE"}; END {say "END"}; }<br/> Running with the same input as originally gives: BEGIN 1,1 ENTER 1 1 [1 1] LEAVE 1,1 ENTER 1 1 [1 1] LEAVE END
– Ruben WesterbergJan 25 at 8:24

@RubenWesterberg Yes you are right! I will correct my answer..
– Håkon HæglandJan 25 at 8:31

First time for everything :) . The only way I can make sense of it currently is if the my @a is actually made into state @a , which is only initialised once the first time the block is executed. Thus for next execution the variable already exists and the accumulation part works.
– Ruben WesterbergJan 25 at 8:43

I see now that I missed an important part of you question: when reading arguments from STDIN, the ENTER and LEAVE seem not to be called for each line read, as you also observe.. I missed this part since I was testing this with input from file like this perl6 test.p6 file.csv, and in this case the phasers was called for each line.. I am not sure what is going on here, maybe the ENTER and LEAVE phasers are associated with a block outside the for loop somehow?
– Håkon HæglandJan 25 at 8:54