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,
🐻
In reply to Logical/defined or as lvalue by jo37
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |