Re: Using class::dbi::factory with multiple table classes in a single file

[prev] [thread] [next] [Date index for 2005/04/05]

From: Thomas L Shinnick
Subject: Re: Using class::dbi::factory with multiple table classes in a single file
Date: 18:41 on 05 Apr 2005
At 13:05 4/5/2005, Barry Dancis wrote:
>Hi--
> 
>    Is there a way to have the class::dbi subclass definitions all in the same file and  use class::dbi::factory?
>If I place each subclass in its own file, factory works fine. If I collect them all into a single pm file, I get an error when I try to get the colums
> 
>    my @columns = $WebCams->columns ('assay');
>and the message is:
> 
>    failed to load class 'Assay': Can't locate Assay.pm in @INC

Total speculation here, but I wonder if this situation is the reason for a bit of code I found in CGI::Prototype::Hidden :

  eval "require $package" unless eval "defined %${package}::";

Could it be that this strange checking is done to allow for multiple classes to be (already) defined within one file, rather than use the "one class will be in one file" search done by require, as in Class::Factory routine _require_class :

  eval "require $class";
  throw Exception::SERVER_ERROR(-text => 
      "failed to load class '$class': $@") if $@;


>    The same aggregate pm file works fine without the factory. If I create a separate file for Assay and reference it (use Assay;) in my code, Factory works fine. If  I delete the reference to Assay, it still works (at least it can get the column definitions)?!
> 
>    Sample code is shown below.
> 
>Thanks,
> 
>Barry
> 
>ps The following also does not work:
>   $WebCams->use_classes (qw/WebCamsDb::Assay WebCamsDb::Results WebCamsDb::Experiment/);
>    my @columns = $WebCams->columns (lc('WebCamsDb::assay'));
>=====================================================================
>(File WebCamsDb.pm)
>#!c:/perl/bin/perl
>package WebCamsDb;
>use strict; use warnings;
>#-----------------------------------------------------------------------------
>{package WebCams_Server;
>use strict; use warnings;
>use lib 'C:/Development/Perl/database';
>use base qw(Class::DBI);
>sub MODULE_NAME { __PACKAGE__ }
>sub class_title {shift->table}
>sub class_plural {shift->table}
>sub class_description {shift->table}
>1;}# end Package WebCams_Server
>#-------------------------------------------------------------------------------------------------------------------------------------------------------{package Assay;
>use strict;
>use warnings;
>use base 'WebCams_Server';
>__PACKAGE__->table (__PACKAGE__);
>__PACKAGE__->columns(All => qw/ name description executable comments timestamp /);
>1;}
>#-------------------------------------------------------------------------------------------------------------------------------------------------------{package Results;
>use strict; use warnings;
>use base 'WebCams_Server';
>__PACKAGE__->table (__PACKAGE__);
>__PACKAGE__->columns(All => qw/ id seq_id exp_run value timestamp /);
>1;}
>#=============================================================================
>(File try_factory.pl)
>use strict; use warnings;
>use lib "c:/development/perl/lib/";
>use WebCamsDb;
>use Class::DBI::Factory;
> 
>my $WebCams = Class::DBI::Factory->new;
>$WebCams->set_db ({db_type => 'mysql',
>                   db_name => 'WebCams',
>                   db_username => $name,
>                   db_password => $pw,
>                  });
>$WebCams->use_classes (qw/Assay Results Experiment/);
>my $table = 'Assay';
>my @columns = $WebCams->columns (lc($table));
>#=============================================================================
>(File Assay.pm)
>package Assay;
>use strict; use warnings;
>use lib "c:/development/perl/lib/";
>use base 'WebCams_Server';
>__PACKAGE__->table (__PACKAGE__);
>__PACKAGE__->columns(All => qw/ name description executable comments timestamp /);
>1;


Re: Using class::dbi::factory with multiple table classes in a single file
Thomas L Shinnick 18:41 on 05 Apr 2005

Generated at 14:42 on 11 Apr 2005 by mariachi v0.52