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