Skip to content

Commit a7c61e7

Browse files
committed
perlintro: Add discussion of sigils
I noticed that nowhere in our documentation do we introduce the concept of sigils. This is something that distinguishes Perl from most other languages, so could easily be confusing to a newcomer.
1 parent ab01694 commit a7c61e7

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

pod/perlintro.pod

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,17 @@ More detailed information about Perl syntax can be found in L<perlsyn>.
162162

163163
=head2 Perl variable types
164164

165-
Perl has three main variable types: scalars, arrays, and hashes.
165+
You may have noticed the strange character string <$name> above. That
166+
denotes a variable, which is generally pronounced as "name". The dollar
167+
sign is called a "sigil", and indicates that "name" is a variable.
168+
Unlike many other computer languages where variables are just strings of
169+
typically only alphabetic and numeric characters, all Perl variables are
170+
preceded by a sigil.
171+
172+
Perl has three main variable types: scalars, arrays, and hashes. The
173+
particular sigil used for a variable name indicates the type of that
174+
variable. We've already seen the dollar sign used as a sigil, C<$name>.
175+
The dollar sign indicates this variable is a scalar.
166176

167177
=over 4
168178

@@ -195,17 +205,30 @@ it's set implicitly by certain looping constructs.
195205

196206
=item Arrays
197207

198-
An array represents a list of values:
208+
An array represents a list of values. A C<@> sigil indicates a variable
209+
is an array:
199210

200211
my @animals = ("camel", "llama", "owl");
201212
my @numbers = (23, 42, 69);
202213
my @mixed = ("camel", 42, 1.23);
203214

204-
Arrays are zero-indexed. Here's how you get at elements in an array:
215+
Each of these arrays is an aggregation of individual values. And each
216+
value is actually a scalar.
217+
218+
Arrays are zero-indexed. Here's how you get at the individual scalar
219+
elements in an array:
205220

206221
print $animals[0]; # prints "camel"
207222
print $animals[1]; # prints "llama"
208223

224+
In Perl, scalars are always referenced with the C<$> sigil. Thus we
225+
wrote C<$animals[0]> above to get the 0th scalar variable inside the
226+
C<@animals> array. The square brackets C<[...]> indicate that it is an
227+
array that we are getting the value from.
228+
This is a major point of confusion for newcomers to
229+
Perl. When you are looking at a single value, you need to use C<$>,
230+
even though that value is part of a larger aggregation.
231+
209232
The special variable C<$#array> tells you the index of the last element
210233
of an array:
211234

@@ -228,7 +251,8 @@ To get multiple values from an array:
228251
@animals[0..2]; # gives ("camel", "llama", "owl");
229252
@animals[1..$#animals]; # gives all except the first element
230253

231-
This is called an "array slice".
254+
This is called an "array slice". We used the C<@> sigil here because
255+
we want multiple values; C<$> is reserved for referencing single values.
232256

233257
You can do various useful things to lists:
234258

@@ -241,7 +265,8 @@ subroutine). These are documented in L<perlvar>.
241265

242266
=item Hashes
243267

244-
A hash represents a set of key/value pairs:
268+
A hash represents a set of key/value pairs. A C<%> sigil indicates a
269+
variable is a hash:
245270

246271
my %fruit_color = ("apple", "red", "banana", "yellow");
247272

@@ -251,13 +276,27 @@ nicely:
251276
my %fruit_color = (
252277
apple => "red",
253278
banana => "yellow",
279+
lime => "green",
254280
);
255281

256282
To get at hash elements:
257283

258284
$fruit_color{"apple"}; # gives "red"
259285

260-
You can get at lists of keys and values with C<keys()> and
286+
Note again the use of the C<$> sigil to access a single hash element.
287+
The curly braces C<{...}> indicate that it is an hash that we are
288+
getting the value from.
289+
290+
And we can get multiple values using a slice
291+
292+
my @non_red = @fruit_color{"banana", "lime"};
293+
print $non_red[0], "\n"; # yellow
294+
print $non_red[1], "\n"; # green
295+
296+
Note again that multiple values use the sigil C<@>, and single ones use
297+
C<$>.
298+
299+
You can get at lists of the keys and values with C<keys()> and
261300
C<values()>.
262301

263302
my @fruits = keys %fruit_color;
@@ -275,6 +314,15 @@ L<perlvar>.
275314

276315
Scalars, arrays and hashes are documented more fully in L<perldata>.
277316

317+
Note that C<$name>, C<@name>, and C<%name> are three different
318+
variables. It can be confusing to use the same alphanumerics to
319+
represent different variables, but in some situations it might be
320+
clarifying; one such could be to use C<$name> to represent the
321+
particular element currently of interest within the array C<@name>.
322+
The naming choice is yours; the Perl interpreter doesn't care.
323+
When multiple variables have the same name, using square brackets versus
324+
curly braces tells the interpreter which one is meant.
325+
278326
More complex data types can be constructed using references, which allow
279327
you to build lists and hashes within lists and hashes.
280328

0 commit comments

Comments
 (0)