Course of Raku / Advanced / Containers / Ordered containers / Exercises / Bind a scalar

Solution: Bind a scalar

Here is a possible solution to the task.

Code

my $source = 1;
my $bound := $source;
my $copy = $source;

$source = 9;
say $bound;
say $copy;

🦋 You can find the source code in the file bind-a-scalar.raku.

Output

9
1

Comments

  1. Binding with := makes $bound another name for the same container as $source, rather than a copy. So changing $source is visible through $bound, which prints 9.

  2. Ordinary assignment with = copies the value into a separate container, so $copy is unaffected by the later change and still prints 1.

  3. Seeing the two side by side is the whole point: := shares a container, = duplicates the value.

Course navigation

Bind a scalar   |   Contexts 🆕