In the following example I try to create a Capture dynamically by "converting" an array (@a) to a Capture.
Consider the code:
sub f (|c){
say '';
say ' List : ' ~ do {c.list.gist if c.list.elems > 0};
say ' Hash : ' ~ do {c.hash.gist if c.hash.elems > 0};
say '';
}
my $c1 = \(1,(2,3),4,5, :t1('test1'), 6,7, :t2('test2'), 8,9);
my @a = 1,(2,3),4,5, :t1('test1'), 6,7, :t2('test2'), 8,9;
my $c2 = \(|@a);
f(|$c1);
f(|@a);
f(|$c2);
The result is:
List : (1 (2 3) 4 5 6 7 8 9)
Hash : Map.new((t1 => test1, t2 => test2))
List : (1 (2 3) 4 5 t1 => test1 6 7 t2 => test2 8 9)
Hash :
List : (1 (2 3) 4 5 t1 => test1 6 7 t2 => test2 8 9)
Hash :
The first run (with Capture $c1) is running as it should, producing the desired behaviour. The second and third attempts, to create a Capture dynamically, are failing (probably because the argument of the subroutine f in those cases is NOT the desired Capture). I observe that the pairs incorporated into array @a, are taken to be members of a list and NOT named parameters as I wanted them to be.
I know that there must be, sort of speak, a "flattening" of the pairs in the array going on, before passing to subroutine f, but i can NOT figure out the way to do that!
Can anyone give me a hint?