When "take" is called on a variable that is later modified, is the Capture returned by the enclosing "gather" supposed to reflect the change or not? I know that Captures are the Perl 6 equivalent of references, but it seems natural to expect the contents to be copied by value at some point prior to the variables being modified.

As an example, what is the output of the below program supposed to be? On Rakudo (which doesn't seem to support multidimensional captures properly), it's "[20, 21, 22, 20, 21, 22, 20, 21, 22, 20, 21, 22]" instead of the desired "[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]". I could get around this problem by copying the variable before "take"-ing it, e.g., writing "take [ @things ]" instead of "take @things"; likewise, when writing similar code that "take"s string variables, copying could be performed by enclosing the variable in double quotes. However, for more complex datatypes, is there an easier (i.e., shorter) copying mechanism than assigning the value to a new variable that is scoped to the current block? Is this even necessary, i.e., is the observed behavior just a bug in Rakudo, or is that too much to hope for?

#!/usr/bin/env perl6
use v6;
my @stuff = gather {
 my @things;
 for ^23 {
  @things.push: $_;
  if @things == 5 {
   take @things;
   @things = ();
say @stuff.perl;

-- Minimiscience

Reply via email to