sozinsky has asked for the wisdom of the Perl Monks concerning the following question:
Then you would have a simple class my_database::foo with members int and baz. I'd like to solve this by having a Table package which handles the creation of the my_database::foo package for you. IE as a user you'd simplycreate table my_database..foo ( bar int , baz varchar(10) )
</code> The Table class's import method() will query the local database my_database for the columns of the foo table, and then (I use Class::MethodMaker ) construct the class defintion of my_database_foo ( at _compile_ time!). So, specifically,use Table ( { -dbase => "my_database", -table => "foo" } ); my $f = my_database::foo->new( { bar => 1, baz => "neat" } );
Perhaps another way to solve this would be to subclass the Table package , passing it the database and table name. Perhaps this is better, because ( this is another question ) would I ever be allowed to do something like this?#Table's import method sub import { my ( $class, @args ) = @_; my $href = $args[0]; #sanity checking on href... my ( $dbase, $table ) = ( $args->{-dbase}, $args->{table} ); #get_columns returns a reference to an array #containing the columns. my $columns = get_columns( $dbase, $table ); #this is where I get stuck. package $dbase::$table; @ISA = qw(Table); use Class::MethodMaker; make methods get_set => $columns, new_hash_init => 'init'; #broquaint suggested this, but he didn't like it #"this is just so very very wrong" --broquaint #the idea is to export the $dbase::$table package #definition to the caller *{"main::$dbase::$table::"} = *{"$dbase::table"}; }
as opposed touse Table ( { -dbase => "mydb", -table => "foo" } ); use Table ( { -dbae => "someotherdb", -table => "bar" } ); #results in the exporting of 2 class definitions: #mydb::foo and someotherdb::bar
Anyhow, hope this isn't too rambling, thanks all in advance.package mydb::foo; @ISA = qw(Table); package someotherdb::bar; @ISA = qw(Table);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: constructing dynamic class definitions at compile time (from schema definition)
by dragonchild (Archbishop) on Jul 25, 2002 at 14:32 UTC | |
by sozinsky (Initiate) on Jul 25, 2002 at 16:21 UTC | |
by dragonchild (Archbishop) on Jul 25, 2002 at 18:48 UTC | |
|
Re: constructing dynamic class definitions at compile time (from schema definition)
by broquaint (Abbot) on Jul 25, 2002 at 14:26 UTC |