good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^4: can sub check context for lvalue vs rvalue context?by perl-diddler (Chaplain) |
on May 10, 2018 at 07:00 UTC ( [id://1214312]=note: print w/replies, xml ) | Need Help?? |
But it does benefit me to know. If it I got that it was called in rvalue context, that would be a problem. But that it is called in lvalue context, would be what I'd need. I'd want to detect that it was called in lvalue context to give it the more expensive tied var. I could return a simple variable if it was called in rvalue context, only, as an optimization -- since reading a var is often more frequently done than setting it.
The same type of situation already arises with "wantarray":
You could ask "well does sub get that it was called wanting an array or not?" Even though the assignment to the array is thrown away, it will will be called with wantarray=1; To say dual use subs should have been left as "experimental" because the end result is that only the scalar value is assigned to $x, is obviously not what has been done -- subs can return arrays or scalars based on context. The same situation holds with the no-longer-experimental ':lvalue' subs. They just need the same treatment as dual-return subs returning scalar or array -- i.e. a keyword to let the sub know context.
In Section
Seekers of Perl Wisdom
|
|