"How do I get that 'dbq' method into that _dbh object? So the client code could do: $dbh = $foo->get_dbh; $dbh->dbq( "blah blah whatever" );"

I can feel that you are a little bit confused. The first thing you need to do before you get into the coding, let's get your design clear.

So you want a class that wraps the basic database operations, which is fine. We got the mission statement, now let's look at the details of the class.

First we want a property holds the handler (or the connection), so that we can reuse that connection when we need it, and you got that - $self->{'_dbh'}, nice and clean.

Now you want three methods, 1) to initialize or set up the connection; 2) a getter to return the handler; and 3) a method to execute SQL queriES. Now the third one started to introduce soem sort of confusion to yourself. The way you used the mthod shows that.

Here is the confusion: on one hand, the dbq method knows the handler as it is part of this class we are working on, and uses the handler; On the other hand, when you call it, you used it in a way as if it is a method belong to a different class - a second class that wraps the handler.

The correct way of using your methods is (assume that your class is called DB):

my $db = DB->new(); $db->_setup_dbh($dsn, $user, $passwd); $db->dbq("whateverquery");

Now you probably want to consider other things like: 1) let the new() method call _setup_dbh; 2) let the dbq method return the query result, not the statement. Otherwise your class doesn't fully wrap the database operations, as the classes that use your class will need to know how to get result from a prepared statement. 3) the getter is not really needed. If all DB operations are wrapped inside, why does the outside world need the knowledge of this handler?


In reply to Re: object method question by pg
in thread object method question by Zarathustra

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.