I'm looking to implement a large project under CGI::Application. I'd like to keep most database manipulation out of the main WebApp module, and into other modules that represent more tangible objects.

I'm wondering how to pass the database handle around. I plan to run under mod_perl with Apache::DBI. Is it ok to pass handles back and forth this way? or should the objects i'm calling create and destroy the database handles themselves. Or should i be going about this some other way

Below is some code showing what i'm thinking of doing:

package MyWebApp; use base 'CGI::Application'; use MyWebApp::DB; use MyWebApp::TicketCollection; use strict; # Needed for our database connection sub setup { my $self = shift; my $db = new MyWebApp::DB; my $dbh = $db->get_dbh; $self->start_mode('menu'); $self->tmpl_path(''); $self->run_modes( tickets => 'show_tickets', ); # Connect to DBI database $self->param('dbh' => $dbh); } sub teardown { my $self = shift; # Disconnect when we're done $self->param('dbh')->disconnect(); } sub show_tickets{ my $self = shift; my $q = $self->query(); my ($start_date, $end_date) = ( $q->param('start_date'), $q->param('start_date') ); #more code to verify user input my $tc = new MyWebApp::TicketCollection( $self->param('dbh'), $start_date, $end_date ); #code to display tickets } 1;

In reply to Passing Database handles to other modules. by thunders

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.