in reply to oo perl - using the super-class

Perl has a standard way of doing it already.

Create your sub-classes TransactionA and TransactionB, put Transaction in @ISA for each. Have your Transaction constructor respect inheritance by using the two-argument form of bless. For instance:

sub new { my $class = shift; my $self = {}; %$self = @_; # Whatever fits - this is just and example return bless($self, $class); }
And now your end code will be able to say:
my $trans = new TransactionA;
and it will all work.

For more take a look at this tutorial.

Replies are listed 'Best First'.
RE: Re (tilly) 1: oo perl - using the super-class
by kevinlyda (Initiate) on Oct 03, 2000 at 18:50 UTC
    hm, no, that's exactly what i don't want. i never want to have the main code say:
    $trans = new TransactionA
    i want to say
    $trans = new Transaction($request);
    and have $trans be an instance of TransactionA or TransactionB. i'm not up on oo terminology, but i think what i really want is a TransactionFactory class. again, i'm not sure but the above describes what i'm looking for. i'm just wondering if it's a good thing to look for or is there a better design to use?
      Your original code was fine. Just make sure that neither of your two specific types of transaction inherit from the generic transaction, or you're toast on the new routine!

      -- Randal L. Schwartz, Perl hacker

      Well you can do whatever you want with the two argument form of bless, but you will break inheritance.

      OTOH containment (ie making one property of an object be a member of a different class) is usually cleaner than inheritance anyways, but if you play this kind of game you really should document it to avoid potential grief for someone else.