Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.93k stars 552 forks source link

[doc] perlop: Improving description of operators '&&', '||', 'and', 'or' AND possible renaming of them (and '?:') #18572

Open rsFalse opened 3 years ago

rsFalse commented 3 years ago

Where https://perldoc.perl.org/perlop#C-style-Logical-And https://perldoc.perl.org/perlop#C-style-Logical-Or https://perldoc.perl.org/perlop#Logical-And https://perldoc.perl.org/perlop#Logical-or-and-Exclusive-Or https://perldoc.perl.org/perlop#Operator-Precedence-and-Associativity

Description I. Descriptions

  1. Operators '&&' and 'and', also '||' and 'or' are equivalent except their precedence. I suggest to move descriptions of these operators from two places to one place (e.g. move descriptions of both '&&' and 'and' under section '&&' or 'and'), leaving another place empty. Then put the two similar sentences to the beginning of both sections meaning that operator '&&' is equivalent to 'and' except its higher precedence, and vice versa.
  2. To do same with ORs.
  3. To add text explaining that '&&' is similar to '... if ...' (but later is not an expression) or 'do{ ... if ... }' (?) and give link to it.

Maybe it is reasonable to leave examples separated in different subsections? But I suggest that general description should be in one place (also for obtaining SPOT).

Now there is: https://perldoc.perl.org/perlop#C-style-Logical-And --> Binary "&&" performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated. https://perldoc.perl.org/perlop#Logical-And --> Binary "and" returns the logical conjunction of the two surrounding expressions. It's equivalent to && except for the very low precedence. This means that it short-circuits: the right expression is evaluated only if the left expression is true.

In '&&' case it is not clear what operator returns. In the 'and' case it is not clear also, because e.g. an array being a result of "logical conjunction" sounds weird.

I wrote here how do I see an improvement (deleted changes aren't visible, but added words are in italic): https://perldoc.perl.org/perlop#C-style-Logical-And --> Binary "&&" is equivalent to binary 'and' except for the high precedence. [Somewhere here may be a link to move to a description of sibling operator]. "&&" (and 'and') performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated. If left operand is false it is returned evaluated and without context propagation. If left operand is true operator returns the right expression in context propagated. + '&&' is equivalent to '... if ...', except that operand sides are inverted and that later is not an expression (but it can be enveloped with 'do{ ... if ... }' to retrieve a result).

https://perldoc.perl.org/perlop#Logical-And --> It's equivalent to && except for the very low precedence. [Somewhere here may be a link to move to a description of sibling operator]. THE END.

II. Renaming Does the renaming of 'Logical' with 'Conditional' sound better? I suggest to discuss if e.g. 'Conditional-And' is better name for this operator than 'Logical-And'. And also does it sound better if we name them explicitly by their precedence: 'Low-precedence-XXX-and' and 'High-precedence-XXX-and' instead of 'C-style-XXX-and' and '("Non-C-style?"-)XXX-and'? Also I suggest to rename 'Ternary operator' with 'Ternary conditional operator', because 'Ternary' means only an arity attribute. Now it is Perl's only operator which works with 3 operands, but maybe in the future more 'ternaries' will come? Sorry. Now the name of operator is 'Conditional operator' (so named section: https://perldoc.perl.org/5.30.0/perlop#Conditional-Operator ). And I suggest to rename it to 'Ternary conditional operator' or so, because it is not the only conditional operator among all (i.e. 'and', '//' are also conditional).

These suggestions are logical copy of my recent suggestions in perlmonks forum: https://perlmonks.org/?node_id=11127993

Grinnz commented 3 years ago

The perlop document is explicitly ordered by precedence, as described at the end of https://perldoc.perl.org/perlop#Operator-Precedence-and-Associativity. Therefore, it would be inappropriate to combine the sections for operators with different precedence.

rsFalse commented 3 years ago

The perlop document is explicitly ordered by precedence, as described at the end of https://perldoc.perl.org/perlop#Operator-Precedence-and-Associativity. Therefore, it would be inappropriate to combine the sections for operators with different precedence.

No no, I don't mean to combine them leaving only one point (one subsection). I mean that all information from both sections should be in one. And another section should be almost empty. I'm not sure I wrote clear. But look at my example.

jkeenan commented 3 years ago

The perlop document is explicitly ordered by precedence, as described at the end of https://perldoc.perl.org/perlop#Operator-Precedence-and-Associativity. Therefore, it would be inappropriate to combine the sections for operators with different precedence.

No no, I don't mean to combine them leaving only one point (one subsection). I mean that all information from both sections should be in one. And another section should be almost empty. I'm not sure I wrote clear. But look at my example.

Given what @Grinnz has pointed out, I am skeptical that we would adopt your request for a change in this documentation. However, if you were to supply a pull request or patch it would be easier to evaluate your request than the way it was provided in your original request.

Thank you very much. Jim Keenan