-
Notifications
You must be signed in to change notification settings - Fork 550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
possible inconsistency in "perlop" documentation on associativity of operators #15153
Comments
From wolf-dietrich_moeller@t-online.deHello, In the section on "Operator Precedence and Associativity" there is the line: Taking the explanation in the section on "Assignment Operators" (further This is counter-intuitive to the text in the documentation given above, that This issue is not related to any particular Perl version, as I found the Best regards |
From @maukeAm 27.01.2016 um 17:32 schrieb Wolf-Dietrich Moeller (via RT):
OK so far.
I don't understand this at all. If &&= and += were left associative, What do you mean by effective associativity? -- |
The RT System itself - Status changed from 'new' to 'open' |
From wolf-dietrich_moeller@t-online.deHi Lukas, The perlop-documentation specifies associativity as (citation): Perhaps my example mislead you, as I did not show a sequence of the SAME operators (sorry for that). But please look at the following test program: So for the &&= operator to be "right-associative" according to the specification cited above, I am missing the evaluation of the rightmost operator. Thus the associativity of &&= must be something else. Hope this clarifies my issue. Best regards -----Original Message----- Am 27.01.2016 um 17:32 schrieb Wolf-Dietrich Moeller (via RT):
OK so far.
I don't understand this at all. If &&= and += were left associative, What do you mean by effective associativity? -- |
From zefram@fysh.orgWolf-Dietrich Moeller wrote:
This is a misunderstanding of operator precedence. Precedence resolves In your example, the expression $x &&= $y &&= $z &&= 7 by virtue of precedence groups as $x &&= ($y &&= ($z &&= 7)) In evaluation, the operator having greatest control is always the $y &&= ($z &&= 7) The top-level operator here is the middle &&= of the original expression.
It's not left-associative. If it were, then the expression you wrote (($x &&= $y) &&= $z) &&= 7 This is a valid expression, and does not behave the same as the The distinctive behaviour of &&= that you mistakenly described as $ perl -e '(print 1) + (print 2) + (print 3); print "\n"' In this example, both + and ** evaluate their operands from left to right, -zefram |
From wolf-dietrich_moeller@t-online.deHi Zefram, You write that describing associativity in terms of order of evaluation is only true Do I understand you correctly, that you imply that the existing text in the perlop-documentation is wrong for the combined operators with short-circuit operations (&&=, ||= and //=), where not all operands are evaluated by the operator? The existing text reads (cited): (I agree that for the example of subtraction given there the wording with "evaluation" is correct, but it is wrong for the case of combined operators with short-circuit behavior.) Should this text instead be adapted to refer to "grouping" instead of "evaluation", or should there be an additional sentence catering for the special case of combined operators with short-circuit behaviour? Best regards |
From zefram@fysh.orgWolf-Dietrich Moeller wrote:
Ah yes, the text you quote (and the preceding paragraph about precedence)
I'd rather not treat short-circuiting as a special case, because it's not, I<Operator precedence> means some operators group more tightly than I<Operator associativity> defines what happens if a sequence of the For simple operators that evaluate all their operands and then -zefram |
From wolf-dietrich_moeller@t-online.deZefram wrote:
I agree with the text you suggest to replace the existing text in perlop-documentation. Thanks |
From @jkeenanOn Mon Feb 01 09:29:42 2016, zefram@fysh.org wrote:
In order to move this ticket toward resolution I've formatted Zefram's recommendation into a patch attached to this ticket. Please discuss so that we know whether we want to apply this in 5.25.1. Thank you very much. -- |
From @jkeenan127391-0001-Improve-documentation-of-operator-precedence-and-ass.patchFrom fc6fe274f70057369f007f92305e3a31ae706a68 Mon Sep 17 00:00:00 2001
From: Zefram <zefram@fysh.org>
Date: Wed, 20 Apr 2016 22:00:09 -0400
Subject: [PATCH] Improve documentation of operator precedence and
associativity.
For RT #127391
---
pod/perlop.pod | 42 +++++++++++++++++++++++++++++++-----------
1 file changed, 31 insertions(+), 11 deletions(-)
diff --git a/pod/perlop.pod b/pod/perlop.pod
index 17d24bb..ef47b45 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -27,17 +27,37 @@ X<operator, precedence> X<precedence> X<associativity>
Operator precedence and associativity work in Perl more or less like
they do in mathematics.
-I<Operator precedence> means some operators are evaluated before
-others. For example, in S<C<2 + 4 * 5>>, the multiplication has higher
-precedence so S<C<4 * 5>> is evaluated first yielding S<C<2 + 20 ==
-22>> and not S<C<6 * 5 == 30>>.
-
-I<Operator associativity> defines what happens if a sequence of the
-same operators is used one after another: whether the evaluator will
-evaluate the left operations first, or the right first. For example, in
-S<C<8 - 4 - 2>>, subtraction is left associative so Perl evaluates the
-expression left to right. S<C<8 - 4>> is evaluated first making the
-expression S<C<4 - 2 == 2>> and not S<C<8 - 2 == 6>>.
+I<Operator precedence> means some operators group more tightly than others.
+For example, in C<2 + 4 * 5>, the multiplication has higher precedence, so C<4
+* 5> is grouped together as the right-hand operand of the addition, rather
+than C<2 + 4> being grouped together as the left-hand operand of the
+multiplication. It is as if the expression were written C<2 + (4 * 5)>, not
+C<(2 + 4) * 5>. So the expression yields C<2 + 20 == 22>, rather than
+C<6 * 5 == 30>.
+
+I<Operator associativity> defines what happens if a sequence of the same
+operators is used one after another: whether they will be grouped at the left
+or the right. For example, in C<9 - 3 - 2>, subtraction is left associative,
+so C<9 - 3> is grouped together as the left-hand operand of the second
+subtraction, rather than C<3 - 2> being grouped together as the right-hand
+operand of the first subtraction. It is as if the expression were written
+C<(9 - 3) - 2>, not C<9 - (3 - 2)>. So the expression yields C<6 - 2 == 4>,
+rather than C<9 - 1 == 8>.
+
+For simple operators that evaluate all their operands and then combine the
+values in some way, precedence and associativity (and parentheses) imply some
+ordering requirements on those combining operations. For example, in C<2 + 4 *
+5>, the grouping implied by precedence means that the multiplication of 4 and
+5 must be performed before the addition of 2 and 20, simply because the result
+of that multiplication is required as one of the operands of the addition. But
+the order of operations is not fully determined by this: in C<2 * 2 + 4 * 5>
+both multiplications must be performed before the addition, but the grouping
+does not say anything about the order in which the two multiplications are
+performed. In fact Perl has a general rule that the operands of an operator
+are evaluated in left-to-right order. A few operators such as C<&&=> have
+special evaluation rules that can result in an operand not being evaluated at
+all; in general, the top-level operator in an expression has control of
+operand evaluation.
Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. Operators borrowed from
--
1.9.1
|
From @demerphqI vote yes. On 21 April 2016 at 04:05, James E Keenan via RT
-- |
From @iabyn
On Thu, Apr 21, 2016 at 09:24:20AM +0200, demerphq wrote:
Me too. -- |
From wolf-dietrich_moeller@t-online.deWhat happened to this proposal? Wolf -----Original Message-----
On Thu, Apr 21, 2016 at 09:24:20AM +0200, demerphq wrote:
Me too. -- |
From zefram@fysh.orgWolf-Dietrich Moeller (Munchen) wrote:
It got dropped on the floor, sorry. I've now patched the documentation -zefram |
@cpansprout - Status changed from 'open' to 'pending release' |
From @khwilliamsonThank you for filing this report. You have helped make Perl better. With the release yesterday of Perl 5.28.0, this and 185 other issues have been Perl 5.28.0 may be downloaded via: If you find that the problem persists, feel free to reopen this ticket. |
@khwilliamson - Status changed from 'pending release' to 'resolved' |
Migrated from rt.perl.org#127391 (status was 'resolved')
Searchable as RT127391$
The text was updated successfully, but these errors were encountered: