davemabe has asked for the wisdom of the Perl Monks concerning the following question:

As I've mentioned before, I am working on a mod_perl application. I would like to have a development version of my application running on a different port to test things. However, because memory is still expensive relative to my budget, I want to run both sites (production and development) in the same server. However the global $dbh variable needs to somehow be partitioned, as it would represent a handle to the production DB on the main site, and a handle to the development DB on the virtual site. Is there a quick fix for this, or do I have to <vi_command>:%s/$dbh/$dbhdev/g</vi_command> my development perl module in vi and then reverse when I move some code up to production?

Replies are listed 'Best First'.
Re: mod_perl and Global Variables
by KM (Priest) on Nov 20, 2000 at 19:51 UTC
    If your dev site is a seperate vserver, you can set environment variables to seperate these. In you section of the conf file for your production server, use something like:

    SetEnv DB prod_db

    And, in the section for the dev environment you can:

    SetEnv DB dev_db

    Now, in your script, you can grab the DB environment variable to know which DB to connect to.

    Why is $dbh global, BTW. Using Apache::DBI?

    Cheers,
    KM

Re: mod_perl and Global Variables
by merlyn (Sage) on Nov 20, 2000 at 19:51 UTC
    I've never had a $dbh be a "global variable" in my mod_perl handlers. Always lexical. How are you writing your code such that it needs a global $dbh?

    -- Randal L. Schwartz, Perl hacker

Re: mod_perl and Global Variables
by cadfael (Friar) on Nov 20, 2000 at 20:56 UTC
    I'm not sure exactly what you are asking for here, but when I write scripts using DBI and DBD, I always put the parameters into variables (i.e. user name, password, db, server) so the script can be easily modified.
    if(! $U){ $U = "foo"; } if(! $S){ $S = $ENV{"DSQUERY"};} if(! $S){ $S = "sybase"; } if(! $P){ $P = "foo_pw"; } if(! $DB){ $DB = "info"; }

    The above is specific to Sybase, which uses an interface file and the DSQUERY environment variable to identify which server to query. The necessary variables default to a particular user name, password, server, and database, and will permit the script to run on a command line as well as a cgi script. If The script needs to query a different database, then all the user needs to do is change the default variables near the beginning of the script.

    -----
    "Computeri non cogitant, ergo non sunt"

Re: mod_perl and Global Variables
by steveAZ98 (Monk) on Nov 21, 2000 at 02:26 UTC
    What happens if you use a global hash. Would Apache::DBI cache the hash and allow multiple persisitant connections between different servers since the cached handle is bassed on the connect string.
    i.e.
    use strict; use DBI; use vars qw( %conn ); sub connect { reread_conf("./config"); my $server = $config->{server}; my $db = $config->{db}; $conn{$db} ||= DBI->connect("dbi:$server:$db", ...); } my $dbh = connect();
    Where reread_conf has the config hash for that app. Would this work? I haven't tested it myself and would be interested to know.