Skip to content

Commit 104331a

Browse files
committed
Add docs for Scalar type
1 parent 867e294 commit 104331a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

doc/Type/Scalar.pod

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
=begin pod
2+
3+
=TITLE class Scalar
4+
5+
=SUBTITLE A mostly transparent container used for indirections
6+
7+
class Scalar { ... }
8+
9+
A C<Scalar> is an internal indirection which is for most purposes
10+
invisible during ordinary use of Perl 6. It is the default container
11+
type associated with the C<$> sigil. A literal C<Scalar> may be
12+
placed around a literal value by enclosing the value in C<$(…)>.
13+
This notation will appear in the output of a C<.perl> method in
14+
certain places where it is important to note the presence of C<Scalar>s.
15+
16+
When a value is assigned to a C<$>-sigiled variable, the variable will
17+
actually bind to a C<Scalar>, which in turn will bind to the value.
18+
When a C<Scalar> is assigned to a C<$-sigiled> variable, the value
19+
bound to by that C<Scalar> will be bound to the C<Scalar> which that
20+
variable was bound to (a new one will be created if necessary.)
21+
22+
In addition C<Scalar>s delegate all method calls to the value which
23+
they contain. As such, C<Scalar>s are for the most part invisible.
24+
There is, however, one important place where C<Scalar>s have a visible
25+
impact: a C<Scalar> will shield its contents from flattening by most
26+
Perl 6 core list operations.
27+
28+
A <$>-sigiled variable may be bound directly to a value with no
29+
intermediate C<Scalar> using the binding operator C<:=>. You can
30+
tell if this has been done by examining the introspective pseudo-method
31+
C<.VAR>:
32+
33+
my $a = 1;
34+
$a.WHAT.say; # says "(Int)"
35+
$a.VAR.WHAT.say; # says "(Scalar)"
36+
my $b := 1;
37+
$b.WHAT.say; # says "(Int)"
38+
$b.VAR.WHAT.say; # says "(Int)"
39+
40+
This same thing happens when values are assigned to an element of
41+
an C<Array>, however, C<List>s directly contain their values:
42+
43+
my @a = 1,2,3;
44+
@a[0].WHAT.say; # says "(Int)"
45+
@a[0].VAR.WHAT.say; # says "(Scalar)"
46+
[1,2,3][0].WHAT.say; # says "(Int)"
47+
[1,2,3][0].VAR.WHAT.say; # says "(Scalar)"
48+
(1,2,3)[0].WHAT.say; # says "(Int)"
49+
(1,2,3)[0].VAR.WHAT.say; # says "(Int)"
50+
51+
Array elements may be bound directly to values using C<:=> as well,
52+
however this is to be discouraged as it may lead to confusion.
53+
Doing so will break exact round-tripping of C<.perl> output -- since
54+
C<Array>s are assumed to place C<Scalar>s around each element,
55+
C<Scalar>s are not denoted with C<$> in the output of C<.perl> methods.
56+
57+
[1,$(2,3)].perl.say # says [1, (2, 3)]
58+
(1,$(2,3)).perl.say # says (1, $(2, 3))
59+
60+
Binding a Scalar to a C<$>-sigiled variable replaces the existing
61+
C<Scalar> in that variable, if any, with the given C<Scalar>.
62+
That means more than one variable may refer to the same C<Scalar>.
63+
Because the C<Scalar> may be mutated, this makes it possible to
64+
alter the value of both variables by altering only one of them:
65+
66+
my $a = 1;
67+
my $b := $a;
68+
$b = 2;
69+
$a.say; # says "2"
70+
71+
SSA-style constants bind directly to their value with no
72+
intervening C<Scalar>, even when C<=>/assignment is used. They may
73+
be forced to use a C<Scalar> by assigning a C<$>-sigiled variable
74+
to them, at which point, they behave entirely like C<$>-sigiled
75+
variables.
76+
77+
my \c = 1;
78+
c.WHAT.say; # says "(Int)"
79+
c.VAR.WHAT.say; # says "(Int)"
80+
my $a = 1;
81+
my \d = $a; # just my \d = $ = 1 works, too
82+
d.WHAT.say; # says "(Int)"
83+
d.VAR.WHAT.say; # says "(Scalar)"
84+
d = 2; # ok
85+
c = 2; # error, cannot modify an immutable Int
86+
87+
=end pod

0 commit comments

Comments
 (0)