Hi Monks,

Read some posts here that were very similar to my goal, so came here seeking enlightenment. In short, am trying to move some DBI calls (prepare, execute) into a module, but the execution fails when calling "execute" (please see below). Would like to have both $dbh and $sth available to the CGI (e.g. outside of the module). The module snippet looks like this:

package My::DBU; use strict; use warnings; use diagnostics; use CGI::Carp; use DBI; sub new { my $class = shift; my $self = bless {}, $class; my $dbh = shift || return undef; $self->{dbh} = $dbh; return $self; } sub begin_work{ my $self = shift; $self->{dbh}->begin_work; } sub execute { my $self = shift; my $query = shift; my $sth = $self->{dbh}->prepare($query); my $rows = $sth->execute or die $DBI::errstr;; return \$sth; }

and the CGI snippet like this:

my $dbh = DBI->connect('DBI:mysql:db:localhost', ${dbuser},${dbpass}) or confess "$DBI::errstr<br>"; my $dbu = new My::DBU $dbh or confess "$DBI::errstr <br>"; $dbu->begin_work(); print "BEFORE ERR<br>"; my $sth = $dbu->execute( \$dbh, \$query, \$errmsg ); print "NEVER GETS HERE<br>";

with the error

You have an error in your SQL syntax; check the manual that correspond +s to your MySQL server version for the right syntax to use near 'REF( +0xaaa3700)' at line 1 at /usr/lib/perl5/site_perl/5.8.8/My/DBU.pm lin +e 96.\n

The query "select id from ${table}" is being used while attempting to resolve this issue. Links mentioned above are:

http://www.perlmonks.org/?node_id=324671 http://www.perlmonks.org/?node_id=870145

Many thanks for your time / suggestions.


In reply to passing objects as references ? by bbfan

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.