--- name: reference basics --- # Basics — taking references and using them There are only two ways to **make** a reference to something that already has a name, and only two ways to **use** a reference once you have it. This chapter covers both. Anonymous constructors (`[...]`, `{...}`) are covered in *Anonymous references*. ## Making a reference with backslash Put a backslash in front of any variable, and you get a scalar that refers to it: ```perl my @array = (1, 2, 3); my %hash = (APR => 4, AUG => 8); my $n = 42; my $aref = \@array; # reference to the array my $href = \%hash; # reference to the hash my $sref = \$n; # reference to the scalar ``` A reference is itself a scalar. You can copy it, pass it, store it in another data structure, without touching the thing it points at: ```perl my $copy = $aref; # two scalars, one underlying array my @slots; $slots[3] = $href; # hash reference stored as an array element my $pulled = $slots[3]; ``` Both `$aref` and `$copy` point at the **same** array. Changing the array through one is visible through the other. To actually copy the contents, see *Anonymous references*. ## What a reference prints as Stringify a reference and you get a type tag plus an address: ```perl print "$aref\n"; # ARRAY(0x55d3c1a02b40) print "$href\n"; # HASH(0x55d3c1a02be0) ``` If you ever see output like that by accident, you printed the reference where you meant to print the contents. ## Using a reference — the curly-brace form Wherever you could have written a variable name, you can put the reference in curly braces instead. The rule is mechanical: | Operation on `@array` | Same operation via `$aref` | |---------------------------------|------------------------------------------------| | `@array` | `@{$aref}` | | `scalar @array` | `scalar @{$aref}` | | `$array[3]` | `${$aref}[3]` | | `$array[3] = 17` | `${$aref}[3] = 17` | | `for my $x (@array) { ... }` | `for my $x (@{$aref}) { ... }` | Hashes follow the same pattern: | Operation on `%hash` | Same operation via `$href` | |---------------------------------|------------------------------------------------| | `%hash` | `%{$href}` | | `keys %hash` | `keys %{$href}` | | `$hash{red}` | `${$href}{red}` | | `$hash{red} = 17` | `${$href}{red} = 17` | And scalars: ```perl my $x = 10; my $ref = \$x; print ${$ref}; # 10 ${$ref} = 99; print $x; # 99 ``` The curly braces can usually be dropped when what's inside is a plain scalar variable: `@$aref` means the same as `@{$aref}`, and `$$sref` means the same as `${$sref}`. Start with the curly form until the short form stops looking ambiguous; `$$hash{key}` and `${$hash}{key}` both work, but `$$table{$country}[0]` is easier to misread than `${$table}{$country}[0]`. ## Using a reference — the arrow form The most common thing you do with a reference is fetch one element out of the array or hash it points at. The curly form works (`${$aref}[3]`) but is hard to read. The arrow form is the abbreviation: ```perl $aref->[3] # same as ${$aref}[3] $href->{red} # same as ${$href}{red} ``` The arrow is not cosmetic. It is the thing that distinguishes two different meanings: ```perl $aref->[3] # fourth element of the array $aref points at $aref[3] # fourth element of the unrelated array @aref ``` `$aref` and `@aref` are separate variables, as unrelated as `$x` and `@x`. Forgetting the arrow silently fetches from a different array that probably does not exist. Running under `use strict` turns the mistake into a compile-time error instead of a silent wrong answer at runtime. ## Asking what a reference points at The [`ref`](../../p5/core/perlfunc/ref) built-in returns a string describing the referent's kind: ```perl ref $aref # ARRAY ref $href # HASH ref $sref # SCALAR ref \&printer # CODE ref $n # empty string — $n isn't a reference ``` Combine with boolean context to test "is this a reference?": ```perl if (ref $maybe) { # it's a reference of some kind } if (ref $maybe eq 'ARRAY') { # it's specifically an array reference } ``` See [`ref`](../../p5/core/perlfunc/ref) for the full list of return strings (including `CODE`, `GLOB`, `REF`, `Regexp`, and the package name when the reference has been blessed into a class). ## Two common beginner mistakes - **Treating a reference like the underlying container.** `push $aref, 1` does not push into the array; it's a type error. You must dereference: `push @{$aref}, 1` or (equivalently) `push @$aref, 1`. - **Dropping the arrow between a variable and its subscript.** `$aref[3]` is not the same as `$aref->[3]`. The first reads from `@aref`, the second reads through the reference. ## Where to go next - *Arrays of arrays* — the two-dimensional case, and the arrow-between-subscripts rule. - *Anonymous references* — `[...]` and `{...}` for building structures without named intermediates.