I created a central script (jdb.cgi for lack of a snazzy name ;)) that simply uses the caller() function and based on the filepath (of the script that requires this), sets up the DSN, connects and declares a global variable ($dbh) that the script requiring this file will use for db stuff.

So then I create a script like so:

$database = "foo"; require '/home/share/jdb.cgi'; # now I can do whatever with $dbh..

This may not seem like the best way to do it (and may not be ;)), but I have two web servers & two different directories and I don't want to work on CGI scripts in one, test and get it ready and then move it over to production and have to remember to switch something (such as if I had a subroutine to change a parameter).

I hope this makes sense so far. Ok, so the problem is whenever my script on the development server updates the database, it's updating the production database server (not the DSN it was given, which would be port 3308, a development db server that has a different datadir, etc). I debugged & found that the DSN for the development server was being provided correctly for a script that was within the "site-dev" path ... boggles me as to what could be wrong.

Thanks in advance for any help! :)

Jason

Here's the script:

use DBI; my ($package, $filename) = caller; #my ($dsn); # if it's the dev site, return the Dev DSN, otherwise, return # the production DSN. if ($filename =~ /sites-dev/) { # it's the dev site $dsn = "DBI:mysql:database=$database;port=3308"; } else { # it's the production site $dsn = "DBI:mysql:database=$database;port=3306"; } $dbh = DBI->connect($dsn, "perlscript", "passboy"); 1;

In reply to Centralized DSN Switch Script (MySQL) by Purdy

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.