develooper Front page | perl.perl5.porters | Postings from September 2013

\@ and \% prototypes and slices

Thread Next
From:
Father Chrysostomos
Date:
September 16, 2013 21:53
Subject:
\@ and \% prototypes and slices
Message ID:
20130916215323.14784.qmail@lists-nntp.develooper.com
It has never been clearly documented that \@ prototypes do not accept
slices.  Now that we have % slices, I thought it might be good to doc-
ument backslash prototypes more clearly.  But I fear my attempt may
make things too verbose.

(It will also conflict with the postfix dereference doc patch that
Ricardo Signes is working on.)

diff --git a/pod/perlsub.pod b/pod/perlsub.pod
index 2b4b35b..4c99a9e 100644
--- a/pod/perlsub.pod
+++ b/pod/perlsub.pod
@@ -1230,13 +1230,44 @@ corresponding built-in.
    sub myrand (;$)   myrand 42
    sub mytime ()   mytime
 
-Any backslashed prototype character represents an actual argument
-that must start with that character (optionally preceded by C<my>,
-C<our> or C<local>), with the exception of C<$>, which will
-accept any scalar lvalue expression, such as C<$foo = 7> or
-C<< my_function()->[0] >>. The value passed as part of C<@_> will be a
+A backslashed prototype character expects its argument to
+be a variable or dereference of the given type, with the
+exception of C<$>, which will accept any scalar lvalue
+expression. The value passed as part of C<@_> will be a
 reference to the actual argument given in the subroutine call,
-obtained by applying C<\> to that argument.
+obtained by applying C<\> to that argument.  Examples of valid (and some
+invalid) arguments include:
+
+    \**foo
+    \**foolocal *foo
+    \**foolocal *foo*$ref
+    \**foolocal *foo*$ref*{ func() }
+
+    \&&foo
+    \&&foo&$coderef
+    \&&foo&$coderef&{ $coderef }
+
+    \%%foo
+    \%%foomy %foo
+    \%%foomy %foo%$foo
+    \%%foomy %foo%$foo%{ $foo }
+    \%%foomy %foo%$foo%{ $foo }%array[$slice] # invalid; not a hash
+    \%%foomy %foo%$foo%{ $foo }%array[$slice] # invalid; not a hash%hash{slice}   # invalid; not a hash
+
+    \@@foo
+    \@@foomy @foo
+    \@@foomy @foo@$foo
+    \@@foomy @foo@$foo@{ $foo }
+    \@@foomy @foo@$foo@{ $foo }@array[$slice] # invalid; not an array
+    \@@foomy @foo@$foo@{ $foo }@array[$slice] # invalid; not an array@hash{slice}   # invalid; not an array
+
+    \$$x
+    \$$xmy $x
+    \$$xmy $x$$x
+    \$$xmy $x$$x${ $x }
+    \$$xmy $x$$x${ $x }$foo = 7# any lvalue is permitted
+    \$$xmy $x$$x${ $x }$foo = 7# any lvalue is permittedmy_function()->[0]# including array
+    \$$xmy $x$$x${ $x }$foo = 7# any lvalue is permittedmy_function()->[0]# including array( $hashes[0] )->{foo}# and hash elements
 
 You can use the C<\[]> backslash group notation to specify more than one
 allowed argument type. For example:


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About