Recently I played with the logical/defined or in a scenario where the result should be an lvalue. For the defined or (//) the documentation clearly states that the result is not an lvalue. While this is not mentioned for the logical or (||), it holds there too.

I don't know the reason for this behaviour, but it prevents the usage of these operators as a substitute for the non-existing (binary) "Elvis operator" (?:), where X ?: Z would act like X ? X : Z, without evaluating X twice.

To my surprise I realized that dereferencing a reference to the result of an or operator provides an lvalue, given the operands are lvalues. That is, the following expression does compile and works: ${\(X // Y)}++.
I could not find anything in the documentation that would forbid such a usage.

So my questions are:

Here is a complete example:

#!/usr/bin/perl use v5.24; use warnings; use experimental 'signatures'; say "This is perl $]"; sub f :lvalue ($k) { state $href = {a => 10}; $href->{$k}; } sub g :lvalue ($k) { state $href = {b => 20}; $href->{$k}; } eval q{ (f($_) || g($_))++ for qw(a b); 1; } or warn $@; ${\(f($_) // g($_))}++ for qw(a b); say "f(a) = ", f('a'); say "g(b) = ", g('b'); __DATA__ Can't modify logical or (||) in postincrement (++) at (eval 5) line 2, + near ")++" This is perl 5.032001 f(a) = 11 g(b) = 21

Greetings,
🐻

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

In reply to Logical/defined or as lvalue by jo37

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.