Skip to content

Commit d32a024

Browse files
nicqrocksAlexDaniel
authored andcommitted
Add ^ twigil to traps. Fixes #364 (#964)
This commit adds documentation to help clarify troubles with the ^ twigil.
1 parent 76bfdd9 commit d32a024

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/Language/traps.pod6

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,32 @@ throws. Explicitly sinking it inside the C<try> avoids the issue:
445445
say "still alive";
446446
# OUTPUT: still alive
447447
448+
=head1 Using Shortcuts
449+
450+
=head2 The ^ twigil
451+
452+
Using the C<^> twigil can save a fair amount of time and space when writing out small blocks of code. As an example:
453+
454+
for 1..8 -> $a, $b { say $a + $b; }
455+
456+
can be shortened to just
457+
458+
for 1..8 { say $^a + $^b; }
459+
460+
The trouble arises when a person wants to use more complex names for the variables, instead of just one letter. The C<^> twigil is able to have the positional variables be out of order and named whatever you want, but assigns values based on the variable's Unicode ordering. In the above example, we can have C<$^a> and C<$^b> switch places, and those variables will keep their positional values. This is beacuse the Unicode character 'a' comes before the character 'b'. For example:
461+
462+
#In order
463+
sub f1 { say "$^first $^second"; }
464+
f1 "Hello", "there"; #"Hello There"
465+
466+
#Out of order
467+
sub f2 { say "$^second $^first"; }
468+
f2 "Hello", "there"; #"there Hello"
469+
470+
Due to the variables allowed to be called anything, this can cause some problems if you are not accustomed to how Perl6 handles these variables.
471+
472+
for 1..4 { say "$^one $^two $^three $^four"; } # 2 4 3 1
473+
448474
=end pod
449475

450476
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)