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

I have troube using Class::DBI and Class::MethodMaker together.
the following code produdes the error as shown below. any ideas?
package Foo::DBI; use base 'Class::DBI'; use Class::MethodMaker new_with_init => 'new'; sub init { my $self->set_db('Main', 'dbi:mysql:database', 'user', 'pass +word'); }
Error-Output:
[Wed Sep 24 12:39:38 2003] [error] [client 10.0.1.48] Premature end of + script headers: test.cgi [Wed Sep 24 12:39:38 2003] [error] [client 10.0.1.48] Can't connect(), + no database driver specified and DBI_DSN env var not set at blib/lib +/Class/MethodMaker.pm (autosplit into blib/lib/auto/Class/MethodMaker +/new_with_init.al) line 259

Replies are listed 'Best First'.
Re: problem using Class::DBI with Class::Methodmaker
by perrin (Chancellor) on Sep 24, 2003 at 12:57 UTC
    What are you trying to do? Class::DBI already provides a constructor (several in fact) for your class, and you have to to use it if you want Class::DBI to work.
Re: problem using Class::DBI with Class::Methodmaker
by Anonymous Monk on Sep 24, 2003 at 10:49 UTC
    ok, i got some ideas and thoughts about this on #perl, actually even after changing the code to
    package Foo::DBI; @Foo::DBI::ISA = qw( Class::DBI ); use Class::MethodMaker new_with_init => 'new'; sub init { my $self=shift; $self->set_db('Main', 'dbi:mysql:my_database', 'user', 'xxxx +'); }
    fxn (#perl) pointed me to the following problems:
    fxn	you would need to debug whether the underlying implementation is compatible
    fxn	this is a problem inherent of the openess of OOP in Perl
    fxn	Class::MethodMaker uses a hashref IIRC
    fxn	whereas I would expect Class::DBI to be a tied hash behind the scenes (just a guess)
    
    any ideas or suggestions for that? thanks
Re: problem using Class::DBI with Class::Methodmaker
by simonm (Vicar) on Sep 24, 2003 at 17:08 UTC
    I don't think Class::MethodMaker is really the problem.

    That use Class::MethodMaker new_with_init   => 'new'; line is equivalent to adding the following subroutine declaration to your package:

    sub new { my $class = shift; $class = ref $class || $class; my $self = {}; bless $self, $class; $self->init(@_); $self; };

    Perhaps the problem is that Class::DBI provides its own constructor method which it expects you to call?

    You might be able to do something more like the following:

    package Foo::DBI; use base 'Class::DBI'; sub new { my $self = (shift)->SUPER::new( @_ ); $self->init(); return $self; } sub init { my $self->set_db('Main', 'dbi:mysql:database', 'user', 'pass +word'); }
Re: problem using Class::DBI with Class::Methodmaker
by zengargoyle (Deacon) on Sep 25, 2003 at 01:34 UTC

    it's something to do with namespaces. try this which works.

    package Foo::DBI; use base 'Class::DBI'; use Class::MethodMaker new_with_init => 'new'; sub init { my $self = shift; __PACKAGE__->set_db('Main', 'dbi:Pg:dbname=kickcache;host=somewhere. +sub.dom'); __PACKAGE__->table('kickcache'); __PACKAGE__->columns( All => qw( session username ) ); } sub test { my $x = Foo::DBI->new; my $y = $x->retrieve(0); print $y->username, $/; } 1; __END__ $ perl -MFoo::DBI -e 'Foo::DBI->test()' 1064452728 # which happens to be the right answer...
Re: problem using Class::DBI with Class::Methodmaker
by parasew (Beadle) on Sep 24, 2003 at 22:48 UTC
    maybe you want to take a look also at Class::Accessor, which is a Module used already in Class::DBI. therefore they both should work much more nice together...
      but Class::Accessor doesn't seem to have a that huge functionalty, such as Class::MethodMaker has... any comments on that?