References can be created in several ways.
-=over 4
-
-=item 1.
+=head3 Backslash Operator
X<\> X<backslash>
By using the backslash operator on a variable, subroutine, or value.
But see the explanation of the C<*foo{THING}> syntax below. However,
you can still use type globs and globrefs as though they were IO handles.
-=item 2.
+=head3 Square Brackets
X<array, anonymous> X<[> X<[]> X<square bracket>
X<bracket, square> X<arrayref> X<array reference> X<reference, array>
except that the key references are to copies (since the keys are just
strings rather than full-fledged scalars).
-=item 3.
+=head3 Curly Brackets
X<hash, anonymous> X<{> X<{}> X<curly bracket>
X<bracket, curly> X<brace> X<hashref> X<hash reference> X<reference, hash>
The leading C<+{> and C<{;> always serve to disambiguate
the expression to mean either the HASH reference, or the BLOCK.
-=item 4.
+=head3 Anonymous Subroutines
X<subroutine, anonymous> X<subroutine, reference> X<reference, subroutine>
X<scope, lexical> X<closure> X<lexical> X<lexical scope>
continue to work as they have always worked. Closure is not something
that most Perl programmers need trouble themselves about to begin with.
-=item 5.
+=head3 Constructors
X<constructor> X<new>
References are often returned by special subroutines called constructors. Perl
$menubar = $main->Frame(-relief => "raised",
-borderwidth => 2)
-=item 6.
+This indirect object syntax is only available when
+L<C<use feature "indirect">|feature/The 'indirect' feature> is in effect,
+and that is not the case when L<C<use v5.36>|perlfunc/use VERSION> (or
+higher) is requested, it is best to avoid indirect object syntax entirely.
+
+=head3 Autovivification
X<autovivification>
References of the appropriate type can spring into existence if you
dereference them in a context that assumes they exist. Because we haven't
talked about dereferencing yet, we can't show you any examples yet.
-=item 7.
+=head3 Typeglob Slots
X<*foo{THING}> X<*>
A reference can be created by using a special syntax, lovingly known as
return scalar <$fh>;
}
-=back
-
=head2 Using References
X<reference, use> X<dereferencing> X<dereference>
know how to use references to get back to your long-lost data. There
are several basic methods.
-=over 4
-
-=item 1.
+=head3 Simple Scalar
Anywhere you'd put an identifier (or chain of identifiers) as part
of a variable or subroutine name, you can replace the identifier with
$refrefref = \\\"howdy";
print $$$$refrefref;
-=item 2.
+=head3 Block
Anywhere you'd put an identifier (or chain of identifiers) as part of a
variable or subroutine name, you can replace the identifier with a
called %hashref, not dereferencing through $hashref to the hash
it's presumably referencing. That would be case 3.
-=item 3.
+=head3 Arrow Notation
Subroutine calls and lookups of individual array elements arise often
enough that it gets cumbersome to use method 2. As a form of
Well, okay, not entirely like C's arrays, actually. C doesn't know how
to grow its arrays on demand. Perl does.
-=item 4.
+=head3 Objects
If a reference happens to be a reference to an object, then there are
probably methods to access the things referred to, and you should probably
encapsulation. We are not totalitarians here. We do expect some basic
civility though.
-=back
+=head3 Miscellaneous Usage
Using a string or number as a reference produces a symbolic reference,
as explained above. Using a reference as a number produces an
weak reference does not increment the reference count for a variable,
which means that the object can go out of scope and be destroyed. You
can weaken a reference with the C<weaken> function exported by the
-L<Scalar::Util> module.
+L<Scalar::Util> module, or available as C<builtin::weaken> directly in
+Perl version 5.35.7 or later.
Here's how we can make the first example safer:
the same rule applies to any bareword that is used for subscripting a hash.
So now, instead of writing
- $array{ "aaa" }{ "bbb" }{ "ccc" }
+ $hash{ "aaa" }{ "bbb" }{ "ccc" }
you can write just
- $array{ aaa }{ bbb }{ ccc }
+ $hash{ aaa }{ bbb }{ ccc }
and not worry about whether the subscripts are reserved words. In the
rare event that you do wish to do something like
- $array{ shift }
+ $hash{ shift }
you can force interpretation as a reserved word by adding anything that
makes it more than a bareword:
- $array{ shift() }
- $array{ +shift }
- $array{ shift @_ }
+ $hash{ shift() }
+ $hash{ +shift }
+ $hash{ shift @_ }
The C<use warnings> pragma or the B<-w> switch will warn you if it
interprets a reserved word as a string.
This has the interesting effect of creating a function local to another
function, something not normally supported in Perl.
-=head1 WARNING
-X<reference, string context> X<reference, use as hash key>
-
-You may not (usefully) use a reference as the key to a hash. It will be
-converted into a string:
-
- $x{ \$a } = $a;
-
-If you try to dereference the key, it won't do a hard dereference, and
-you won't accomplish what you're attempting. You might want to do something
-more like
-
- $r = \@a;
- $x{ $r } = $r;
-
-And then at least you can use the values(), which will be
-real refs, instead of the keys(), which won't.
-
-The standard Tie::RefHash module provides a convenient workaround to this.
-
-=head1 Postfix Dereference Syntax
+=head2 Postfix Dereference Syntax
Beginning in v5.20.0, a postfix syntax for using references is
available. It behaves as described in L</Using References>, but instead
in interpolating strings (double quotes or the C<qq> operator), but only
if the C<postderef_qq> L<feature> is enabled.
-=head1 Assigning to References
+=head2 Assigning to References
Beginning in v5.22.0, the referencing operator can be assigned to. It
performs an aliasing operation, so that the variable name referenced on the
around a hash are forbidden (because it is not clear what they should do):
\local(@array) = foo(); # WRONG
- \(%hash) = bar(); # wRONG
+ \(%hash) = bar(); # WRONG
Assignment to references and non-references may be combined in lists and
conditional ternary expressions, as long as the values on the right-hand
subroutine where the variables are declared. This bizarre behavior is
subject to change.
-=head1 Declaring a Reference to a Variable
+=head2 Declaring a Reference to a Variable
Beginning in v5.26.0, the referencing operator can come after C<my>,
C<state>, C<our>, or C<local>. This syntax must be enabled with C<use
my ($foo, \@bar, \%baz); # equivalent to: my $foo, \my(@bar, %baz);
+=head1 WARNING: Don't use references as hash keys
+X<reference, string context> X<reference, use as hash key>
+
+You may not (usefully) use a reference as the key to a hash. It will be
+converted into a string:
+
+ $x{ \$a } = $a;
+
+If you try to dereference the key, it won't do a hard dereference, and
+you won't accomplish what you're attempting. You might want to do something
+more like
+
+ $r = \@a;
+ $x{ $r } = $r;
+
+And then at least you can use the values(), which will be
+real refs, instead of the keys(), which won't.
+
+The standard Tie::RefHash module provides a convenient workaround to this.
+
=head1 SEE ALSO
Besides the obvious documents, source code can be instructive.