in reply to Logical/defined or as lvalue

Is ( X || Y ) = Z; really something we want to see in our code? When would it be useful?

Replies are listed 'Best First'.
Re^2: Logical/defined or as lvalue
by NERDVANA (Priest) on Sep 03, 2024 at 21:13 UTC
    We have ($w? $x : $y)= $z so why not?
      Well yes, according to Wikipedia is PHP implementing the "Elvis operator"
      • $x ?: $y
      as
      • $x ? $x : $y

      But I think Perl has bigger fish to frie.

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

        My point was that if the ternary conditional is an lvalue, then I don't see why not to make the '||' and '//' operators lvalues. It would result in better consistency.
        ... implementing the "Elvis operator" ... as $x ? $x : $y

        Actually, I read the opposite from the Wiki article:

        ... the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional expression A ? A : B

        ... something like ... x = f() ?: g() ... is equivalent to ... the conditional ternary operator x = f() ? f() : g() except that it does not evaluate f() twice if it yields truthy.

        Accentuations by me.

        Greetings,
        🐻

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      Many reasons why not. I already gave the two primary ones: lack of readability (due to its rare and unexpected use) and lack of usefulness.

      If anything, I read your post as an argument against ( W ? X : Y ) = Z. But that has at least some modicum of usefulness.

Re^2: Logical/defined or as lvalue
by jo37 (Curate) on Sep 03, 2024 at 20:12 UTC
    When would it be useful?

    Consider items from @list to be counted, with one "given" set and a "rest". A pre-initialized hash %h1 holds the "given" set and an empty hash %h2 counts the rest. Then we could:

    ${\($h1{$_} // ($h2{$_} //= 0))}++ for @list;

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      If you're going to count them all, I don't see the point of having the counts split across two hashes. If there's an important distinction between the two sets of strings, you would need those lists, and you could access the counts of either lists.

      If I wanted to do this, I'd count only in one hash and use a hash-slice to extract the "given" ones.

      Less code and more flexible.

      Update

      Alternatively a HoH where the first key decides what is given or rest.

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