in reply to inheriting from Data::Table

If I understand you correctly, why not directly derive from Data::Table? Something like:

use strict; use warnings; use Data::Table; package SQLTable; push @SQLTable::ISA, "Data::Table"; sub new { my ($class, $dbh, $sql, $vars) = @_; return $class->fromSql($dbh, $sql, $vars); } package main; my $dbh = ...; my $sql = ...; my $vars = [...]; my $obj = SQLTable->new($dbh, $sql, $vars);

The code above is untested obviously (it'd barf on the yada-yada operator), but should be enough to get you going.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: inheriting from Data::Table
by Anonymous Monk on Mar 17, 2012 at 12:06 UTC

    Thanks for responding. The reason for the extra level of inheritance (Data::Table <- MyTable <- TableA): I'll have many of these TableA-type classes and then if I wanted to add any extra functionality, I could add it in MyTable. Also, I figured that any complexity involved with inheriting can be tucked away inside MyTable. Any other thoughts?

    thanks, Michael

      Create an instance of Data::Table inside a Moose class. If you think this would be the cause for multiple inheritance (which is really not a good thing) them look at the Moose::Role.

      It might look as a waste of time in the beginning having to learn a new OO system, but you will see that you'll write better code in less time shortly.

      Alceu Rodrigues de Freitas Junior
      ---------------------------------
      "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Re^2: inheriting from Data::Table
by Anonymous Monk on Mar 17, 2012 at 17:09 UTC

    I guess I'm not making my question very clear. How do you derive from a class that uses the Factory pattern for the constructor (i.e. Data::Table::fromSQL). Can someone point me in the right direction (any articles would be good too.)

    thanks! Michael

      Data::Table isn't an object factory. The various "from" constructors are just that, constructors. Data::Table has multiple constructors, but there is nothing particularly magical about them. The SQLness of fromSQL isn't provided by a separate class, it is simply what the fromSQL constructor does.

      Can you give an example of the problem you are trying to solve in the context of the following code?

      use strict; use warnings; package Table; sub MakeTypeA { my ($class, %params) = @_; return $class->TypeA::new(%params); } sub MakeTypeB { my ($class, %params) = @_; return $class->TypeB::new(%params); } sub ShowType { my ($self) = @_; print "$self->{type}\n"; } package TypeA; push @TypeA::ISA, 'Table'; sub new { my ($class, %params) = @_; return bless {type => 'TypeA', %params}, $class; } sub foo { print "TypeA::foo\n"; } package TypeB; push @TypeB::ISA, 'Table'; sub new { my ($class, %params) = @_; return bless {type => 'TypeB', %params}, $class; } sub baa { print "TypeA::baa\n"; } package MiddleMan; push @MiddleMan::ISA, 'Table'; sub ShowType { my ($self) = @_; print 'MiddleMan, '; $self->SUPER::ShowType(); } sub ShowClass { my ($self) = @_; print ref $self, "\n"; } package MyClass1; push @MyClass1::ISA, 'MiddleMan'; package MyClass2; push @MyClass2::ISA, 'MiddleMan'; package main; my $obj1 = MyClass1->MakeTypeA(); my $obj2a = MyClass2->MakeTypeA(); my $obj2b = MyClass2->MakeTypeB(); $obj1->ShowType(); $obj1->ShowClass(); $obj2a->ShowType(); $obj2a->ShowClass(); $obj2b->ShowType(); $obj2b->ShowClass();
      True laziness is hard work