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

Replies are listed 'Best First'.
Re: Report on Blessing to 'tag' an object
by jmcnamara (Monsignor) on Jan 21, 2003 at 08:54 UTC

    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.

    If necessary you could use UNIVERSAL::isa() to check if the blessed ref is an array ref:

    #!/usr/bin/perl -wl use strict; my @a; my @b; my $a = \@a; my $b = \@b; print ref $a; print ref $b, "\n"; bless $a; print ref $a; print ref $b, "\n"; print 'ARRAY' if UNIVERSAL::isa($a, 'ARRAY'); print 'ARRAY' if UNIVERSAL::isa($b, 'ARRAY'); __END__ Prints: ARRAY ARRAY main ARRAY ARRAY ARRAY

    --
    John.

      If necessary you could use UNIVERSAL::isa() to check if the blessed ref is an array ref:

      isa won't always work:

      my $o = bless {}, 'ARRAY'; print "$o isa array" if UNIVERSAL::isa($o, 'ARRAY'); __END__ ARRAY=HASH(0x759c) isa array

      When you want to know your implementation type you want reftype from Scalar::Util.

      use Scalar::Util qw(reftype); my $hash_object = bless {}, 'ARRAY'; my $array_ref = [1,2,3]; my $array_object = bless [], 'Foo'; foreach ($hash_object, $array_ref, $array_object) { print "reftype of ", $_, " is ", reftype($_), "\n"; }; __END__ reftype of ARRAY=HASH(0x759c) is HASH reftype of ARRAY(0x7650) is ARRAY reftype of Foo=ARRAY(0x74b8) is ARRAY

        That won't always work:     Can't locate Scalar/Util.pm in @INC Which do you think is more likely? 1) That I blessed a non-array reference into the package named 'ARRAY' and I'll be upset that UNIVERSAL::isa() gets confused or 2) That I don't have Scalar::Util ?

        I think Scalar::Util has become "core" and so in a few years, the probabilities will shift. For now, I much prefer UNIVERSAL::isa() because I like my code to work on relatively old versions of Perl and I don't mind that it is possible to break my code by doing extremely stupid things. q-:

                        - tye