This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update perldelta with details about changes to regexp engine extflags
[perl5.git] / pod / perlref.pod
index 0fab809..6c5a7e1 100644 (file)
@@ -46,12 +46,6 @@ hard reference.
 X<reference, hard> X<hard reference>
 
 References are easy to use in Perl.  There is just one overriding
-principle: Perl does no implicit referencing or dereferencing.  When a
-scalar is holding a reference, it always behaves as a simple scalar.  It
-doesn't magically start being an array or hash or subroutine; you have to
-tell it explicitly to do so, by dereferencing it.
-
-References are easy to use in Perl.  There is just one overriding
 principle: in general, Perl does no implicit referencing or dereferencing.
 When a scalar is holding a reference, it always behaves as a simple scalar.
 It doesn't magically start being an array or hash or subroutine; you have to
@@ -155,7 +149,8 @@ reference to it, you have these options:
 
 On the other hand, if you want the other meaning, you can do this:
 
-    sub showem {        { @_ } }   # ambiguous (currently ok, but may change)
+    sub showem {        { @_ } }   # ambiguous (currently ok,
+                                   # but may change)
     sub showem {       {; @_ } }   # ok
     sub showem { { return @_ } }   # ok
 
@@ -268,8 +263,11 @@ known as foo).
     $ioref     = *STDIN{IO};
     $globref   = *foo{GLOB};
     $formatref = *foo{FORMAT};
+    $globname  = *foo{NAME};    # "foo"
+    $pkgname   = *foo{PACKAGE}; # "main"
 
-All of these are self-explanatory except for C<*foo{IO}>.  It returns
+Most of these are self-explanatory, but C<*foo{IO}>
+deserves special attention.  It returns
 the IO handle, used for file handles (L<perlfunc/open>), sockets
 (L<perlfunc/socket> and L<perlfunc/socketpair>), and directory
 handles (L<perlfunc/opendir>).  For compatibility with previous
@@ -282,6 +280,13 @@ except in the case of scalars.  C<*foo{SCALAR}> returns a reference to an
 anonymous scalar if $foo hasn't been used yet.  This might change in a
 future release.
 
+C<*foo{NAME}> and C<*foo{PACKAGE}> are the exception, in that they return
+strings, rather than references.  These return the package and name of the
+typeglob itself, rather than one that has been assigned to it.  So, after
+C<*foo=*Foo::bar>, C<*foo> will become "*Foo::bar" when used as a string,
+but C<*foo{PACKAGE}> and C<*foo{NAME}> will continue to produce "main" and
+"foo", respectively.
+
 C<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in
 L<perldata/"Typeglobs and Filehandles"> for passing filehandles
 into or out of subroutines, or storing into larger data structures.
@@ -543,7 +548,7 @@ People frequently expect it to work like this.  So it does.
     ${$name x 2} = 3;          # Sets $foofoo
     $name->[0] = 4;            # Sets $foo[0]
     @$name = ();               # Clears @foo
-    &$name();                  # Calls &foo() (as in Perl 4)
+    &$name();                  # Calls &foo()
     $pack = "THAT";
     ${"${pack}::$name"} = 5;   # Sets $THAT::foo without eval
 
@@ -575,7 +580,7 @@ variables, which are all "global" to the package.
 
 =head2 Not-so-symbolic references
 
-Since Perl verion 5.001, brackets around a symbolic reference can simply
+Brackets around a symbolic reference can simply
 serve to isolate an identifier or variable name from the rest of an
 expression, just as they always have within a string.  For example,
 
@@ -583,7 +588,7 @@ expression, just as they always have within a string.  For example,
     print "${push}over";
 
 has always meant to print "pop on over", even though push is
-a reserved word.  In 5.001, this was generalized to work the same
+a reserved word.  This is generalized to work the same
 without the enclosing double quotes, so that
 
     print ${push} . "over";
@@ -592,8 +597,7 @@ and even
 
     print ${ push } . "over";
 
-will have the same effect.  (This would have been a syntax error in
-Perl 5.000, though Perl 4 allowed it in the spaceless form.)  This
+will have the same effect.  This
 construct is I<not> considered to be a symbolic reference when you're
 using strict refs:
 
@@ -740,6 +744,66 @@ real refs, instead of the keys(), which won't.
 
 The standard Tie::RefHash module provides a convenient workaround to this.
 
+=head1 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
+of a prefixed sigil, a postfixed sigil-and-star is used.
+
+For example:
+
+    $r = \@a;
+    @b = $r->@*; # equivalent to @$r or @{ $r }
+
+    $r = [ 1, [ 2, 3 ], 4 ];
+    $r->[1]->@*;  # equivalent to @{ $r->[1] }
+
+This syntax must be enabled with C<use feature 'postderef'>.  It is
+experimental, and will warn by default unless C<no warnings
+'experimental::postderef'> is in effect.
+
+Postfix dereference should work in all circumstances where block
+(circumfix) dereference worked, and should be entirely equivalent.  This
+syntax allows dereferencing to be written and read entirely
+left-to-right.  The following equivalencies are defined:
+
+  $sref->$*;  # same as  ${ $sref }
+  $aref->@*;  # same as  @{ $aref }
+  $aref->$#*; # same as $#{ $aref }
+  $href->%*;  # same as  %{ $href }
+  $cref->&*;  # same as  &{ $cref }
+  $gref->**;  # same as  *{ $gref }
+
+Note especially that C<< $cref->&* >> is I<not> equivalent to C<<
+$cref->() >>, and can serve different purposes.
+
+Glob elements can be extracted through the postfix dereferencing feature:
+
+  $gref->*{SCALAR}; # same as *{ $gref }{SCALAR}
+
+Postfix array and scalar dereferencing I<can> be used in interpolating
+strings (double quotes or the C<qq> operator), but only if the
+additional C<postderef_qq> feature is enabled.
+
+=head2 Postfix Reference Slicing
+
+Value slices of arrays and hashes may also be taken with postfix
+dereferencing notation, with the following equivalencies:
+
+  $aref->@[ ... ];  # same as @$aref[ ... ]
+  $href->@{ ... };  # same as @$href{ ... }
+
+Postfix key/value pair slicing, added in 5.20.0 and documented in
+L<the KeyE<sol>Value Hash Slices section of perldata|perldata/"Key/Value Hash
+Slices">, also behaves as expected:
+
+  $aref->%[ ... ];  # same as %$aref[ ... ]
+  $href->%{ ... };  # same as %$href{ ... }
+
+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 SEE ALSO
 
 Besides the obvious documents, source code can be instructive.