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

Hi All,
  I'm about to split up my database module into 3 or 4 smaller more specific modules. At the moment I'll calling all the routines as methods, such as:-
package database_mod; use DBI; our $dbh; sub connect { $dbh->### DBI connect }#sub sub add_user { $dbh->### DBI SQL, ETC }#sub sub report { $dbh->### ETC }#sub
Using it with:-
use database_mod; database_mod::connect(); database_mod::add_user(); ### etc

Now I'm thinking if I split it up and keep using this method things will get messy.
package database_mod; use DBI; our $dbh; sub connect { $dbh->### DBI connect }#sub package database_user; sub add_user { $database_mod::dbh->### DBI SQL, ETC }#sub package database_report; sub report { $database_mod::dbh->### ETC }#sub
Using it with:-
use database_mod; use database_user; database_mod::connect(); database_user::add_user(); ### etc

So I'm wondering if now is a good time to switch the database system to OO Perl? I've not really written much at all in the way of OO Perl. But I'm guessing it'd be something like:-
package database_mod; use DBI; sub new { ### $class, $self, bless n all. }#sub sub connect { $self->### DBI connect }#sub package database_user; use base database_mod; sub add_user { $self->### DBI SQL, ETC }#sub package database_report; use base database_mod; sub report { $database_mod::dbh->### ETC }#sub
Using it with:-
use database_mod; use database_user; my $db = new database_mod; $db->connect(); $db->add_user(); $db->report(); ### etc

Would this be the best way to go? Certainly looks a lot cleaner, but would it be OO for the sake of OO? Have I got the OO package logic right? Or should I stick to using methods like before?
I'm sure many of you have a lot more OO Perl experience than me.


Lyle

Replies are listed 'Best First'.
Re: Time to upgrade tp OO Perl?
by stvn (Monsignor) on Apr 28, 2008 at 23:46 UTC

    Take a look at DBIx::Class, it gets things right so you don't have too. Plus it will help you decouple your database access from your business logic more, which is always a good thing. If you don't want to go full on into the world of ORMs, then at least give Moose a look instead of using the out-of-the-box DIY OO Perl.

    -stvn

      Strong second. It's somewhat crazy to roll your own DB interface at this point in the game. It's a good learning exercise but you will never get it as right as DBIC or Rose::DB do working on your own the first, second, or third time out of the gate. And if you want to learn deeper stuff without all the hard knocks you can study that code and get on the dev lists.

Re: Time to upgrade tp OO Perl?
by starbolin (Hermit) on Apr 28, 2008 at 22:46 UTC

    cosmicperl writes:

    Now I'm thinking if I split it up and keep using this method things will get messy.
    This is anything but OO for the sake of OO. Also you would be moving from a global $dbh to a closure. Consensus is this is a good thing. Right now it would be difficult to test your code in pieces as $dbh needs to be created first and is global among all the subroutines. With OO each member function receives a reference to the object it is going to work on so testing individual objects becomes trivial.

    s/objects/member functions/


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}