Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Making a Class::DBI object reference

by gryphon (Abbot)
on Apr 02, 2004 at 21:32 UTC ( #342147=perlquestion: print w/replies, xml ) Need Help??

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

Greetings fellow monks,

I've been working with Class::DBI a little while and enjoy it, for the most part. One thing that really annoys me is writing everything with absolute class names instead of objects. For example, instead of MyClass::DBI->some_method('data'), I would write someting like my $obj = new MyClass::DBI; $obj->some_method('data'). So far, my Googling hasn't returned any bits of sample code like this.

Attempting to code this on my own, I end up with:

package MyClass::DBI; use base 'Class::DBI::mysql'; sub new { my $pkg = shift; my $self; { my %hash; $self = bless(\%hash, $pkg); } $self->set_db(@_); return $self; } package MyClass::Item; use base 'MyClass::DBI'; MyClass::Item->set_up_table('item'); 1;

This doesn't work, but I don't really know why. I get an obscure Ima::DBI error. My little non-database brain can't figure this out. Is this possible to do? Anyone have some basic sample code they could share with me? Thanks.

code('Perl') || die;

Replies are listed 'Best First'.
Re: Making a Class::DBI object reference
by perrin (Chancellor) on Apr 02, 2004 at 21:41 UTC
    You can't just override new() like that. You are subclassing other classes that implement new() and excpect it to be called.

    You question makes it sound like you don't really understand the difference between object methods and class methods. A Class::DBI object represents a specific row of data. You wouldn't call methods on it that don't correspond to that single row. Maybe you should read a little more primer stuff about OO, like the tutorials that come with the perl man pages.

Re: Making a Class::DBI object reference
by iburrell (Chaplain) on Apr 03, 2004 at 00:57 UTC
    Is some_method a class method or instance method? Is it a constructor? Class::DBI objects already have a constructor for making objects. The constructor

    BTW, most class methods can be called on objects instead of with the class name if you already have an object to work with. The dispatch works and most class methods don't use the class name. The only place it doesn't work are with constructors where the class name is used with bless. Some constructors will use the "ref($proto) || $proto" idom to get the class name from an object.

    Also, Class::DBI uses a lot of class methods to setup the class. Most of them can be used on objects. But an object created before the class is configured will not work right.

Re: Making a Class::DBI object reference
by bilfurd (Hermit) on Apr 03, 2004 at 09:41 UTC
    Hello, gryphon --

    Since I do not know what the code is for, I have to ask if 'vanilla' DBI wouldn't be an easier route for a "non-database brain."

    If nothing else, you don't need the absolute class names. A newbie posted some basic DBI functionality at 180939 a couple years ago. If you have already used DBI, I won't mention the poster's name so I don't incriminate myself.

    Good luck!

Re: Making a Class::DBI object reference
by edoc (Chaplain) on Apr 03, 2004 at 13:22 UTC

    ok, how about this..

    When you want to create a new object, use 'create'.

    my $object = My::CDBI::Class->create({ id => 1, label => 'example' });

    When you already have a record in the database you want as an object, use 'retrieve'.

    my $object = My::CDBI::Class->retrieve(1);

    You could say that these two methods replace what you would normally think of as 'new's job, either taking the data from you or from the database to instantiate the object. You can now call methods on your object.

    $object->label('new example'); $object->update;



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://342147]
Approved by kvale
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2020-11-26 00:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found