1PERLREFTUT(1) Perl Programmers Reference Guide PERLREFTUT(1)
2
3
4
6 perlreftut - Mark's very short tutorial about references
7
9 One of the most important new features in Perl 5 was the capability to
10 manage complicated data structures like multidimensional arrays and
11 nested hashes. To enable these, Perl 5 introduced a feature called
12 references, and using references is the key to managing complicated,
13 structured data in Perl. Unfortunately, there's a lot of funny syntax
14 to learn, and the main manual page can be hard to follow. The manual
15 is quite complete, and sometimes people find that a problem, because it
16 can be hard to tell what is important and what isn't.
17
18 Fortunately, you only need to know 10% of what's in the main page to
19 get 90% of the benefit. This page will show you that 10%.
20
22 One problem that comes up all the time is needing a hash whose values
23 are lists. Perl has hashes, of course, but the values have to be
24 scalars; they can't be lists.
25
26 Why would you want a hash of lists? Let's take a simple example: You
27 have a file of city and country names, like this:
28
29 Chicago, USA
30 Frankfurt, Germany
31 Berlin, Germany
32 Washington, USA
33 Helsinki, Finland
34 New York, USA
35
36 and you want to produce an output like this, with each country
37 mentioned once, and then an alphabetical list of the cities in that
38 country:
39
40 Finland: Helsinki.
41 Germany: Berlin, Frankfurt.
42 USA: Chicago, New York, Washington.
43
44 The natural way to do this is to have a hash whose keys are country
45 names. Associated with each country name key is a list of the cities
46 in that country. Each time you read a line of input, split it into a
47 country and a city, look up the list of cities already known to be in
48 that country, and append the new city to the list. When you're done
49 reading the input, iterate over the hash as usual, sorting each list of
50 cities before you print it out.
51
52 If hash values couldn't be lists, you lose. You'd probably have to
53 combine all the cities into a single string somehow, and then when time
54 came to write the output, you'd have to break the string into a list,
55 sort the list, and turn it back into a string. This is messy and
56 error-prone. And it's frustrating, because Perl already has perfectly
57 good lists that would solve the problem if only you could use them.
58
60 By the time Perl 5 rolled around, we were already stuck with this
61 design: Hash values must be scalars. The solution to this is
62 references.
63
64 A reference is a scalar value that refers to an entire array or an
65 entire hash (or to just about anything else). Names are one kind of
66 reference that you're already familiar with. Each human being is a
67 messy, inconvenient collection of cells. But to refer to a particular
68 human, for instance the first computer programmer, it isn't necessary
69 to describe each of their cells; all you need is the easy, convenient
70 scalar string "Ada Lovelace".
71
72 References in Perl are like names for arrays and hashes. They're
73 Perl's private, internal names, so you can be sure they're unambiguous.
74 Unlike a human name, a reference only refers to one thing, and you
75 always know what it refers to. If you have a reference to an array,
76 you can recover the entire array from it. If you have a reference to a
77 hash, you can recover the entire hash. But the reference is still an
78 easy, compact scalar value.
79
80 You can't have a hash whose values are arrays; hash values can only be
81 scalars. We're stuck with that. But a single reference can refer to
82 an entire array, and references are scalars, so you can have a hash of
83 references to arrays, and it'll act a lot like a hash of arrays, and
84 it'll be just as useful as a hash of arrays.
85
86 We'll come back to this city-country problem later, after we've seen
87 some syntax for managing references.
88
90 There are just two ways to make a reference, and just two ways to use
91 it once you have it.
92
93 Making References
94 Make Rule 1
95
96 If you put a "\" in front of a variable, you get a reference to that
97 variable.
98
99 $aref = \@array; # $aref now holds a reference to @array
100 $href = \%hash; # $href now holds a reference to %hash
101 $sref = \$scalar; # $sref now holds a reference to $scalar
102
103 Once the reference is stored in a variable like $aref or $href, you can
104 copy it or store it just the same as any other scalar value:
105
106 $xy = $aref; # $xy now holds a reference to @array
107 $p[3] = $href; # $p[3] now holds a reference to %hash
108 $z = $p[3]; # $z now holds a reference to %hash
109
110 These examples show how to make references to variables with names.
111 Sometimes you want to make an array or a hash that doesn't have a name.
112 This is analogous to the way you like to be able to use the string "\n"
113 or the number 80 without having to store it in a named variable first.
114
115 Make Rule 2
116
117 "[ ITEMS ]" makes a new, anonymous array, and returns a reference to
118 that array. "{ ITEMS }" makes a new, anonymous hash, and returns a
119 reference to that hash.
120
121 $aref = [ 1, "foo", undef, 13 ];
122 # $aref now holds a reference to an array
123
124 $href = { APR => 4, AUG => 8 };
125 # $href now holds a reference to a hash
126
127 The references you get from rule 2 are the same kind of references that
128 you get from rule 1:
129
130 # This:
131 $aref = [ 1, 2, 3 ];
132
133 # Does the same as this:
134 @array = (1, 2, 3);
135 $aref = \@array;
136
137 The first line is an abbreviation for the following two lines, except
138 that it doesn't create the superfluous array variable @array.
139
140 If you write just "[]", you get a new, empty anonymous array. If you
141 write just "{}", you get a new, empty anonymous hash.
142
143 Using References
144 What can you do with a reference once you have it? It's a scalar
145 value, and we've seen that you can store it as a scalar and get it back
146 again just like any scalar. There are just two more ways to use it:
147
148 Use Rule 1
149
150 You can always use an array reference, in curly braces, in place of the
151 name of an array. For example, "@{$aref}" instead of @array.
152
153 Here are some examples of that:
154
155 Arrays:
156
157 @a @{$aref} An array
158 reverse @a reverse @{$aref} Reverse the array
159 $a[3] ${$aref}[3] An element of the array
160 $a[3] = 17; ${$aref}[3] = 17 Assigning an element
161
162 On each line are two expressions that do the same thing. The left-hand
163 versions operate on the array @a. The right-hand versions operate on
164 the array that is referred to by $aref. Once they find the array
165 they're operating on, both versions do the same things to the arrays.
166
167 Using a hash reference is exactly the same:
168
169 %h %{$href} A hash
170 keys %h keys %{$href} Get the keys from the hash
171 $h{'red'} ${$href}{'red'} An element of the hash
172 $h{'red'} = 17 ${$href}{'red'} = 17 Assigning an element
173
174 Whatever you want to do with a reference, Use Rule 1 tells you how to
175 do it. You just write the Perl code that you would have written for
176 doing the same thing to a regular array or hash, and then replace the
177 array or hash name with "{$reference}". "How do I loop over an array
178 when all I have is a reference?" Well, to loop over an array, you
179 would write
180
181 for my $element (@array) {
182 ...
183 }
184
185 so replace the array name, @array, with the reference:
186
187 for my $element (@{$aref}) {
188 ...
189 }
190
191 "How do I print out the contents of a hash when all I have is a
192 reference?" First write the code for printing out a hash:
193
194 for my $key (keys %hash) {
195 print "$key => $hash{$key}\n";
196 }
197
198 And then replace the hash name with the reference:
199
200 for my $key (keys %{$href}) {
201 print "$key => ${$href}{$key}\n";
202 }
203
204 Use Rule 2
205
206 Use Rule 1 is all you really need, because it tells you how to do
207 absolutely everything you ever need to do with references. But the
208 most common thing to do with an array or a hash is to extract a single
209 element, and the Use Rule 1 notation is cumbersome. So there is an
210 abbreviation.
211
212 "${$aref}[3]" is too hard to read, so you can write "$aref->[3]"
213 instead.
214
215 "${$href}{red}" is too hard to read, so you can write "$href->{red}"
216 instead.
217
218 If $aref holds a reference to an array, then "$aref->[3]" is the fourth
219 element of the array. Don't confuse this with $aref[3], which is the
220 fourth element of a totally different array, one deceptively named
221 @aref. $aref and @aref are unrelated the same way that $item and @item
222 are.
223
224 Similarly, "$href->{'red'}" is part of the hash referred to by the
225 scalar variable $href, perhaps even one with no name. $href{'red'} is
226 part of the deceptively named %href hash. It's easy to forget to leave
227 out the "->", and if you do, you'll get bizarre results when your
228 program gets array and hash elements out of totally unexpected hashes
229 and arrays that weren't the ones you wanted to use.
230
231 An Example
232 Let's see a quick example of how all this is useful.
233
234 First, remember that "[1, 2, 3]" makes an anonymous array containing
235 "(1, 2, 3)", and gives you a reference to that array.
236
237 Now think about
238
239 @a = ( [1, 2, 3],
240 [4, 5, 6],
241 [7, 8, 9]
242 );
243
244 @a is an array with three elements, and each one is a reference to
245 another array.
246
247 $a[1] is one of these references. It refers to an array, the array
248 containing "(4, 5, 6)", and because it is a reference to an array, Use
249 Rule 2 says that we can write $a[1]->[2] to get the third element from
250 that array. $a[1]->[2] is the 6. Similarly, $a[0]->[1] is the 2.
251 What we have here is like a two-dimensional array; you can write
252 $a[ROW]->[COLUMN] to get or set the element in any row and any column
253 of the array.
254
255 The notation still looks a little cumbersome, so there's one more
256 abbreviation:
257
258 Arrow Rule
259 In between two subscripts, the arrow is optional.
260
261 Instead of $a[1]->[2], we can write $a[1][2]; it means the same thing.
262 Instead of "$a[0]->[1] = 23", we can write "$a[0][1] = 23"; it means
263 the same thing.
264
265 Now it really looks like two-dimensional arrays!
266
267 You can see why the arrows are important. Without them, we would have
268 had to write "${$a[1]}[2]" instead of $a[1][2]. For three-dimensional
269 arrays, they let us write $x[2][3][5] instead of the unreadable
270 "${${$x[2]}[3]}[5]".
271
273 Here's the answer to the problem I posed earlier, of reformatting a
274 file of city and country names.
275
276 1 my %table;
277
278 2 while (<>) {
279 3 chomp;
280 4 my ($city, $country) = split /, /;
281 5 $table{$country} = [] unless exists $table{$country};
282 6 push @{$table{$country}}, $city;
283 7 }
284
285 8 for my $country (sort keys %table) {
286 9 print "$country: ";
287 10 my @cities = @{$table{$country}};
288 11 print join ', ', sort @cities;
289 12 print ".\n";
290 13 }
291
292 The program has two pieces: Lines 2-7 read the input and build a data
293 structure, and lines 8-13 analyze the data and print out the report.
294 We're going to have a hash, %table, whose keys are country names, and
295 whose values are references to arrays of city names. The data
296 structure will look like this:
297
298 %table
299 +-------+---+
300 | | | +-----------+--------+
301 |Germany| *---->| Frankfurt | Berlin |
302 | | | +-----------+--------+
303 +-------+---+
304 | | | +----------+
305 |Finland| *---->| Helsinki |
306 | | | +----------+
307 +-------+---+
308 | | | +---------+------------+----------+
309 | USA | *---->| Chicago | Washington | New York |
310 | | | +---------+------------+----------+
311 +-------+---+
312
313 We'll look at output first. Supposing we already have this structure,
314 how do we print it out?
315
316 8 for my $country (sort keys %table) {
317 9 print "$country: ";
318 10 my @cities = @{$table{$country}};
319 11 print join ', ', sort @cities;
320 12 print ".\n";
321 13 }
322
323 %table is an ordinary hash, and we get a list of keys from it, sort the
324 keys, and loop over the keys as usual. The only use of references is
325 in line 10. $table{$country} looks up the key $country in the hash and
326 gets the value, which is a reference to an array of cities in that
327 country. Use Rule 1 says that we can recover the array by saying
328 "@{$table{$country}}". Line 10 is just like
329
330 @cities = @array;
331
332 except that the name "array" has been replaced by the reference
333 "{$table{$country}}". The "@" tells Perl to get the entire array.
334 Having gotten the list of cities, we sort it, join it, and print it out
335 as usual.
336
337 Lines 2-7 are responsible for building the structure in the first
338 place. Here they are again:
339
340 2 while (<>) {
341 3 chomp;
342 4 my ($city, $country) = split /, /;
343 5 $table{$country} = [] unless exists $table{$country};
344 6 push @{$table{$country}}, $city;
345 7 }
346
347 Lines 2-4 acquire a city and country name. Line 5 looks to see if the
348 country is already present as a key in the hash. If it's not, the
349 program uses the "[]" notation (Make Rule 2) to manufacture a new,
350 empty anonymous array of cities, and installs a reference to it into
351 the hash under the appropriate key.
352
353 Line 6 installs the city name into the appropriate array.
354 $table{$country} now holds a reference to the array of cities seen in
355 that country so far. Line 6 is exactly like
356
357 push @array, $city;
358
359 except that the name "array" has been replaced by the reference
360 "{$table{$country}}". The "push" adds a city name to the end of the
361 referred-to array.
362
363 There's one fine point I skipped. Line 5 is unnecessary, and we can
364 get rid of it.
365
366 2 while (<>) {
367 3 chomp;
368 4 my ($city, $country) = split /, /;
369 5 #### $table{$country} = [] unless exists $table{$country};
370 6 push @{$table{$country}}, $city;
371 7 }
372
373 If there's already an entry in %table for the current $country, then
374 nothing is different. Line 6 will locate the value in
375 $table{$country}, which is a reference to an array, and push $city into
376 the array. But what does it do when $country holds a key, say
377 "Greece", that is not yet in %table?
378
379 This is Perl, so it does the exact right thing. It sees that you want
380 to push "Athens" onto an array that doesn't exist, so it helpfully
381 makes a new, empty, anonymous array for you, installs it into %table,
382 and then pushes "Athens" onto it. This is called
383 autovivification--bringing things to life automatically. Perl saw that
384 the key wasn't in the hash, so it created a new hash entry
385 automatically. Perl saw that you wanted to use the hash value as an
386 array, so it created a new empty array and installed a reference to it
387 in the hash automatically. And as usual, Perl made the array one
388 element longer to hold the new city name.
389
391 I promised to give you 90% of the benefit with 10% of the details, and
392 that means I left out 90% of the details. Now that you have an
393 overview of the important parts, it should be easier to read the
394 perlref manual page, which discusses 100% of the details.
395
396 Some of the highlights of perlref:
397
398 • You can make references to anything, including scalars, functions,
399 and other references.
400
401 • In Use Rule 1, you can omit the curly brackets whenever the thing
402 inside them is an atomic scalar variable like $aref. For example,
403 @$aref is the same as "@{$aref}", and $$aref[1] is the same as
404 "${$aref}[1]". If you're just starting out, you may want to adopt
405 the habit of always including the curly brackets.
406
407 • This doesn't copy the underlying array:
408
409 $aref2 = $aref1;
410
411 You get two references to the same array. If you modify
412 "$aref1->[23]" and then look at "$aref2->[23]" you'll see the
413 change.
414
415 To copy the array, use
416
417 $aref2 = [@{$aref1}];
418
419 This uses "[...]" notation to create a new anonymous array, and
420 $aref2 is assigned a reference to the new array. The new array is
421 initialized with the contents of the array referred to by $aref1.
422
423 Similarly, to copy an anonymous hash, you can use
424
425 $href2 = {%{$href1}};
426
427 • To see if a variable contains a reference, use the "ref" function.
428 It returns true if its argument is a reference. Actually it's a
429 little better than that: It returns "HASH" for hash references and
430 "ARRAY" for array references.
431
432 • If you try to use a reference like a string, you get strings like
433
434 ARRAY(0x80f5dec) or HASH(0x826afc0)
435
436 If you ever see a string that looks like this, you'll know you
437 printed out a reference by mistake.
438
439 A side effect of this representation is that you can use "eq" to
440 see if two references refer to the same thing. (But you should
441 usually use "==" instead because it's much faster.)
442
443 • You can use a string as if it were a reference. If you use the
444 string "foo" as an array reference, it's taken to be a reference to
445 the array @foo. This is called a symbolic reference. The
446 declaration "use strict 'refs'" disables this feature, which can
447 cause all sorts of trouble if you use it by accident.
448
449 You might prefer to go on to perllol instead of perlref; it discusses
450 lists of lists and multidimensional arrays in detail. After that, you
451 should move on to perldsc; it's a Data Structure Cookbook that shows
452 recipes for using and printing out arrays of hashes, hashes of arrays,
453 and other kinds of data.
454
456 Everyone needs compound data structures, and in Perl the way you get
457 them is with references. There are four important rules for managing
458 references: Two for making references and two for using them. Once you
459 know these rules you can do most of the important things you need to do
460 with references.
461
463 Author: Mark Jason Dominus, Plover Systems ("mjd-perl-ref+@plover.com")
464
465 This article originally appeared in The Perl Journal (
466 <http://www.tpj.com/> ) volume 3, #2. Reprinted with permission.
467
468 The original title was Understand References Today.
469
470 Distribution Conditions
471 Copyright 1998 The Perl Journal.
472
473 This documentation is free; you can redistribute it and/or modify it
474 under the same terms as Perl itself.
475
476 Irrespective of its distribution, all code examples in these files are
477 hereby placed into the public domain. You are permitted and encouraged
478 to use this code in your own programs for fun or for profit as you see
479 fit. A simple comment in the code giving credit would be courteous but
480 is not required.
481
482
483
484perl v5.36.0 2022-08-30 PERLREFTUT(1)