Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

inheriting from Data::Table

by Anonymous Monk
on Mar 16, 2012 at 21:56 UTC ( [id://960074]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi perlmonks,

I'm trying to inherit from Data::Table, but I'm having difficulty since I need to use Data::Table::FromSql to load the data. I've found a solution by inheriting from MyTable which has an instance of a Data::Table and then uses AUTOLOAD to redirect calls from TableA to Data::Table. The highly simplifed code is shown below. I've left many of the details out.

package MyTable sub new { ... } sub buildFromSql { $self->{t} = Data::Table::fromSql($dbh,$sql); } sub AUTOLOAD { my $self = shift; my $command = our $AUTOLOAD; $command =~ s/.*://; $self->{t}->$command(@_); }
package TableA @ISA = ("MyTable"); sub new { $self = $class->SUPER::new(@_); $self->buildFromSql($dbh,$sql) }
## CLIENT my $table = new TableA(); $table->html2."\n";

This works but I'm wondering if there are better ways to do this. Can anyone suggest any ways of improving this? thanks, Michael

Replies are listed 'Best First'.
Re: inheriting from Data::Table
by GrandFather (Saint) on Mar 17, 2012 at 02:02 UTC

    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

      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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://960074]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found