Front page | perl.perl5.porters |
Postings from April 2016
[perl #127391] possible inconsistency in "perlop" documentation onassociativity of operators
Thread Next
From:
James E Keenan via RT
Date:
April 21, 2016 02:05
Subject:
[perl #127391] possible inconsistency in "perlop" documentation onassociativity of operators
Message ID:
rt-4.0.18-8044-1461204336-216.127391-15-0@perl.org
On Mon Feb 01 09:29:42 2016, zefram@fysh.org wrote:
> Wolf-Dietrich Moeller wrote:
> >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?
>
> Ah yes, the text you quote (and the preceding paragraph about precedence)
> is indeed incorrect for short-circuiting operators.
>
> >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?
>
> I'd rather not treat short-circuiting as a special case, because it's not,
> for this purpose. If anything the special case is the simple operators,
> and that's probably worth bringing out explicitly. I suggest those two
> paragraphs should become
>
> 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.
>
> -zefram
>
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.
--
James E Keenan (jkeenan@cpan.org)
---
via perlbug: queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=127391
Thread Next
-
[perl #127391] possible inconsistency in "perlop" documentation onassociativity of operators
by James E Keenan via RT