This doesn't work because @ISA is a class variable, not an instance variable. You're trying to change the behavior of instances within one class, so you'd have to do that with differences in the instances. On initialization, you can take note of what the "base" class is to be and then use that within the instance.

package foo; sub new{ my $class = shift; my $base = shift; return bless { base_class => $base }, $class; } sub write{ my $self = shift; my $base = $self->{base_class}; print eval "\$self->$base\::read"; }

I used eval because my other attempts at getting this to work didn't.

no strict 'refs'; print $self->&{"$base\::read"}; # syntax error # string found where operator expected print $self->"$base\::read"; # executes but ignores $base's inheritance, if any no strict 'refs'; print &{"$base\::read"}( $self );

This is not to say I recommend using eval for this. In fact, I think it's another argument that what you're trying to do isn't a very good idea.

I'd encourage you to consider object composition instead of some funny inheritance.

If you're dead set on this, you'll probably have to resort to something like what I did in Overloading different instances differently.. Every instance will be in its own package set up with its own @ISA that refers to both "Foo" and either "Alpha" or "Beta". Or something like that.

This kind of works too:

# code above package foo unchanged package foo::alpha; BEGIN { @foo::alpha::ISA = qw( foo alpha ); } package foo::beta; BEGIN { @foo::beta::ISA = qw( foo beta ); } package foo; sub new{ my $class = shift; my $base = shift; return bless {}, "$class\::$base"; } # all other code unchanged

I can't say I recommend that either, but it's better than eval.


In reply to Re: Dynamically setting up inheritence at runtime by kyle
in thread Dynamically setting up inheritence at runtime by hangon

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.