Skip to content

Commit 30231bc

Browse files
committed
[variables] expand scope
talk about variables in general, not just special variables. Adds a section about sigils, and explain the * twigil
1 parent 933934b commit 30231bc

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

lib/variables.pod

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,80 @@
11
=begin pod
22
3-
=head1 perlvar - list of known magical variables
3+
=TITLE Perl 6 variables
4+
5+
Variable names start with a special character called a I<sigil>, followed
6+
optionally by a second special character named C<twigil>, and then an
7+
identifer.
8+
9+
=head1 Sigils
10+
11+
The sigil serves both as rough type constraint, and as an indicator
12+
as to whether the contents of the variable flatten in list context. See also
13+
the documentation in L<List>.
14+
15+
=begin table
16+
17+
Sigil Type constraint Default type Flattens
18+
===== =============== ============ ========
19+
$ Mu (no type constraint) Any No
20+
& Callable Callable No
21+
@ Positional Array Yes
22+
% Associative Hash Yes
23+
24+
=end table
25+
26+
Examples:
27+
28+
my $square = 9 ** 2;
29+
my @array = 1, 2, 3; # Array-variable with three elements
30+
my %hash = London => 'UK', Berlin => 'Germany';
31+
32+
Assignment to C<%> and C<@>-sigiled variables is special-cased
33+
syntactically. Instead of replacing the variable on the left-hand side with
34+
those of the right-hand side, it empties the variable, and fills it
35+
again with the values from the right-hand side.
36+
37+
my $item = (1, 2); say $item.WHAT; # Parcel()
38+
my @arr = (1, 2); say @arr.WHAT; # Array()
39+
my %hash = (1, 2); asy %hash.WHAT; # Hash()
40+
41+
=head1 Twigils
42+
43+
Twigils influence the scoping of a variable
44+
45+
=begin table
46+
47+
Twigil Scope
48+
====== =====
49+
* dynamic
50+
! attribute (class member)
51+
? compile-time "constant"
52+
. method (not really a variable)
53+
< index into match object (not really a variable)
54+
55+
=end table
56+
57+
=head2 *
58+
59+
Dynamic variables are looked up through the caller, not through the outer
60+
scope.
61+
62+
my $l = 23;
63+
my $*d = 23;
64+
65+
sub f() {
66+
say $l; # 23
67+
say $*d; # 42
68+
}
69+
70+
{
71+
my $l = 42;
72+
my $*d = 42;
73+
f();
74+
}
75+
76+
77+
=head1 Special Variables
478
579
=head2 Often-Used Variables
680

0 commit comments

Comments
 (0)