Re: Logical/defined or as lvalue
by hv (Prior) on Sep 03, 2024 at 21:54 UTC
|
What is the reason for the logical/defined or operators not to provide an lvalue as result?
I don't know. It may have been oversight, or there may have been an explicit or implicit rationale that has most likely been lost in the mists of time.
Would changing this behaviour break any existing code?
My first guess would be autovivification: $h{$_} behaves differently in lvalue and rvalue contexts, which is already enough to cause regular confusion. Also any classes that provide overloading of these operators is likely to have been designed to the current behaviour.
Is the usage of ${\(X // Y)} as an lvalue well-defined behaviour?
I think it probably depends on X and Y, but in the cases where it is legal to take the reference, I think I'd expect the dereference to be well-behaved. In other words: where it works, I wouldn't expect it to suddenly stop working.
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
| [reply] |
|
My first guess would be autovivification: $h{$_} behaves differently in lvalue and rvalue contexts,...
True, but currently the usage of X // Y as an lvalue results in a compile error.
I'm not sure if this is any different in overloading.
Update:
One overlooked detail:
Also any classes that provide overloading of these operators...
Logical operators cannot be overloaded.
Greetings, 🐻
$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] |
Re: Logical/defined or as lvalue
by LanX (Saint) on Sep 03, 2024 at 19:57 UTC
|
I wasted too much time on this ;)
Anyway, my best suggestion, please don't use this deref-ref syntax for the sake of readability
Try something like the following ors or defs and you are free to chain even more than two operators and assign too.
sub ors :lvalue {
$_ && return $_ for @_ ;
return $_[-1];
}
# define analogous &defs
ors($x,$y,$z) = 42;
This could also be included in a module like List::Util (if really missing, compare any and all )¹
Regarding documentation: the perlop Doc for // also covers || and &&.
They talk at length about returning the scalar value of the first operand because of the Boolean test, this might be related.
FWIW, I was also experimenting with
sub lv(&) :lvalue { ${ \( $_[0]->() ) } }
lv{ $a // $b } = 666;
But this doesn't work because I can't specify that the prototype & code-block is an lvalue too. ( Probably a posteriori???)
Finally I discovered what looks like a bug at least in 5.36 inside the debugger.
feature 'signatures' seems to be enabled by default when testing sub declarations inside a prompt of perl -de0
¹) I'm having a deja-vu right now, did we discuss this before? | [reply] [d/l] [select] |
|
| [reply] |
Re: Logical/defined or as lvalue
by ikegami (Patriarch) on Sep 03, 2024 at 19:43 UTC
|
| [reply] [d/l] |
|
We have ($w? $x : $y)= $z so why not?
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
|
|
|
|
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.
| [reply] [d/l] |
|
${\($h1{$_} // ($h2{$_} //= 0))}++ for @list;
Greetings, 🐻
$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] |