| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLREFTUT 1" |
| 132 | .TH PERLREFTUT 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perlreftut \- Mark's very short tutorial about references |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | One of the most important new features in Perl 5 was the capability to |
| 138 | manage complicated data structures like multidimensional arrays and |
| 139 | nested hashes. To enable these, Perl 5 introduced a feature called |
| 140 | `references', and using references is the key to managing complicated, |
| 141 | structured data in Perl. Unfortunately, there's a lot of funny syntax |
| 142 | to learn, and the main manual page can be hard to follow. The manual |
| 143 | is quite complete, and sometimes people find that a problem, because |
| 144 | it can be hard to tell what is important and what isn't. |
| 145 | .PP |
| 146 | Fortunately, you only need to know 10% of what's in the main page to get |
| 147 | 90% of the benefit. This page will show you that 10%. |
| 148 | .SH "Who Needs Complicated Data Structures?" |
| 149 | .IX Header "Who Needs Complicated Data Structures?" |
| 150 | One problem that came up all the time in Perl 4 was how to represent a |
| 151 | hash whose values were lists. Perl 4 had hashes, of course, but the |
| 152 | values had to be scalars; they couldn't be lists. |
| 153 | .PP |
| 154 | Why would you want a hash of lists? Let's take a simple example: You |
| 155 | have a file of city and country names, like this: |
| 156 | .PP |
| 157 | .Vb 6 |
| 158 | \& Chicago, USA |
| 159 | \& Frankfurt, Germany |
| 160 | \& Berlin, Germany |
| 161 | \& Washington, USA |
| 162 | \& Helsinki, Finland |
| 163 | \& New York, USA |
| 164 | .Ve |
| 165 | .PP |
| 166 | and you want to produce an output like this, with each country mentioned |
| 167 | once, and then an alphabetical list of the cities in that country: |
| 168 | .PP |
| 169 | .Vb 3 |
| 170 | \& Finland: Helsinki. |
| 171 | \& Germany: Berlin, Frankfurt. |
| 172 | \& USA: Chicago, New York, Washington. |
| 173 | .Ve |
| 174 | .PP |
| 175 | The natural way to do this is to have a hash whose keys are country |
| 176 | names. Associated with each country name key is a list of the cities in |
| 177 | that country. Each time you read a line of input, split it into a country |
| 178 | and a city, look up the list of cities already known to be in that |
| 179 | country, and append the new city to the list. When you're done reading |
| 180 | the input, iterate over the hash as usual, sorting each list of cities |
| 181 | before you print it out. |
| 182 | .PP |
| 183 | If hash values can't be lists, you lose. In Perl 4, hash values can't |
| 184 | be lists; they can only be strings. You lose. You'd probably have to |
| 185 | combine all the cities into a single string somehow, and then when |
| 186 | time came to write the output, you'd have to break the string into a |
| 187 | list, sort the list, and turn it back into a string. This is messy |
| 188 | and error\-prone. And it's frustrating, because Perl already has |
| 189 | perfectly good lists that would solve the problem if only you could |
| 190 | use them. |
| 191 | .SH "The Solution" |
| 192 | .IX Header "The Solution" |
| 193 | By the time Perl 5 rolled around, we were already stuck with this |
| 194 | design: Hash values must be scalars. The solution to this is |
| 195 | references. |
| 196 | .PP |
| 197 | A reference is a scalar value that \fIrefers to\fR an entire array or an |
| 198 | entire hash (or to just about anything else). Names are one kind of |
| 199 | reference that you're already familiar with. Think of the President |
| 200 | of the United States: a messy, inconvenient bag of blood and bones. |
| 201 | But to talk about him, or to represent him in a computer program, all |
| 202 | you need is the easy, convenient scalar string \*(L"George Bush\*(R". |
| 203 | .PP |
| 204 | References in Perl are like names for arrays and hashes. They're |
| 205 | Perl's private, internal names, so you can be sure they're |
| 206 | unambiguous. Unlike \*(L"George Bush\*(R", a reference only refers to one |
| 207 | thing, and you always know what it refers to. If you have a reference |
| 208 | to an array, you can recover the entire array from it. If you have a |
| 209 | reference to a hash, you can recover the entire hash. But the |
| 210 | reference is still an easy, compact scalar value. |
| 211 | .PP |
| 212 | You can't have a hash whose values are arrays; hash values can only be |
| 213 | scalars. We're stuck with that. But a single reference can refer to |
| 214 | an entire array, and references are scalars, so you can have a hash of |
| 215 | references to arrays, and it'll act a lot like a hash of arrays, and |
| 216 | it'll be just as useful as a hash of arrays. |
| 217 | .PP |
| 218 | We'll come back to this city-country problem later, after we've seen |
| 219 | some syntax for managing references. |
| 220 | .SH "Syntax" |
| 221 | .IX Header "Syntax" |
| 222 | There are just two ways to make a reference, and just two ways to use |
| 223 | it once you have it. |
| 224 | .Sh "Making References" |
| 225 | .IX Subsection "Making References" |
| 226 | \&\fBMake Rule 1\fR |
| 227 | .PP |
| 228 | If you put a \f(CW\*(C`\e\*(C'\fR in front of a variable, you get a |
| 229 | reference to that variable. |
| 230 | .PP |
| 231 | .Vb 2 |
| 232 | \& $aref = \e@array; # $aref now holds a reference to @array |
| 233 | \& $href = \e%hash; # $href now holds a reference to %hash |
| 234 | .Ve |
| 235 | .PP |
| 236 | Once the reference is stored in a variable like \f(CW$aref\fR or \f(CW$href\fR, you |
| 237 | can copy it or store it just the same as any other scalar value: |
| 238 | .PP |
| 239 | .Vb 3 |
| 240 | \& $xy = $aref; # $xy now holds a reference to @array |
| 241 | \& $p[3] = $href; # $p[3] now holds a reference to %hash |
| 242 | \& $z = $p[3]; # $z now holds a reference to %hash |
| 243 | .Ve |
| 244 | .PP |
| 245 | These examples show how to make references to variables with names. |
| 246 | Sometimes you want to make an array or a hash that doesn't have a |
| 247 | name. This is analogous to the way you like to be able to use the |
| 248 | string \f(CW"\en"\fR or the number 80 without having to store it in a named |
| 249 | variable first. |
| 250 | .PP |
| 251 | \&\fBMake Rule 2\fR |
| 252 | .PP |
| 253 | \&\f(CW\*(C`[ ITEMS ]\*(C'\fR makes a new, anonymous array, and returns a reference to |
| 254 | that array. \f(CW\*(C`{ ITEMS }\*(C'\fR makes a new, anonymous hash. and returns a |
| 255 | reference to that hash. |
| 256 | .PP |
| 257 | .Vb 2 |
| 258 | \& $aref = [ 1, "foo", undef, 13 ]; |
| 259 | \& # $aref now holds a reference to an array |
| 260 | .Ve |
| 261 | .PP |
| 262 | .Vb 2 |
| 263 | \& $href = { APR => 4, AUG => 8 }; |
| 264 | \& # $href now holds a reference to a hash |
| 265 | .Ve |
| 266 | .PP |
| 267 | The references you get from rule 2 are the same kind of |
| 268 | references that you get from rule 1: |
| 269 | .PP |
| 270 | .Vb 2 |
| 271 | \& # This: |
| 272 | \& $aref = [ 1, 2, 3 ]; |
| 273 | .Ve |
| 274 | .PP |
| 275 | .Vb 3 |
| 276 | \& # Does the same as this: |
| 277 | \& @array = (1, 2, 3); |
| 278 | \& $aref = \e@array; |
| 279 | .Ve |
| 280 | .PP |
| 281 | The first line is an abbreviation for the following two lines, except |
| 282 | that it doesn't create the superfluous array variable \f(CW@array\fR. |
| 283 | .Sh "Using References" |
| 284 | .IX Subsection "Using References" |
| 285 | What can you do with a reference once you have it? It's a scalar |
| 286 | value, and we've seen that you can store it as a scalar and get it back |
| 287 | again just like any scalar. There are just two more ways to use it: |
| 288 | .PP |
| 289 | \&\fBUse Rule 1\fR |
| 290 | .PP |
| 291 | If \f(CW$aref\fR contains a reference to an array, then you |
| 292 | can put \f(CW\*(C`{$aref}\*(C'\fR anywhere you would normally put the name of an |
| 293 | array. For example, \f(CW\*(C`@{$aref}\*(C'\fR instead of \f(CW@array\fR. |
| 294 | .PP |
| 295 | Here are some examples of that: |
| 296 | .PP |
| 297 | Arrays: |
| 298 | .PP |
| 299 | .Vb 4 |
| 300 | \& @a @{$aref} An array |
| 301 | \& reverse @a reverse @{$aref} Reverse the array |
| 302 | \& $a[3] ${$aref}[3] An element of the array |
| 303 | \& $a[3] = 17; ${$aref}[3] = 17 Assigning an element |
| 304 | .Ve |
| 305 | .PP |
| 306 | On each line are two expressions that do the same thing. The |
| 307 | left-hand versions operate on the array \f(CW@a\fR, and the right-hand |
| 308 | versions operate on the array that is referred to by \f(CW$aref\fR, but |
| 309 | once they find the array they're operating on, they do the same things |
| 310 | to the arrays. |
| 311 | .PP |
| 312 | Using a hash reference is \fIexactly\fR the same: |
| 313 | .PP |
| 314 | .Vb 4 |
| 315 | \& %h %{$href} A hash |
| 316 | \& keys %h keys %{$href} Get the keys from the hash |
| 317 | \& $h{'red'} ${$href}{'red'} An element of the hash |
| 318 | \& $h{'red'} = 17 ${$href}{'red'} = 17 Assigning an element |
| 319 | .Ve |
| 320 | .PP |
| 321 | \&\fBUse Rule 2\fR |
| 322 | .PP |
| 323 | \&\f(CW\*(C`${$aref}[3]\*(C'\fR is too hard to read, so you can write \f(CW\*(C`$aref\->[3]\*(C'\fR |
| 324 | instead. |
| 325 | .PP |
| 326 | \&\f(CW\*(C`${$href}{red}\*(C'\fR is too hard to read, so you can write |
| 327 | \&\f(CW\*(C`$href\->{red}\*(C'\fR instead. |
| 328 | .PP |
| 329 | Most often, when you have an array or a hash, you want to get or set a |
| 330 | single element from it. \f(CW\*(C`${$aref}[3]\*(C'\fR and \f(CW\*(C`${$href}{'red'}\*(C'\fR have |
| 331 | too much punctuation, and Perl lets you abbreviate. |
| 332 | .PP |
| 333 | If \f(CW$aref\fR holds a reference to an array, then \f(CW\*(C`$aref\->[3]\*(C'\fR is |
| 334 | the fourth element of the array. Don't confuse this with \f(CW$aref[3]\fR, |
| 335 | which is the fourth element of a totally different array, one |
| 336 | deceptively named \f(CW@aref\fR. \f(CW$aref\fR and \f(CW@aref\fR are unrelated the |
| 337 | same way that \f(CW$item\fR and \f(CW@item\fR are. |
| 338 | .PP |
| 339 | Similarly, \f(CW\*(C`$href\->{'red'}\*(C'\fR is part of the hash referred to by |
| 340 | the scalar variable \f(CW$href\fR, perhaps even one with no name. |
| 341 | \&\f(CW$href{'red'}\fR is part of the deceptively named \f(CW%href\fR hash. It's |
| 342 | easy to forget to leave out the \f(CW\*(C`\->\*(C'\fR, and if you do, you'll get |
| 343 | bizarre results when your program gets array and hash elements out of |
| 344 | totally unexpected hashes and arrays that weren't the ones you wanted |
| 345 | to use. |
| 346 | .SH "An Example" |
| 347 | .IX Header "An Example" |
| 348 | Let's see a quick example of how all this is useful. |
| 349 | .PP |
| 350 | First, remember that \f(CW\*(C`[1, 2, 3]\*(C'\fR makes an anonymous array containing |
| 351 | \&\f(CW\*(C`(1, 2, 3)\*(C'\fR, and gives you a reference to that array. |
| 352 | .PP |
| 353 | Now think about |
| 354 | .PP |
| 355 | .Vb 4 |
| 356 | \& @a = ( [1, 2, 3], |
| 357 | \& [4, 5, 6], |
| 358 | \& [7, 8, 9] |
| 359 | \& ); |
| 360 | .Ve |
| 361 | .PP |
| 362 | @a is an array with three elements, and each one is a reference to |
| 363 | another array. |
| 364 | .PP |
| 365 | \&\f(CW$a[1]\fR is one of these references. It refers to an array, the array |
| 366 | containing \f(CW\*(C`(4, 5, 6)\*(C'\fR, and because it is a reference to an array, |
| 367 | \&\fB\s-1USE\s0 \s-1RULE\s0 2\fR says that we can write \f(CW$a[1]\->[2]\fR to get the |
| 368 | third element from that array. \f(CW$a[1]\->[2]\fR is the 6. |
| 369 | Similarly, \f(CW$a[0]\->[1]\fR is the 2. What we have here is like a |
| 370 | two-dimensional array; you can write \f(CW$a[ROW]\->[COLUMN]\fR to get |
| 371 | or set the element in any row and any column of the array. |
| 372 | .PP |
| 373 | The notation still looks a little cumbersome, so there's one more |
| 374 | abbreviation: |
| 375 | .SH "Arrow Rule" |
| 376 | .IX Header "Arrow Rule" |
| 377 | In between two \fBsubscripts\fR, the arrow is optional. |
| 378 | .PP |
| 379 | Instead of \f(CW$a[1]\->[2]\fR, we can write \f(CW$a[1][2]\fR; it means the |
| 380 | same thing. Instead of \f(CW$a[0]\->[1]\fR, we can write \f(CW$a[0][1]\fR; |
| 381 | it means the same thing. |
| 382 | .PP |
| 383 | Now it really looks like two-dimensional arrays! |
| 384 | .PP |
| 385 | You can see why the arrows are important. Without them, we would have |
| 386 | had to write \f(CW\*(C`${$a[1]}[2]\*(C'\fR instead of \f(CW$a[1][2]\fR. For |
| 387 | three-dimensional arrays, they let us write \f(CW$x[2][3][5]\fR instead of |
| 388 | the unreadable \f(CW\*(C`${${$x[2]}[3]}[5]\*(C'\fR. |
| 389 | .SH "Solution" |
| 390 | .IX Header "Solution" |
| 391 | Here's the answer to the problem I posed earlier, of reformatting a |
| 392 | file of city and country names. |
| 393 | .PP |
| 394 | .Vb 12 |
| 395 | \& 1 while (<>) { |
| 396 | \& 2 chomp; |
| 397 | \& 3 my ($city, $country) = split /, /; |
| 398 | \& 4 push @{$table{$country}}, $city; |
| 399 | \& 5 } |
| 400 | \& 6 |
| 401 | \& 7 foreach $country (sort keys %table) { |
| 402 | \& 8 print "$country: "; |
| 403 | \& 9 my @cities = @{$table{$country}}; |
| 404 | \& 10 print join ', ', sort @cities; |
| 405 | \& 11 print ".\en"; |
| 406 | \& 12 } |
| 407 | .Ve |
| 408 | .PP |
| 409 | The program has two pieces: Lines 1\-\-5 read the input and build a |
| 410 | data structure, and lines 7\-\-12 analyze the data and print out the |
| 411 | report. |
| 412 | .PP |
| 413 | In the first part, line 4 is the important one. We're going to have a |
| 414 | hash, \f(CW%table\fR, whose keys are country names, and whose values are |
| 415 | (references to) arrays of city names. After acquiring a city and |
| 416 | country name, the program looks up \f(CW$table{$country}\fR, which holds (a |
| 417 | reference to) the list of cities seen in that country so far. Line 4 is |
| 418 | totally analogous to |
| 419 | .PP |
| 420 | .Vb 1 |
| 421 | \& push @array, $city; |
| 422 | .Ve |
| 423 | .PP |
| 424 | except that the name \f(CW\*(C`array\*(C'\fR has been replaced by the reference |
| 425 | \&\f(CW\*(C`{$table{$country}}\*(C'\fR. The \f(CW\*(C`push\*(C'\fR adds a city name to the end of the |
| 426 | referred-to array. |
| 427 | .PP |
| 428 | In the second part, line 9 is the important one. Again, |
| 429 | \&\f(CW$table{$country}\fR is (a reference to) the list of cities in the country, so |
| 430 | we can recover the original list, and copy it into the array \f(CW@cities\fR, |
| 431 | by using \f(CW\*(C`@{$table{$country}}\*(C'\fR. Line 9 is totally analogous to |
| 432 | .PP |
| 433 | .Vb 1 |
| 434 | \& @cities = @array; |
| 435 | .Ve |
| 436 | .PP |
| 437 | except that the name \f(CW\*(C`array\*(C'\fR has been replaced by the reference |
| 438 | \&\f(CW\*(C`{$table{$country}}\*(C'\fR. The \f(CW\*(C`@\*(C'\fR tells Perl to get the entire array. |
| 439 | .PP |
| 440 | The rest of the program is just familiar uses of \f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`sort\*(C'\fR, |
| 441 | \&\f(CW\*(C`print\*(C'\fR, and doesn't involve references at all. |
| 442 | .PP |
| 443 | There's one fine point I skipped. Suppose the program has just read |
| 444 | the first line in its input that happens to mention Greece. |
| 445 | Control is at line 4, \f(CW$country\fR is \f(CW'Greece'\fR, and \f(CW$city\fR is |
| 446 | \&\f(CW'Athens'\fR. Since this is the first city in Greece, |
| 447 | \&\f(CW$table{$country}\fR is undefined\-\-\-in fact there isn't an \f(CW'Greece'\fR key |
| 448 | in \f(CW%table\fR at all. What does line 4 do here? |
| 449 | .PP |
| 450 | .Vb 1 |
| 451 | \& 4 push @{$table{$country}}, $city; |
| 452 | .Ve |
| 453 | .PP |
| 454 | This is Perl, so it does the exact right thing. It sees that you want |
| 455 | to push \f(CW\*(C`Athens\*(C'\fR onto an array that doesn't exist, so it helpfully |
| 456 | makes a new, empty, anonymous array for you, installs it in the table, |
| 457 | and then pushes \f(CW\*(C`Athens\*(C'\fR onto it. This is called `autovivification'. |
| 458 | .SH "The Rest" |
| 459 | .IX Header "The Rest" |
| 460 | I promised to give you 90% of the benefit with 10% of the details, and |
| 461 | that means I left out 90% of the details. Now that you have an |
| 462 | overview of the important parts, it should be easier to read the |
| 463 | perlref manual page, which discusses 100% of the details. |
| 464 | .PP |
| 465 | Some of the highlights of perlref: |
| 466 | .IP "\(bu" 4 |
| 467 | You can make references to anything, including scalars, functions, and |
| 468 | other references. |
| 469 | .IP "\(bu" 4 |
| 470 | In \fB\s-1USE\s0 \s-1RULE\s0 1\fR, you can omit the curly brackets whenever the thing |
| 471 | inside them is an atomic scalar variable like \f(CW$aref\fR. For example, |
| 472 | \&\f(CW@$aref\fR is the same as \f(CW\*(C`@{$aref}\*(C'\fR, and \f(CW$$aref[1]\fR is the same as |
| 473 | \&\f(CW\*(C`${$aref}[1]\*(C'\fR. If you're just starting out, you may want to adopt |
| 474 | the habit of always including the curly brackets. |
| 475 | .IP "\(bu" 4 |
| 476 | To see if a variable contains a reference, use the `ref' function. |
| 477 | It returns true if its argument is a reference. Actually it's a |
| 478 | little better than that: It returns \s-1HASH\s0 for hash references and |
| 479 | \&\s-1ARRAY\s0 for array references. |
| 480 | .IP "\(bu" 4 |
| 481 | If you try to use a reference like a string, you get strings like |
| 482 | .Sp |
| 483 | .Vb 1 |
| 484 | \& ARRAY(0x80f5dec) or HASH(0x826afc0) |
| 485 | .Ve |
| 486 | .Sp |
| 487 | If you ever see a string that looks like this, you'll know you |
| 488 | printed out a reference by mistake. |
| 489 | .Sp |
| 490 | A side effect of this representation is that you can use \f(CW\*(C`eq\*(C'\fR to see |
| 491 | if two references refer to the same thing. (But you should usually use |
| 492 | \&\f(CW\*(C`==\*(C'\fR instead because it's much faster.) |
| 493 | .IP "\(bu" 4 |
| 494 | You can use a string as if it were a reference. If you use the string |
| 495 | \&\f(CW"foo"\fR as an array reference, it's taken to be a reference to the |
| 496 | array \f(CW@foo\fR. This is called a \fIsoft reference\fR or \fIsymbolic reference\fR. |
| 497 | .PP |
| 498 | You might prefer to go on to perllol instead of perlref; it |
| 499 | discusses lists of lists and multidimensional arrays in detail. After |
| 500 | that, you should move on to perldsc; it's a Data Structure Cookbook |
| 501 | that shows recipes for using and printing out arrays of hashes, hashes |
| 502 | of arrays, and other kinds of data. |
| 503 | .SH "Summary" |
| 504 | .IX Header "Summary" |
| 505 | Everyone needs compound data structures, and in Perl the way you get |
| 506 | them is with references. There are four important rules for managing |
| 507 | references: Two for making references and two for using them. Once |
| 508 | you know these rules you can do most of the important things you need |
| 509 | to do with references. |
| 510 | .SH "Credits" |
| 511 | .IX Header "Credits" |
| 512 | Author: Mark-Jason Dominus, Plover Systems (\f(CW\*(C`mjd\-perl\-ref+@plover.com\*(C'\fR) |
| 513 | .PP |
| 514 | This article originally appeared in \fIThe Perl Journal\fR |
| 515 | ( http://www.tpj.com/ ) volume 3, #2. Reprinted with permission. |
| 516 | .PP |
| 517 | The original title was \fIUnderstand References Today\fR. |
| 518 | .Sh "Distribution Conditions" |
| 519 | .IX Subsection "Distribution Conditions" |
| 520 | Copyright 1998 The Perl Journal. |
| 521 | .PP |
| 522 | When included as part of the Standard Version of Perl, or as part of |
| 523 | its complete documentation whether printed or otherwise, this work may |
| 524 | be distributed only under the terms of Perl's Artistic License. Any |
| 525 | distribution of this file or derivatives thereof outside of that |
| 526 | package require that special arrangements be made with copyright |
| 527 | holder. |
| 528 | .PP |
| 529 | Irrespective of its distribution, all code examples in these files are |
| 530 | hereby placed into the public domain. You are permitted and |
| 531 | encouraged to use this code in your own programs for fun or for profit |
| 532 | as you see fit. A simple comment in the code giving credit would be |
| 533 | courteous but is not required. |