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

Re: \@ and \% prototypes and slices

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
September 16, 2013 23:24
Subject:
Re: \@ and \% prototypes and slices
Message ID:
20130916232417.9716.qmail@lists-nntp.develooper.com
I wrote:
> 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.

The inline patch got totally screwed up.  lynx doesn’t like itwhen
I paste tabs.  So here it is without tabs.  It will not apply as-is.

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
+                local *foo
+                *$ref
+                *{ func() }
+
+    \&          &foo
+                &$coderef
+                &{ $coderef }
+
+    \%          %foo
+                my %foo
+                %$foo
+                %{ $foo }
+                %array[$slice] # invalid; not a hash
+                %hash{slice}   # invalid; not a hash
+
+    \@          @foo
+                my @foo
+                @$foo
+                @{ $foo }
+                @array[$slice] # invalid; not an array
+                @hash{slice}   # invalid; not an array
+
+    \$          $x
+                my $x
+                $$x
+                ${ $x }
+                $foo = 7                # any lvalue is permitted
+                my_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 Previous | 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