A short time ago I asked for ideas on how to "tag" a ref as already having been processed. shotgunefx was inspired to implement a full-blown XS solution. I went ahead and used a blessing.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.