The problem space: I'm creating a web-logging system that records hits to one Apache server to an Oracle database.

There are a number of virtual hosts (47 at last count) and I want to log hits to specific locations on each virtual host.

So it seems I have this kind of hierarchy: Vhost -> location.

Now I've got one script that's going to yank the hits out of the logfile and insert them into the DB. The Vhost and Location objects are going to need access to the DB, but I don't want to create a new db handle for each instance of a location (there are going to be hundreds in all).

Each hit is logged as being to a specific location (file or directory), so each unique location needs a unique identifier. however, locations of the same name may occur on different virtual host, so each location needs to be tied to a particular virtual host. For reporting purposes, all this stuff needs labels (virtual hosts have names, the locations have labels).

so here's one way of writing the script that uses these objects:

use strict; use DBI; my $dbh = DBI->connect(blah blah blah) or die "blah"; use Vhost; use Vhost::Location; my @vhosts; # the constructor takes the ID (the primary key) # of the virtual host, queries the DB # and returns all the # properties of the host (name, etc.) foreach (#list of identifiers from DB) { push (@vhosts, new Vhost($_); } my @locations; foreach my $vhost(@vhosts) { my @location_ids = $vhost->get_location_ids(); foreach @location_ids) { # here, as above, the constructor takes the primary key # of the object in the DB, and returns the other properties push @locations, new Vhost::Location($_); } }
What I'm left with are two arrays: one of location objects, and one of virtual host objects. But clearly, both the Vhost module and the Location module need to access the db. I want to use the same handle all the way through.

Thanks for any input!

Philosophy can be made out of anything -- or less


In reply to Sharing a database handle among objects by arturo

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.