10

The following line declares a variable and binds it to the number on the right-hand side.

my $a := 42;

The effect is that $a is not a Scalar, but an Int, as can be seen by

say $a.VAR.^name;

My question is, can I bind multiple variables in one declaration? This doesn't work:

my ($a, $b) := 17, 42;

because, as can be seen using say $a.VAR.^name, both $a and $b are Scalars now. (I think I understand why this happens, the question is whether there is a different approach that would bind both $a and $b to given Ints without creating Scalars.)

Furthermore, is there any difference between using := and = in this case?

Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
  • 2
    You bind the left hand side to the right hand side. In the second case, you're binding a list to another. The list elements are assigned, not bound, the elements on the right hand side. Binding is not recursive. Making it so would probably involve using map or somesuch, but not the assignment statement itself. And no, there's no difference. – jjmerelo Jun 27 '21 at 16:05

1 Answers1

9

You can use sigilless containers:

my (\a, \b) := 17,42;
say a.VAR.^name; # Int

Sigilless variables do not have an associated container

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
  • 2
    Interestingly, if I use `my (\a, \b) = 17, 42` (assignment, not binding), then `say a.VAR.^name` prints `Scalar`. What happens there? – Nikola Benes Jun 27 '21 at 18:27
  • 1
    [:=] my \a, my \b, 17 #Cannot reduce with := because list assignment operators are too fiddly – Holli Jun 27 '21 at 19:25