1N/A
1N/A=head1 NAME
1N/A
1N/Aperlreftut - Mark's very short tutorial about references
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AOne of the most important new features in Perl 5 was the capability to
1N/Amanage complicated data structures like multidimensional arrays and
1N/Anested hashes. To enable these, Perl 5 introduced a feature called
1N/A`references', and using references is the key to managing complicated,
1N/Astructured data in Perl. Unfortunately, there's a lot of funny syntax
1N/Ato learn, and the main manual page can be hard to follow. The manual
1N/Ais quite complete, and sometimes people find that a problem, because
1N/Ait can be hard to tell what is important and what isn't.
1N/A
1N/AFortunately, you only need to know 10% of what's in the main page to get
1N/A90% of the benefit. This page will show you that 10%.
1N/A
1N/A=head1 Who Needs Complicated Data Structures?
1N/A
1N/AOne problem that came up all the time in Perl 4 was how to represent a
1N/Ahash whose values were lists. Perl 4 had hashes, of course, but the
1N/Avalues had to be scalars; they couldn't be lists.
1N/A
1N/AWhy would you want a hash of lists? Let's take a simple example: You
1N/Ahave a file of city and country names, like this:
1N/A
1N/A Chicago, USA
1N/A Frankfurt, Germany
1N/A Berlin, Germany
1N/A Washington, USA
1N/A Helsinki, Finland
1N/A New York, USA
1N/A
1N/Aand you want to produce an output like this, with each country mentioned
1N/Aonce, and then an alphabetical list of the cities in that country:
1N/A
1N/A Finland: Helsinki.
1N/A Germany: Berlin, Frankfurt.
1N/A USA: Chicago, New York, Washington.
1N/A
1N/AThe natural way to do this is to have a hash whose keys are country
1N/Anames. Associated with each country name key is a list of the cities in
1N/Athat country. Each time you read a line of input, split it into a country
1N/Aand a city, look up the list of cities already known to be in that
1N/Acountry, and append the new city to the list. When you're done reading
1N/Athe input, iterate over the hash as usual, sorting each list of cities
1N/Abefore you print it out.
1N/A
1N/AIf hash values can't be lists, you lose. In Perl 4, hash values can't
1N/Abe lists; they can only be strings. You lose. You'd probably have to
1N/Acombine all the cities into a single string somehow, and then when
1N/Atime came to write the output, you'd have to break the string into a
1N/Alist, sort the list, and turn it back into a string. This is messy
1N/Aand error-prone. And it's frustrating, because Perl already has
1N/Aperfectly good lists that would solve the problem if only you could
1N/Ause them.
1N/A
1N/A=head1 The Solution
1N/A
1N/ABy the time Perl 5 rolled around, we were already stuck with this
1N/Adesign: Hash values must be scalars. The solution to this is
1N/Areferences.
1N/A
1N/AA reference is a scalar value that I<refers to> an entire array or an
1N/Aentire hash (or to just about anything else). Names are one kind of
1N/Areference that you're already familiar with. Think of the President
1N/Aof the United States: a messy, inconvenient bag of blood and bones.
1N/ABut to talk about him, or to represent him in a computer program, all
1N/Ayou need is the easy, convenient scalar string "George Bush".
1N/A
1N/AReferences in Perl are like names for arrays and hashes. They're
1N/APerl's private, internal names, so you can be sure they're
1N/Aunambiguous. Unlike "George Bush", a reference only refers to one
1N/Athing, and you always know what it refers to. If you have a reference
1N/Ato an array, you can recover the entire array from it. If you have a
1N/Areference to a hash, you can recover the entire hash. But the
1N/Areference is still an easy, compact scalar value.
1N/A
1N/AYou can't have a hash whose values are arrays; hash values can only be
1N/Ascalars. We're stuck with that. But a single reference can refer to
1N/Aan entire array, and references are scalars, so you can have a hash of
1N/Areferences to arrays, and it'll act a lot like a hash of arrays, and
1N/Ait'll be just as useful as a hash of arrays.
1N/A
1N/AWe'll come back to this city-country problem later, after we've seen
1N/Asome syntax for managing references.
1N/A
1N/A
1N/A=head1 Syntax
1N/A
1N/AThere are just two ways to make a reference, and just two ways to use
1N/Ait once you have it.
1N/A
1N/A=head2 Making References
1N/A
1N/A=head3 B<Make Rule 1>
1N/A
1N/AIf you put a C<\> in front of a variable, you get a
1N/Areference to that variable.
1N/A
1N/A $aref = \@array; # $aref now holds a reference to @array
1N/A $href = \%hash; # $href now holds a reference to %hash
1N/A
1N/AOnce the reference is stored in a variable like $aref or $href, you
1N/Acan copy it or store it just the same as any other scalar value:
1N/A
1N/A $xy = $aref; # $xy now holds a reference to @array
1N/A $p[3] = $href; # $p[3] now holds a reference to %hash
1N/A $z = $p[3]; # $z now holds a reference to %hash
1N/A
1N/A
1N/AThese examples show how to make references to variables with names.
1N/ASometimes you want to make an array or a hash that doesn't have a
1N/Aname. This is analogous to the way you like to be able to use the
1N/Astring C<"\n"> or the number 80 without having to store it in a named
1N/Avariable first.
1N/A
1N/AB<Make Rule 2>
1N/A
1N/AC<[ ITEMS ]> makes a new, anonymous array, and returns a reference to
1N/Athat array. C<{ ITEMS }> makes a new, anonymous hash, and returns a
1N/Areference to that hash.
1N/A
1N/A $aref = [ 1, "foo", undef, 13 ];
1N/A # $aref now holds a reference to an array
1N/A
1N/A $href = { APR => 4, AUG => 8 };
1N/A # $href now holds a reference to a hash
1N/A
1N/A
1N/AThe references you get from rule 2 are the same kind of
1N/Areferences that you get from rule 1:
1N/A
1N/A # This:
1N/A $aref = [ 1, 2, 3 ];
1N/A
1N/A # Does the same as this:
1N/A @array = (1, 2, 3);
1N/A $aref = \@array;
1N/A
1N/A
1N/AThe first line is an abbreviation for the following two lines, except
1N/Athat it doesn't create the superfluous array variable C<@array>.
1N/A
1N/AIf you write just C<[]>, you get a new, empty anonymous array.
1N/AIf you write just C<{}>, you get a new, empty anonymous hash.
1N/A
1N/A
1N/A=head2 Using References
1N/A
1N/AWhat can you do with a reference once you have it? It's a scalar
1N/Avalue, and we've seen that you can store it as a scalar and get it back
1N/Aagain just like any scalar. There are just two more ways to use it:
1N/A
1N/A=head3 B<Use Rule 1>
1N/A
1N/AYou can always use an array reference, in curly braces, in place of
1N/Athe name of an array. For example, C<@{$aref}> instead of C<@array>.
1N/A
1N/AHere are some examples of that:
1N/A
1N/AArrays:
1N/A
1N/A
1N/A @a @{$aref} An array
1N/A reverse @a reverse @{$aref} Reverse the array
1N/A $a[3] ${$aref}[3] An element of the array
1N/A $a[3] = 17; ${$aref}[3] = 17 Assigning an element
1N/A
1N/A
1N/AOn each line are two expressions that do the same thing. The
1N/Aleft-hand versions operate on the array C<@a>. The right-hand
1N/Aversions operate on the array that is referred to by C<$aref>. Once
1N/Athey find the array they're operating on, both versions do the same
1N/Athings to the arrays.
1N/A
1N/AUsing a hash reference is I<exactly> the same:
1N/A
1N/A %h %{$href} A hash
1N/A keys %h keys %{$href} Get the keys from the hash
1N/A $h{'red'} ${$href}{'red'} An element of the hash
1N/A $h{'red'} = 17 ${$href}{'red'} = 17 Assigning an element
1N/A
1N/AWhatever you want to do with a reference, B<Use Rule 1> tells you how
1N/Ato do it. You just write the Perl code that you would have written
1N/Afor doing the same thing to a regular array or hash, and then replace
1N/Athe array or hash name with C<{$reference}>. "How do I loop over an
1N/Aarray when all I have is a reference?" Well, to loop over an array, you
1N/Awould write
1N/A
1N/A for my $element (@array) {
1N/A ...
1N/A }
1N/A
1N/Aso replace the array name, C<@array>, with the reference:
1N/A
1N/A for my $element (@{$aref}) {
1N/A ...
1N/A }
1N/A
1N/A"How do I print out the contents of a hash when all I have is a
1N/Areference?" First write the code for printing out a hash:
1N/A
1N/A for my $key (keys %hash) {
1N/A print "$key => $hash{$key}\n";
1N/A }
1N/A
1N/AAnd then replace the hash name with the reference:
1N/A
1N/A for my $key (keys %{$href}) {
1N/A print "$key => ${$href}{$key}\n";
1N/A }
1N/A
1N/A=head3 B<Use Rule 2>
1N/A
1N/AB<Use Rule 1> is all you really need, because it tells you how to to
1N/Aabsolutely everything you ever need to do with references. But the
1N/Amost common thing to do with an array or a hash is to extract a single
1N/Aelement, and the B<Use Rule 1> notation is cumbersome. So there is an
1N/Aabbreviation.
1N/A
1N/AC<${$aref}[3]> is too hard to read, so you can write C<< $aref->[3] >>
1N/Ainstead.
1N/A
1N/AC<${$href}{red}> is too hard to read, so you can write
1N/AC<< $href->{red} >> instead.
1N/A
1N/AIf C<$aref> holds a reference to an array, then C<< $aref->[3] >> is
1N/Athe fourth element of the array. Don't confuse this with C<$aref[3]>,
1N/Awhich is the fourth element of a totally different array, one
1N/Adeceptively named C<@aref>. C<$aref> and C<@aref> are unrelated the
1N/Asame way that C<$item> and C<@item> are.
1N/A
1N/ASimilarly, C<< $href->{'red'} >> is part of the hash referred to by
1N/Athe scalar variable C<$href>, perhaps even one with no name.
1N/AC<$href{'red'}> is part of the deceptively named C<%href> hash. It's
1N/Aeasy to forget to leave out the C<< -> >>, and if you do, you'll get
1N/Abizarre results when your program gets array and hash elements out of
1N/Atotally unexpected hashes and arrays that weren't the ones you wanted
1N/Ato use.
1N/A
1N/A
1N/A=head2 An Example
1N/A
1N/ALet's see a quick example of how all this is useful.
1N/A
1N/AFirst, remember that C<[1, 2, 3]> makes an anonymous array containing
1N/AC<(1, 2, 3)>, and gives you a reference to that array.
1N/A
1N/ANow think about
1N/A
1N/A @a = ( [1, 2, 3],
1N/A [4, 5, 6],
1N/A [7, 8, 9]
1N/A );
1N/A
1N/A@a is an array with three elements, and each one is a reference to
1N/Aanother array.
1N/A
1N/AC<$a[1]> is one of these references. It refers to an array, the array
1N/Acontaining C<(4, 5, 6)>, and because it is a reference to an array,
1N/AB<Use Rule 2> says that we can write C<< $a[1]->[2] >> to get the
1N/Athird element from that array. C<< $a[1]->[2] >> is the 6.
1N/ASimilarly, C<< $a[0]->[1] >> is the 2. What we have here is like a
1N/Atwo-dimensional array; you can write C<< $a[ROW]->[COLUMN] >> to get
1N/Aor set the element in any row and any column of the array.
1N/A
1N/AThe notation still looks a little cumbersome, so there's one more
1N/Aabbreviation:
1N/A
1N/A=head2 Arrow Rule
1N/A
1N/AIn between two B<subscripts>, the arrow is optional.
1N/A
1N/AInstead of C<< $a[1]->[2] >>, we can write C<$a[1][2]>; it means the
1N/Asame thing. Instead of C<< $a[0]->[1] = 23 >>, we can write
1N/AC<$a[0][1] = 23>; it means the same thing.
1N/A
1N/ANow it really looks like two-dimensional arrays!
1N/A
1N/AYou can see why the arrows are important. Without them, we would have
1N/Ahad to write C<${$a[1]}[2]> instead of C<$a[1][2]>. For
1N/Athree-dimensional arrays, they let us write C<$x[2][3][5]> instead of
1N/Athe unreadable C<${${$x[2]}[3]}[5]>.
1N/A
1N/A=head1 Solution
1N/A
1N/AHere's the answer to the problem I posed earlier, of reformatting a
1N/Afile of city and country names.
1N/A
1N/A 1 my %table;
1N/A
1N/A 2 while (<>) {
1N/A 3 chomp;
1N/A 4 my ($city, $country) = split /, /;
1N/A 5 $table{$country} = [] unless exists $table{$country};
1N/A 6 push @{$table{$country}}, $city;
1N/A 7 }
1N/A
1N/A 8 foreach $country (sort keys %table) {
1N/A 9 print "$country: ";
1N/A 10 my @cities = @{$table{$country}};
1N/A 11 print join ', ', sort @cities;
1N/A 12 print ".\n";
1N/A 13 }
1N/A
1N/A
1N/AThe program has two pieces: Lines 2--7 read the input and build a data
1N/Astructure, and lines 8-13 analyze the data and print out the report.
1N/AWe're going to have a hash, C<%table>, whose keys are country names,
1N/Aand whose values are references to arrays of city names. The data
1N/Astructure will look like this:
1N/A
1N/A
1N/A %table
1N/A +-------+---+
1N/A | | | +-----------+--------+
1N/A |Germany| *---->| Frankfurt | Berlin |
1N/A | | | +-----------+--------+
1N/A +-------+---+
1N/A | | | +----------+
1N/A |Finland| *---->| Helsinki |
1N/A | | | +----------+
1N/A +-------+---+
1N/A | | | +---------+------------+----------+
1N/A | USA | *---->| Chicago | Washington | New York |
1N/A | | | +---------+------------+----------+
1N/A +-------+---+
1N/A
1N/AWe'll look at output first. Supposing we already have this structure,
1N/Ahow do we print it out?
1N/A
1N/A 8 foreach $country (sort keys %table) {
1N/A 9 print "$country: ";
1N/A 10 my @cities = @{$table{$country}};
1N/A 11 print join ', ', sort @cities;
1N/A 12 print ".\n";
1N/A 13 }
1N/A
1N/AC<%table> is an
1N/Aordinary hash, and we get a list of keys from it, sort the keys, and
1N/Aloop over the keys as usual. The only use of references is in line 10.
1N/AC<$table{$country}> looks up the key C<$country> in the hash
1N/Aand gets the value, which is a reference to an array of cities in that country.
1N/AB<Use Rule 1> says that
1N/Awe can recover the array by saying
1N/AC<@{$table{$country}}>. Line 10 is just like
1N/A
1N/A @cities = @array;
1N/A
1N/Aexcept that the name C<array> has been replaced by the reference
1N/AC<{$table{$country}}>. The C<@> tells Perl to get the entire array.
1N/AHaving gotten the list of cities, we sort it, join it, and print it
1N/Aout as usual.
1N/A
1N/ALines 2-7 are responsible for building the structure in the first
1N/Aplace. Here they are again:
1N/A
1N/A 2 while (<>) {
1N/A 3 chomp;
1N/A 4 my ($city, $country) = split /, /;
1N/A 5 $table{$country} = [] unless exists $table{$country};
1N/A 6 push @{$table{$country}}, $city;
1N/A 7 }
1N/A
1N/ALines 2-4 acquire a city and country name. Line 5 looks to see if the
1N/Acountry is already present as a key in the hash. If it's not, the
1N/Aprogram uses the C<[]> notation (B<Make Rule 2>) to manufacture a new,
1N/Aempty anonymous array of cities, and installs a reference to it into
1N/Athe hash under the appropriate key.
1N/A
1N/ALine 6 installs the city name into the appropriate array.
1N/AC<$table{$country}> now holds a reference to the array of cities seen
1N/Ain that country so far. Line 6 is exactly like
1N/A
1N/A push @array, $city;
1N/A
1N/Aexcept that the name C<array> has been replaced by the reference
1N/AC<{$table{$country}}>. The C<push> adds a city name to the end of the
1N/Areferred-to array.
1N/A
1N/AThere's one fine point I skipped. Line 5 is unnecessary, and we can
1N/Aget rid of it.
1N/A
1N/A 2 while (<>) {
1N/A 3 chomp;
1N/A 4 my ($city, $country) = split /, /;
1N/A 5 #### $table{$country} = [] unless exists $table{$country};
1N/A 6 push @{$table{$country}}, $city;
1N/A 7 }
1N/A
1N/AIf there's already an entry in C<%table> for the current C<$country>,
1N/Athen nothing is different. Line 6 will locate the value in
1N/AC<$table{$country}>, which is a reference to an array, and push
1N/AC<$city> into the array. But
1N/Awhat does it do when
1N/AC<$country> holds a key, say C<Greece>, that is not yet in C<%table>?
1N/A
1N/AThis is Perl, so it does the exact right thing. It sees that you want
1N/Ato push C<Athens> onto an array that doesn't exist, so it helpfully
1N/Amakes a new, empty, anonymous array for you, installs it into
1N/AC<%table>, and then pushes C<Athens> onto it. This is called
1N/A`autovivification'--bringing things to life automatically. Perl saw
1N/Athat they key wasn't in the hash, so it created a new hash entry
1N/Aautomatically. Perl saw that you wanted to use the hash value as an
1N/Aarray, so it created a new empty array and installed a reference to it
1N/Ain the hash automatically. And as usual, Perl made the array one
1N/Aelement longer to hold the new city name.
1N/A
1N/A=head1 The Rest
1N/A
1N/AI promised to give you 90% of the benefit with 10% of the details, and
1N/Athat means I left out 90% of the details. Now that you have an
1N/Aoverview of the important parts, it should be easier to read the
1N/AL<perlref> manual page, which discusses 100% of the details.
1N/A
1N/ASome of the highlights of L<perlref>:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AYou can make references to anything, including scalars, functions, and
1N/Aother references.
1N/A
1N/A=item *
1N/A
1N/AIn B<Use Rule 1>, you can omit the curly brackets whenever the thing
1N/Ainside them is an atomic scalar variable like C<$aref>. For example,
1N/AC<@$aref> is the same as C<@{$aref}>, and C<$$aref[1]> is the same as
1N/AC<${$aref}[1]>. If you're just starting out, you may want to adopt
1N/Athe habit of always including the curly brackets.
1N/A
1N/A=item *
1N/A
1N/AThis doesn't copy the underlying array:
1N/A
1N/A $aref2 = $aref1;
1N/A
1N/AYou get two references to the same array. If you modify
1N/AC<< $aref1->[23] >> and then look at
1N/AC<< $aref2->[23] >> you'll see the change.
1N/A
1N/ATo copy the array, use
1N/A
1N/A $aref2 = [@{$aref1}];
1N/A
1N/AThis uses C<[...]> notation to create a new anonymous array, and
1N/AC<$aref2> is assigned a reference to the new array. The new array is
1N/Ainitialized with the contents of the array referred to by C<$aref1>.
1N/A
1N/ASimilarly, to copy an anonymous hash, you can use
1N/A
1N/A $href2 = {%{$href1}};
1N/A
1N/A=item *
1N/A
1N/ATo see if a variable contains a reference, use the C<ref> function. It
1N/Areturns true if its argument is a reference. Actually it's a little
1N/Abetter than that: It returns C<HASH> for hash references and C<ARRAY>
1N/Afor array references.
1N/A
1N/A=item *
1N/A
1N/AIf you try to use a reference like a string, you get strings like
1N/A
1N/A ARRAY(0x80f5dec) or HASH(0x826afc0)
1N/A
1N/AIf you ever see a string that looks like this, you'll know you
1N/Aprinted out a reference by mistake.
1N/A
1N/AA side effect of this representation is that you can use C<eq> to see
1N/Aif two references refer to the same thing. (But you should usually use
1N/AC<==> instead because it's much faster.)
1N/A
1N/A=item *
1N/A
1N/AYou can use a string as if it were a reference. If you use the string
1N/AC<"foo"> as an array reference, it's taken to be a reference to the
1N/Aarray C<@foo>. This is called a I<soft reference> or I<symbolic
1N/Areference>. The declaration C<use strict 'refs'> disables this
1N/Afeature, which can cause all sorts of trouble if you use it by accident.
1N/A
1N/A=back
1N/A
1N/AYou might prefer to go on to L<perllol> instead of L<perlref>; it
1N/Adiscusses lists of lists and multidimensional arrays in detail. After
1N/Athat, you should move on to L<perldsc>; it's a Data Structure Cookbook
1N/Athat shows recipes for using and printing out arrays of hashes, hashes
1N/Aof arrays, and other kinds of data.
1N/A
1N/A=head1 Summary
1N/A
1N/AEveryone needs compound data structures, and in Perl the way you get
1N/Athem is with references. There are four important rules for managing
1N/Areferences: Two for making references and two for using them. Once
1N/Ayou know these rules you can do most of the important things you need
1N/Ato do with references.
1N/A
1N/A=head1 Credits
1N/A
1N/AAuthor: Mark Jason Dominus, Plover Systems (C<mjd-perl-ref+@plover.com>)
1N/A
1N/AThis article originally appeared in I<The Perl Journal>
1N/A( http://www.tpj.com/ ) volume 3, #2. Reprinted with permission.
1N/A
1N/AThe original title was I<Understand References Today>.
1N/A
1N/A=head2 Distribution Conditions
1N/A
1N/ACopyright 1998 The Perl Journal.
1N/A
1N/AThis documentation is free; you can redistribute it and/or modify it
1N/Aunder the same terms as Perl itself.
1N/A
1N/AIrrespective of its distribution, all code examples in these files are
1N/Ahereby placed into the public domain. You are permitted and
1N/Aencouraged to use this code in your own programs for fun or for profit
1N/Aas you see fit. A simple comment in the code giving credit would be
1N/Acourteous but is not required.
1N/A
1N/A
1N/A
1N/A
1N/A=cut