in reply to [SOLVED] Moose trait (?) to set "undef" to, say, "empty string?"

Weird.   I tried that.   Didn’t work.   The coercion never got called.   (Pardon me, don’t take this wrongly...)   Did you actually try the code that you posted here?   (I will be delighted if the answer is, “yes.”)

Hmmmm....   Note that I did not (yet...) try specifying coerce=>1.   Is that the magick?

Replies are listed 'Best First'.
Re^2: Moose trait (?) to set "undef" to, say, "empty string?"
by Your Mother (Archbishop) on Nov 29, 2011 at 14:18 UTC

    If by magick you mean the well documented way in which coercions work, the answer is yes. :\

    Moose::Manual::Types: Moose will never try to coerce a value unless you explicitly ask for it. This is done by setting the coerce attribute option to a true value...
    Moose::Cookbook::Basics::Recipe5: However, defining the coercion doesn't do anything until we tell Moose we want a particular attribute to be coerced...
    Moose: coerce => (1|0) This will attempt to use coercion with the supplied type constraint to change the value passed into any accessors or constructors. You must supply a type constraint, and that type constraint must define a coercion...

    See also, Moose::Manual::FAQ and probably other spots.

      Must be getting too old for this ...   I guessed (correctly) that it had to do with type-constraints and with coercions, but simply overlooked the importance of that flag.   I guess I decided too-quickly that maybe it was looking for a particular type-definition and not matching the coercion specification, instead of not attempting any coercion at all.   Merely specifying a coerce rule doesn’t cause it to be invoked.   Sigh.   Shows what ’ya get sometimes, for chasin’ them red herrings and jumpin’ to conclusions ...

      Thank you, sincerely, for pointing out these documentation references to me.

Re^2: Moose trait (?) to set "undef" to, say, "empty string?"
by ikegami (Patriarch) on Nov 29, 2011 at 14:34 UTC
    No, I didn't try that code specifically, but I have done coercions many times before. Yes, it won't perform coercion without coerce=>1. Couldn't you look it up instead of asking me that?

      Oh, sure ... it’s just that I feel so ... 8~<:*} ... stoopid to have been so close to the right answer and yet to have missed it... (swish!!)   Hope you’re not (seriously) offended.   Mea culpa.

      Thank you, in all seriousness, for your valuable assistance.   Upvoted.