| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlreftut - Mark's very short tutorial about references
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 | One of the most important new features in Perl 5 was the capability to
|
|---|
| 8 | manage complicated data structures like multidimensional arrays and
|
|---|
| 9 | nested hashes. To enable these, Perl 5 introduced a feature called
|
|---|
| 10 | `references', and using references is the key to managing complicated,
|
|---|
| 11 | structured data in Perl. Unfortunately, there's a lot of funny syntax
|
|---|
| 12 | to learn, and the main manual page can be hard to follow. The manual
|
|---|
| 13 | is quite complete, and sometimes people find that a problem, because
|
|---|
| 14 | it can be hard to tell what is important and what isn't.
|
|---|
| 15 |
|
|---|
| 16 | Fortunately, you only need to know 10% of what's in the main page to get
|
|---|
| 17 | 90% of the benefit. This page will show you that 10%.
|
|---|
| 18 |
|
|---|
| 19 | =head1 Who Needs Complicated Data Structures?
|
|---|
| 20 |
|
|---|
| 21 | One problem that came up all the time in Perl 4 was how to represent a
|
|---|
| 22 | hash whose values were lists. Perl 4 had hashes, of course, but the
|
|---|
| 23 | values had to be scalars; they couldn't be lists.
|
|---|
| 24 |
|
|---|
| 25 | Why would you want a hash of lists? Let's take a simple example: You
|
|---|
| 26 | have a file of city and country names, like this:
|
|---|
| 27 |
|
|---|
| 28 | Chicago, USA
|
|---|
| 29 | Frankfurt, Germany
|
|---|
| 30 | Berlin, Germany
|
|---|
| 31 | Washington, USA
|
|---|
| 32 | Helsinki, Finland
|
|---|
| 33 | New York, USA
|
|---|
| 34 |
|
|---|
| 35 | and you want to produce an output like this, with each country mentioned
|
|---|
| 36 | once, and then an alphabetical list of the cities in that country:
|
|---|
| 37 |
|
|---|
| 38 | Finland: Helsinki.
|
|---|
| 39 | Germany: Berlin, Frankfurt.
|
|---|
| 40 | USA: Chicago, New York, Washington.
|
|---|
| 41 |
|
|---|
| 42 | The natural way to do this is to have a hash whose keys are country
|
|---|
| 43 | names. Associated with each country name key is a list of the cities in
|
|---|
| 44 | that country. Each time you read a line of input, split it into a country
|
|---|
| 45 | and a city, look up the list of cities already known to be in that
|
|---|
| 46 | country, and append the new city to the list. When you're done reading
|
|---|
| 47 | the input, iterate over the hash as usual, sorting each list of cities
|
|---|
| 48 | before you print it out.
|
|---|
| 49 |
|
|---|
| 50 | If hash values can't be lists, you lose. In Perl 4, hash values can't
|
|---|
| 51 | be lists; they can only be strings. You lose. You'd probably have to
|
|---|
| 52 | combine all the cities into a single string somehow, and then when
|
|---|
| 53 | time came to write the output, you'd have to break the string into a
|
|---|
| 54 | list, sort the list, and turn it back into a string. This is messy
|
|---|
| 55 | and error-prone. And it's frustrating, because Perl already has
|
|---|
| 56 | perfectly good lists that would solve the problem if only you could
|
|---|
| 57 | use them.
|
|---|
| 58 |
|
|---|
| 59 | =head1 The Solution
|
|---|
| 60 |
|
|---|
| 61 | By the time Perl 5 rolled around, we were already stuck with this
|
|---|
| 62 | design: Hash values must be scalars. The solution to this is
|
|---|
| 63 | references.
|
|---|
| 64 |
|
|---|
| 65 | A reference is a scalar value that I<refers to> an entire array or an
|
|---|
| 66 | entire hash (or to just about anything else). Names are one kind of
|
|---|
| 67 | reference that you're already familiar with. Think of the President
|
|---|
| 68 | of the United States: a messy, inconvenient bag of blood and bones.
|
|---|
| 69 | But to talk about him, or to represent him in a computer program, all
|
|---|
| 70 | you need is the easy, convenient scalar string "George Bush".
|
|---|
| 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
|
|---|
| 74 | unambiguous. Unlike "George Bush", a reference only refers to one
|
|---|
| 75 | thing, and you always know what it refers to. If you have a reference
|
|---|
| 76 | to an array, you can recover the entire array from it. If you have a
|
|---|
| 77 | reference to a hash, you can recover the entire hash. But the
|
|---|
| 78 | reference is still an 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 |
|
|---|
| 89 |
|
|---|
| 90 | =head1 Syntax
|
|---|
| 91 |
|
|---|
| 92 | There are just two ways to make a reference, and just two ways to use
|
|---|
| 93 | it once you have it.
|
|---|
| 94 |
|
|---|
| 95 | =head2 Making References
|
|---|
| 96 |
|
|---|
| 97 | =head3 B<Make Rule 1>
|
|---|
| 98 |
|
|---|
| 99 | If you put a C<\> in front of a variable, you get a
|
|---|
| 100 | reference to that variable.
|
|---|
| 101 |
|
|---|
| 102 | $aref = \@array; # $aref now holds a reference to @array
|
|---|
| 103 | $href = \%hash; # $href now holds a reference to %hash
|
|---|
| 104 | $sref = \$scalar; # $sref now holds a reference to $scalar
|
|---|
| 105 |
|
|---|
| 106 | Once the reference is stored in a variable like $aref or $href, you
|
|---|
| 107 | can copy it or store it just the same as any other scalar value:
|
|---|
| 108 |
|
|---|
| 109 | $xy = $aref; # $xy now holds a reference to @array
|
|---|
| 110 | $p[3] = $href; # $p[3] now holds a reference to %hash
|
|---|
| 111 | $z = $p[3]; # $z now holds a reference to %hash
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | These examples show how to make references to variables with names.
|
|---|
| 115 | Sometimes you want to make an array or a hash that doesn't have a
|
|---|
| 116 | name. This is analogous to the way you like to be able to use the
|
|---|
| 117 | string C<"\n"> or the number 80 without having to store it in a named
|
|---|
| 118 | variable first.
|
|---|
| 119 |
|
|---|
| 120 | B<Make Rule 2>
|
|---|
| 121 |
|
|---|
| 122 | C<[ ITEMS ]> makes a new, anonymous array, and returns a reference to
|
|---|
| 123 | that array. C<{ ITEMS }> makes a new, anonymous hash, and returns a
|
|---|
| 124 | reference to that hash.
|
|---|
| 125 |
|
|---|
| 126 | $aref = [ 1, "foo", undef, 13 ];
|
|---|
| 127 | # $aref now holds a reference to an array
|
|---|
| 128 |
|
|---|
| 129 | $href = { APR => 4, AUG => 8 };
|
|---|
| 130 | # $href now holds a reference to a hash
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | The references you get from rule 2 are the same kind of
|
|---|
| 134 | references that you get from rule 1:
|
|---|
| 135 |
|
|---|
| 136 | # This:
|
|---|
| 137 | $aref = [ 1, 2, 3 ];
|
|---|
| 138 |
|
|---|
| 139 | # Does the same as this:
|
|---|
| 140 | @array = (1, 2, 3);
|
|---|
| 141 | $aref = \@array;
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | The first line is an abbreviation for the following two lines, except
|
|---|
| 145 | that it doesn't create the superfluous array variable C<@array>.
|
|---|
| 146 |
|
|---|
| 147 | If you write just C<[]>, you get a new, empty anonymous array.
|
|---|
| 148 | If you write just C<{}>, you get a new, empty anonymous hash.
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | =head2 Using References
|
|---|
| 152 |
|
|---|
| 153 | What can you do with a reference once you have it? It's a scalar
|
|---|
| 154 | value, and we've seen that you can store it as a scalar and get it back
|
|---|
| 155 | again just like any scalar. There are just two more ways to use it:
|
|---|
| 156 |
|
|---|
| 157 | =head3 B<Use Rule 1>
|
|---|
| 158 |
|
|---|
| 159 | You can always use an array reference, in curly braces, in place of
|
|---|
| 160 | the name of an array. For example, C<@{$aref}> instead of C<@array>.
|
|---|
| 161 |
|
|---|
| 162 | Here are some examples of that:
|
|---|
| 163 |
|
|---|
| 164 | Arrays:
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | @a @{$aref} An array
|
|---|
| 168 | reverse @a reverse @{$aref} Reverse the array
|
|---|
| 169 | $a[3] ${$aref}[3] An element of the array
|
|---|
| 170 | $a[3] = 17; ${$aref}[3] = 17 Assigning an element
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | On each line are two expressions that do the same thing. The
|
|---|
| 174 | left-hand versions operate on the array C<@a>. The right-hand
|
|---|
| 175 | versions operate on the array that is referred to by C<$aref>. Once
|
|---|
| 176 | they find the array they're operating on, both versions do the same
|
|---|
| 177 | things to the arrays.
|
|---|
| 178 |
|
|---|
| 179 | Using a hash reference is I<exactly> the same:
|
|---|
| 180 |
|
|---|
| 181 | %h %{$href} A hash
|
|---|
| 182 | keys %h keys %{$href} Get the keys from the hash
|
|---|
| 183 | $h{'red'} ${$href}{'red'} An element of the hash
|
|---|
| 184 | $h{'red'} = 17 ${$href}{'red'} = 17 Assigning an element
|
|---|
| 185 |
|
|---|
| 186 | Whatever you want to do with a reference, B<Use Rule 1> tells you how
|
|---|
| 187 | to do it. You just write the Perl code that you would have written
|
|---|
| 188 | for doing the same thing to a regular array or hash, and then replace
|
|---|
| 189 | the array or hash name with C<{$reference}>. "How do I loop over an
|
|---|
| 190 | array when all I have is a reference?" Well, to loop over an array, you
|
|---|
| 191 | would write
|
|---|
| 192 |
|
|---|
| 193 | for my $element (@array) {
|
|---|
| 194 | ...
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | so replace the array name, C<@array>, with the reference:
|
|---|
| 198 |
|
|---|
| 199 | for my $element (@{$aref}) {
|
|---|
| 200 | ...
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | "How do I print out the contents of a hash when all I have is a
|
|---|
| 204 | reference?" First write the code for printing out a hash:
|
|---|
| 205 |
|
|---|
| 206 | for my $key (keys %hash) {
|
|---|
| 207 | print "$key => $hash{$key}\n";
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | And then replace the hash name with the reference:
|
|---|
| 211 |
|
|---|
| 212 | for my $key (keys %{$href}) {
|
|---|
| 213 | print "$key => ${$href}{$key}\n";
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | =head3 B<Use Rule 2>
|
|---|
| 217 |
|
|---|
| 218 | B<Use Rule 1> is all you really need, because it tells you how to do
|
|---|
| 219 | absolutely everything you ever need to do with references. But the
|
|---|
|
|---|