[Koha-devel] Repetitious behavior repeatedly

paul POULAIN paul.poulain at free.fr
Tue Oct 1 04:32:01 CEST 2002


Nathan Gray wrote:

>I believe what you are considering is object-oriented programming,
>which can make a lot of sense, if you build your objects correctly.
>You decide which information is allowed to be stored in the object,
>and which methods (functions) will be handled internally by the
>object.  Then you just pass around one variable, usually called
>'$self'.  When you want a dbh, you could run:
>
>  my $dbh = $self->dbh;
>
That's it ! Great idea. Thus, there will be only a few things to change 
in koha.
Every script starts with
my $param_object = buildparam->new();

Every C4 sub is called with
$result = C4Function($param_object,$other_param1, $other_param2,...);

And every C4 sub needs only to have 2 lines rewritten :
sub MySub();
my ($param,$other1, $other2) = @_;
my $dbh = $param->dbh;

instead of
sub MySub();
my ($dbh,$other1, $other2) = @_;

If in a C4 sub we need an other parameter, we can use :
my $HashOfStopWord = $param->stopwordlist(); and work with HashOfStopWord.

>The 'dbh' method would know how to get a new database handle, or would
>return the existing one, if it has already made a connection.
>
Very nice idea too. With this one, we fill, for example stopword list 
only when ->stopwordlist() is called : no unuseful DB readings.
Note that the DB handle is so necessary that i think this one should 
always be created when object is created.

>You could very easily create a 'prepare' method in self, that would in
>turn run 'prepare' on the dbh.
>  sub prepare {
>      my $self = shift;
>      my $query = shift || return;
>      my $dbh = $self->dbh || return;
>      my $sth = $dbh->prepare($query);
>      unless ($sth) {
>          warn $dbh->errstr;
>      }
>      return $sth;
>  }
>Each method would handle its own error handling, when appropriate.
>Adding methods for 'param' and 'stopwords' would also be really easy.
>Let me know if you need any help, if you decide to go this route.  I
>now write most of my code in an object-oriented style, because I find
>it much easier to keep track of where everything is.  Then you can
>also inherit methods from other objects, and all sorts of fun stuff
>(perhaps down the road a ways).
>
I'm very poor at Perl-OOP, so any help is greatly welcomed ;-)
Give an object with db handle and stopwords management, i'll copy/paste 
for other tables if you don't do. Don't you have a cvs account on 
sourceforge ?

>kolibrie
>
I think we've got a winner at Perl Programming...
(twice winning in 1 week. Kudos Nathan ;-) )
--
Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/koha-devel/attachments/20021001/d06ef5a4/attachment-0002.htm>


More information about the Koha-devel mailing list