It turns out that blessing the ref was particularly good in my situation. In Exporter::VA, it looks at a rich structure you give it, probably as a literal, so it's made up of annonomous hash refs and annonomous array refs and plain scalars. That is, you create it directly out of primitives (braces and brackets), not by calling constructor functions to create objects and then feeding them to other objects.
In my code, an array means one thing, a scalar something else, etc. and the first thing it does is see what kind of value it has. This is run-time "overloading" of the function.
Well, a side-effect I hadn't thought of before I tried it is that when blessing the array ref, it's no longer an 'ARRAY' and my switching on the value of ref breaks.
Ah, but then I embraced it. In another place, I have two kinds of arrays, which it further distinghishes (after figuring out that it's an array) by content. Once I figured out which it was the first time, I blessed it as a specific type. Now my first level overloading code can immeditly switch to the case for this-kind, that-kind, or not-yet-known. The latter figures it out, blesses it to remember that, and then tries again (now it knows).
Likewise, in the original case where I was putting the array into a canonocal form, I no longer have to even check if it's been done via some kind of tag. The check was done for free when it figured out it was an array in the first place. It distinghished 'ARRAY' from 'ARRAY-seen' and directly called the code that first normalized it, or called code that knew it didn't have to. I got one more decision out of it for no extra cost.
Now, it's been said that any time you have a switch statement based on type, you have an object just waiting to happen. I agree there. I could have turned all the values into blessed objects, and then just called a polymorphic function on the value thereafter. But, that doesn't directly support multi-methods, and I'd have a whole bunch of small classes just to hold that function. Maybe it's still a good idea moving forward, but I'm pretty happy with the way it turned out.
—John
In reply to Report on Blessing to 'tag' an object by John M. Dlugosz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |