@@ -162,7 +162,17 @@ More detailed information about Perl syntax can be found in L<perlsyn>.
162
162
163
163
=head2 Perl variable types
164
164
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.
166
176
167
177
=over 4
168
178
@@ -195,17 +205,30 @@ it's set implicitly by certain looping constructs.
195
205
196
206
=item Arrays
197
207
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:
199
210
200
211
my @animals = ("camel", "llama", "owl");
201
212
my @numbers = (23, 42, 69);
202
213
my @mixed = ("camel", 42, 1.23);
203
214
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:
205
220
206
221
print $animals[0]; # prints "camel"
207
222
print $animals[1]; # prints "llama"
208
223
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
+
209
232
The special variable C<$#array> tells you the index of the last element
210
233
of an array:
211
234
@@ -228,7 +251,8 @@ To get multiple values from an array:
228
251
@animals[0..2]; # gives ("camel", "llama", "owl");
229
252
@animals[1..$#animals]; # gives all except the first element
230
253
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.
232
256
233
257
You can do various useful things to lists:
234
258
@@ -241,7 +265,8 @@ subroutine). These are documented in L<perlvar>.
241
265
242
266
=item Hashes
243
267
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:
245
270
246
271
my %fruit_color = ("apple", "red", "banana", "yellow");
247
272
@@ -251,13 +276,27 @@ nicely:
251
276
my %fruit_color = (
252
277
apple => "red",
253
278
banana => "yellow",
279
+ lime => "green",
254
280
);
255
281
256
282
To get at hash elements:
257
283
258
284
$fruit_color{"apple"}; # gives "red"
259
285
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
261
300
C<values()>.
262
301
263
302
my @fruits = keys %fruit_color;
@@ -275,6 +314,15 @@ L<perlvar>.
275
314
276
315
Scalars, arrays and hashes are documented more fully in L<perldata>.
277
316
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
+
278
326
More complex data types can be constructed using references, which allow
279
327
you to build lists and hashes within lists and hashes.
280
328
0 commit comments