in reply to Situation where warning "Found = in conditional, should be" seems obsolete

> But in perlop operators and and or are not named being conditional.

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.

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.

what do you mean by "named being conditional" and how could the description be clearer?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re: Situation where warning "Found = in conditional, should be" seems obsolete (documentation)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Situation where warning "Found = in conditional, should be" seems obsolete (documentation - C terminology)
by LanX (Saint) on Feb 06, 2021 at 19:08 UTC
    > what do you mean by "named being conditional" and how could the description be clearer?

    This problem is borrowed from C, which was one of the templates for designing Perl

    "Conditional operator" seems to be the original term for the "ternary operator"

    I have an almost original copy of Kernighan/Ritchie in my bookshelf - though in German - and it's introduced as "Conditional Validation" ("Bedingte Bewertung")

    I agree with you that the name is unfortunate and I've never heard using it in Perl's context. "Ternary Operator" is the "normal" term.

    I'd suggest:

    changing the title to "Ternary Operator" and appending an example exemplifying the analogy to if-then-else

    https://perldoc.perl.org/perlop#Conditional-Operator

    * Ternary Operator

    Ternary "?:" is like the conditional operator in C.

    It works much like an if-then-else. ... yadda yadda ..

    $max = ($a > $b) ? $a : $b

    has the same effect like

    $max = do { if ($a > $b) {$a} else {$b} }

    I'd also add similar code examples to and , && , or and ||

    • "It works much like an if-then"
    • "It works much like an unless-then"

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I agree with you.
      You gave a good answer instead of me :)
      >>what do you mean by "named being conditional"
      I mean that when I press CTRL+F and ask for "condit..." in perlop it didn't land into 'and', 'or' sections.
      By the way a warning warns about '=' been found inside "conditional", so an operator 'and' worked with "conditional". Sorry if I'm not pretty good at understanding english.

      And how about replacing infamous variable names '$a' and '$b' with foo/bar or so.

      UPD.: I changed my mind and now I disagree with '?:' naming 'Ternary operator' and suggest explicitly name it 'Ternary conditional operator'. Because I think that the name 'ternary' is too abstract and meaningless (it only says about how much operands it operates on). OK, it is the only ternary operator by now, but why not future Perl include other ternary operators?!
        Terminology is not easy if it involves history and parallels to other (normative) languages.

        see also https://en.wikipedia.org/wiki/Conditional_operator

        The Conditional operator is supported in many programming languages. This term usually refers to ?: as in C, C++, C#, Python, and JavaScript.

        However, in Java, this term can also refer to && and ||

        When Perl was born, C-programmers where one of the main target groups.

        But as you see "Java" is doing it like you suggested.

        And in Perl it's not only that

        • and has the same "flow control" like if
        but also
        • if returns a value like the operand and

        As I said, both are implemented with the same OP-Code.

        edit

        At the same time we want documentation to stay concise, otherwise it's considered "too complicated"

        I think adding an extra section "Control flow in conditional operators" with these deeper informations and linking to it from ?: and && or || might solve the issue.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^2: Situation where warning "Found = in conditional, should be" seems obsolete (documentation)
by rsFalse (Chaplain) on Feb 06, 2021 at 21:00 UTC
    Then maybe there should be the only one description?!

    I mean that it is enough to describe one operator, and only get a link to it with explanation that difference is only precedence. This would become SPOT - single point of thuth.

    I suggest to rename these two groups of operators.
    1. They are EQUIVALENT except for their precedence, that means that they are both C-style or both not-C-style. Am I right?
    2. The name of operators should differ in the way they differ from each other, so I suggest for example: 'Low-precedence-Logical-And' and 'High-precedence-Logical-And'.
    3. As 'and' ('or') operates on so called "conditional" (on its left side) and also it returns the last evaluated expression (or so), I suggest further to rename it from 'Logical-And' to something else, if someone can find more suitable name. E.g. I would suggest 'Conditional-And' (therefore 'High/Low-precedence-Conditional-And'), how does it sound?

    Also I suggest to make a link from documentation of operator 'and' to the documentation of statement 'if then' or vice versa. Why? For having SPOT. All the same behavior in one place, except for explanation of differences(?) (as I know they are not fully equivalent, because we can not use the return value of 'if' directly (e.g. we needs to envelope it with do{ })).

    How do you think?

    Upd.: Oh... I just realized that I made a suggestion (in 3rd point) the same as it is used in Java as you wrote earlier :( (Re^4: Situation where warning "Found = in conditional, should be" seems obsolete (documentation - C terminology)).
      Also I want to suggest another thing in documentation, which is a bit related:
      Now in documentation there is a section 'Logical or and Exclusive Or' - https://perldoc.perl.org/perlop#Logical-or-and-Exclusive-Or. Both operators 'or' and 'xor' has the same precedence. But I think they differ from each other by significant: 1) one short-circuits, another - doesn't, 2) one - returns only 1 or ''/0, another returns last expression (in case of left expression it is returned evaluated). I suggest to split it into separate sections.
      As they were both in one section, I though of their similar behavior, and once I thought 'xor' will return last expression not 1 or ''/0 (related node: 'xor' operator is not a sibling to 'or' and 'and'?).
        PerlMonks is not the maintainer of the documentation. You can always open a documentation Pull Request in Perl's GitHub.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]