*Question 1-> There has to be a more efficient way to 
*store the queries because I am defining the queries 
*each time the method is called... but where should 
*they be stored? 

Create an .PM file called Queries.pm (or some such name as that.) In that file you start off with:

package Queries; use DBI; use strict; #we really don't care about this as we are not #going to instantiate a Queries object. Just its #children sub new { my $proto=shift; my $class=ref($proto) || $proto; my $self={}; bless $self,$class; # We'll just put this here for clarity $self -> init(); return $self; } sub init { my $self=shift; #modify this to suit your situation my $self->{dbh}=DBI->connect('DBI:mysql',"username" ,"password" ); } sub execute { my $self=shift; my @parms=@_; return unless $self->{dbh}; return unless $self->{sql}; $self->{sth}=$self->{dbh}->prepare($self->{sql}); } sub fetchrow_hashref { my $self=shift; return unless $self->{sth}; return $self->{sth}->fetchrow_hashref; }
8<snip!8<

Now that you have the base class you can add more classes to the same .pm file as follows:

package loadusertable; use Queries; use strict; use vars qw( @ISA ); @ISA=qw(Queries); sub new { my $proto=shift; my $class=ref($proto) || $proto; my $self={}; bless $self,$class; $self->init; # we get that from the base class $self->{sql}="select * from users where id=?"; return $self; }

Now we have a class that when we instatiate it it connects to the database and can be used as follows:

use Queries; use strict; my $q=new loadusertable; $q->execute(15); my $row=$q->fetchrow_hashref;

There is more that can be done with this. This was just a quick "something" I threw together to answer your question.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Peter L. Berghold --- Peter@Berghold.Net "Those who fail to learn from history are condemned to repeat it."

In reply to Re: Object Oriented Pattern help by blue_cowdawg
in thread Object Oriented Pattern help by thefid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.