Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Thursday April 19, 2007
09:07 AM

Not Enough Abuse (bummer)

[ #33035 ]

Try as I might, I can't get two instances of a class to have different inheritance trees at the same time. @ISA is a package variable, so changing it affects all instances. Hmm, can @ISA be tied? You'd have to invalidate the cache every time you did anything and this would be bad, wicked, evil, wrong, etc., but I'm still curious to know if it can be done.

Or ... maybe I could bless every instance into an anonymous subclass and ... oh wait, I can't tie symbol tables. Damn. Maybe I could use anonymous subclasses, do some introspection and pulled out method names and provided wrapper which switched @INC that way...

ACME::DiddleMyInc needs to be written!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The closest I've come up with so far is this:

    #!/usr/bin/perl -l

    use strict;
    use warnings;

    {

        package KillMeNow;
        sub whee { return 'whee!' }
        sub new { bless {}, shift }
    }

    {

        package DontKillMe;
        sub whee { return 'asdfasdf' }
    }

    {

        package KillMeLater;
        @KillMeLater::ISA = 'KillMeNow';
        use overload '%{}' => sub {
            @KillMeLater::ISA =
              rand 1 > .5

  • We need support for coderefs @ISA, like in @INC :)
    --
    life is short
    • Yeah, we were talking about that here at work. It would have solved an annoying little problem here (well, more of a hack to get around some buggy CPAN code).

  • What effect do you really need? You could use an AUTOLOAD which can build a dispatch path and query it on a per-object basis. Or you could generate a new package for each object with the proper path to which calls are delegated by the apparent class. Fixing isa() would not be too difficult, I think.

    This would not affect @ISA, though, if you need that to work.
    --
    rjbs
  • Why would you want to do that?
  • I use instance-based modifications to classes by using Moose and MooseX::Object::Pluggable which I wrote. The basic idea is to use Class::MOP to generate an anonymous class that has feature XYZ you want that inherits from your original class. sounds like you already thought of it, but it works quite nicely and easily with the introspection
  • ... and why do you think that necesarily needs to involve changing @ISA?

    @ISA is just an array store used by the default implementation of the ->isa method to determine is something isa something else, and by the method resolved.

    If you want to fake being an object, them perhaps just overload ->isa?

    Or then there's a Decorator object...

    I'm not sure why making objects do weird things needs to involve @ISA.
    • It's not that I want to have different instances with different inheritance trees. I just want to know if it can be done. It's a matter of curiosity, that's all.