good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^3: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)by LanX (Saint) |
on Dec 27, 2020 at 19:37 UTC ( [id://11125815]=note: print w/replies, xml ) | Need Help?? |
> combined getter/setter ... Return the value when called without an additional arg, from my experience are mutators written in a way to always return the value. > A call with an undefined arg clears the attribute - just like in my example. OK ... but in that case why do you have a default value at all? Anyway, if you want to allow to set to undef and don't wanna introspect @_ , then you're stuck in a kind of semipredicate problem. Such problems can be solved in Perl by setting a new "impossible"° object MISSING as default, like use constant MISSING => bless {}, "MyPackg::__Missing__" and later sub mutator ($value = MISSING) { ...BODY... } (untested) If you see that an arguments equals to that "impossible" value blessed into your own private namespace below "MyPackg", you can be sure that it wasn't used by accident. HTH! :)
updateclearer rewording, clearer code
Cheers Rolf °) of course it's possible that such a value is used, but extremely unlikely without intend.
In Section
Seekers of Perl Wisdom
|
|