summaryrefslogtreecommitdiff
path: root/pod/perlref.pod
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2014-10-11 00:00:55 -0700
committerFather Chrysostomos <sprout@cpan.org>2014-10-11 00:10:20 -0700
commit82848c10865918ee3d8db12bee74a88a54d8aa7f (patch)
treea38433ca18f1600c87df175bb943c62e8800f1e2 /pod/perlref.pod
parentc96cf1c0e26c1d10561709cb663973772f4cb044 (diff)
downloadperl-82848c10865918ee3d8db12bee74a88a54d8aa7f.tar.gz
Document lvalue references
Diffstat (limited to 'pod/perlref.pod')
-rw-r--r--pod/perlref.pod103
1 files changed, 103 insertions, 0 deletions
diff --git a/pod/perlref.pod b/pod/perlref.pod
index 12e1e14620..e71b7c752e 100644
--- a/pod/perlref.pod
+++ b/pod/perlref.pod
@@ -804,6 +804,109 @@ As with postfix array, postfix value slice dereferencing I<can> be used
in interpolating strings (double quotes or the C<qq> operator), but only
if the additional C<postderef_qq> L<feature> is enabled.
+=head1 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
+left-hand side becomes an alias for the thing referenced on the right-hand
+side:
+
+ \$a = \$b; # $a and $b now point to the same scalar
+ \&foo = \&bar; # foo() now means bar()
+
+This syntax must be enabled with C<use feature 'lvalue_refs'>. It is
+experimental, and will warn by default unless C<no warnings
+'experimental::lvalue_refs'> is in effect.
+
+These forms may be assigned to, and cause the right-hand side to be
+evaluated in scalar context:
+
+ \$scalar
+ \@array
+ \%hash
+ \&sub
+ \my $scalar
+ \my @array
+ \my %hash
+ \state $scalar # or @array, etc.
+ \our $scalar # etc.
+ \local $scalar # etc.
+ \local our $scalar # etc.
+ \$some_array[$index]
+ \$some_hash{$key}
+ \local $some_array[$index]
+ \local $some_hash{$key}
+ condition ? \$this : \$that[0] # etc.
+
+Parentheses cause the right-hand side to be evaluated in list context:
+
+ (\$scalar)
+ \($scalar)
+ \(my $scalar)
+ \my($scalar)
+ (\@array)
+ (\%hash)
+ (\&sub)
+ \(&sub)
+ \($foo, @bar, %baz)
+ (\$foo, \@bar, \%baz)
+
+Each element on the right-hand side must be a reference to a datum of the
+right type. Parentheses immediately surrounding an array (and possibly
+also C<my>/C<state>/C<our>/C<local>) will make each element of the array an
+alias to the corresponding scalar referenced on the right-hand side:
+
+ \(@a) = \(@b); # @a and @b now have the same elements
+ \my(@a) = \(@b); # likewise
+ \(my @a) = \(@b); # likewise
+ push @a, 3; # but now @a has an extra element that @b lacks
+ \(@a) = (\$a, \$b, \$c); # @a now contains $a, $b, and $c
+
+Combining that form with C<local> and putting parentheses immediately
+around a hash are forbidden (because it is not clear what they should do):
+
+ \local(@array) = foo(); # 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
+side are the right type for each element on the left, though this may make
+for obfuscated code:
+
+ (my $tom, \my $dick, \my @harry) = (\1, \2, [1..3]);
+ # $tom is now \1
+ # $dick is now 2 (read-only)
+ # @harry is (1,2,3)
+
+ my $type = ref $thingy;
+ ($type ? $type == 'ARRAY' ? \@foo : \$bar : $baz) = $thingy;
+
+The C<foreach> loop can also take a reference constructor for its loop
+variable, though the syntax is limited to one of the following, with an
+optional C<my>, C<state>, or C<our> after the backslash:
+
+ \$s
+ \@a
+ \%h
+ \&c
+
+No parentheses are permitted. This feature is particularly useful for
+arrays-of-arrays, or arrays-of-hashes:
+
+ foreach \my @a (@array_of_arrays) {
+ frobnicate($a[0], $a[-1]);
+ }
+
+ foreach \my %h (@array_of_hashes) {
+ $h{gelastic}++ if $h{type} == 'funny';
+ }
+
+B<CAVEAT:> Aliasing does not work correctly with closures. If you try to
+alias lexical variables from an inner subroutine or C<eval>, the aliasing
+will only be visible within that inner sub, and will not affect the outer
+subroutine where the variables are declared. This bizarre behavior is
+subject to change.
+
=head1 SEE ALSO
Besides the obvious documents, source code can be instructive.