From 3a8944db48a72ff3e936211f8b0433b10f3c6c80 Mon Sep 17 00:00:00 2001 From: Father Chrysostomos Date: Mon, 15 Aug 2011 09:20:08 -0700 Subject: [PATCH] Document and test $; prototype syntax This has worked this way for yonks. It is actually useful, so it might as well be documented. --- pod/perlsub.pod | 6 +++++- t/comp/proto.t | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pod/perlsub.pod b/pod/perlsub.pod index d344c47..e2a9bcf 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -1149,7 +1149,11 @@ arguments, just like C. That is, if you say mytime +2; you'll get C, not C, which is how it would be parsed -without a prototype. +without a prototype. If you want to force a unary function to have the +same precedence as a list operator, add C<;> to the end of the prototype: + + sub mygetprotobynumber($;); + mygetprotobynumber $a > $b; # parsed as mygetprotobynumber($a > $b) The interesting thing about C<&> is that you can generate new syntax with it, provided it's in the initial position: diff --git a/t/comp/proto.t b/t/comp/proto.t index 50aebef..2394164 100644 --- a/t/comp/proto.t +++ b/t/comp/proto.t @@ -18,7 +18,7 @@ BEGIN { # strict use strict; -print "1..174\n"; +print "1..177\n"; my $i = 1; @@ -700,6 +700,17 @@ print "not " unless eval 'sub uniproto9 (;+) {} uniproto9 $_, 1' or warn $@; print "ok ", $i++, "\n"; +# Test that a trailing semicolon makes a sub have listop precedence +sub unilist ($;) { $_[0]+1 } +sub unilist2(_;) { $_[0]+1 } +sub unilist3(;$;) { $_[0]+1 } +print "not " unless (unilist 0 || 5) == 6; +print "ok ", $i++, "\n"; +print "not " unless (unilist2 0 || 5) == 6; +print "ok ", $i++, "\n"; +print "not " unless (unilist3 0 || 5) == 6; +print "ok ", $i++, "\n"; + { # Lack of prototype on a subroutine definition should override any prototype # on the declaration. -- 2.7.4