in reply to Best Method of Object-Oriented Behavior Addition?

You could also just create class Z, subclassing Y, which has a Z::A with your special stuff. Then create your objects of Z instead of Y on the condition.
BEGIN { package X; sub A {} sub B {} } BEGIN { package Y; @ISA=qw(X); sub B {} } BEGIN { package Z; @ISA=qw(Y); sub A {} } my $object = ($opt{specialcase} ? 'Z' : 'Y')->new(@newargs);

-- Randal L. Schwartz, Perl hacker


update: I added the BEGIN above. Too asleep when I hacked before to remember that. {grin}

Replies are listed 'Best First'.
RE: Re: Best Method of Object-Oriented Behavior Addition?
by jplindstrom (Monsignor) on Sep 30, 2000 at 18:25 UTC
    Excellent solution to the problem.

    Another one could be to use the "decorator" design pattern instead of subclassing. Here is an interesting discussion of when and why to use this pattern:

    http://www.stevenblack.com/Articles/PTN-Decorator.asp

    (it's not Perl, but you get the idea)

    From your description I would go with the subclass solution, but it's always good to know your options.

    /J