develooper Front page | perl.perl5.porters | Postings from April 2016

[perl #127921] possible inconsistency in "perlop" documentation onprecedence of || and //

Thread Previous
Wolf-Dietrich Moeller
April 18, 2016 15:40
[perl #127921] possible inconsistency in "perlop" documentation onprecedence of || and //
Message ID:
# New Ticket Created by  Wolf-Dietrich Moeller 
# Please include the string:  [perl #127921]
# in the subject line of all future correspondence about this issue. 
# <URL: >

I think the following issue in the “perlop”-documentation needs clarification.

In the section on “Operator Precedence and Associativity” there is the text:
“Operator precedence means some operators are evaluated before others. For example, in 2 + 4 * 5, the multiplication has higher precedence so 4 * 5 is evaluated first yielding 2 + 20 == 22 and not 6 * 5 == 30.”
(BTW, this text is more clarified wrt. grouping in the new proposed text in perl bug #127391).

And further below:
“In the following *sections*, these operators are covered *in precedence order*.”

In the text further below in perlop these operators are listed in *separate* sections:
  C-style Logical Or
  Logical Defined-Or
This means that “C-style Logical-Or” should have higher precedence than “Logical Defined-Or”.

The test program below has no grouping in line 3, but gives an explicit order of evaluation by grouping in lines 4 and 5:
# test for precedence of || and //
my ($x,$y) = (0,1);
print   $x // $y || 'last3',"\n"; # line 3
print+ ($x // $y) || 'last4',"\n"; # line 4
print   $x // ($y || 'last5'),"\n"; # line 5
This program yields the output:
This result shows that both operators in line 3 have the same precedence, as they are evaluated left-to-right (same as line 4). According to the perlop-documentation, line 3 should yield the same result as line 5 (higher precedence for “||”).

My suggestion is to combine the two sections on “C-style Logical-Or” and “Logical Defined-Or” in one section, as this would clearly show that both operators have the same precedence (as implemented in Perl).
(Changing the implementation of Perl to give higher precedence for “||” is probably no good idea).

I tested this issue with both Perl 5.16 and Perl 5.22, and probably it existed since the introduction of “Logical Defined-Or”.

Best regards
Wolf Moeller

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About