Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

DBI and Constant

by skeight (Novice)
on Feb 12, 2001 at 14:35 UTC ( #57868=perlquestion: print w/replies, xml ) Need Help??

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

Hello Everyone,

I would like to embed a constant into a DBI connect string to allow easy switching from a test database to a live database and I'm not quite sure how to do so.
Here's my current constant string and DBI connect string
use constant DBNAME => "database_test"; #database_test | database ... my $dbh = DBI->connect('DBI:mysql:database=database','username','passw +ord') or die(); ...
Anyone know how I can get that constant into that string??

Leonard Leblanc

Replies are listed 'Best First'.
Re: DBI and Constant
by MeowChow (Vicar) on Feb 12, 2001 at 15:56 UTC
    You probably don't need a constant constant for this; just use a variable. This will simplify your connection string, since you can then use Perl's built-in variable interpolation instead of the clunkier concatenation with a constant value:
    our $DBNAME = 'database_test'; my $dbh = DBI->connect("DBI:mysql:database=$DBNAME", 'username', 'pass +word') or die;
    Note the single-quotes have changed to double-quotes to allow for the interpolation. From one of your previous posts, it looks like you haven't learned about variable interpolation (excuse my presumptuousness if I'm mistaken :). Interpolation allows you to just type your variable names into double-quoted strings, and have Perl automatically replace the value with the variable's value. You can get a detailed (if not gentle) explanation of this feature in perlop.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: DBI and Constant
by bbfu (Curate) on Feb 12, 2001 at 14:43 UTC

    Hrm. I think you'll have to use string concatenation:

    use constant DBNAME => "database_test"; ... my $dbh = DBI->connect('DBI:mysql:database=' . DBNAME, 'username', 'pa +ssword') or die "$0: can't connect to DB: " . DBI->errstr . "\n";

    The optimizer is probably smart enough to do the concatenation at compile time since it's smart enough to do the function inlining...

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re: DBI and Constant
by TheoPetersen (Priest) on Feb 12, 2001 at 19:49 UTC
    If you really want to do this as a constant, you can use the usual interpolation trick for non-scalars:
    my $dbh = DBI->connect("DBI:mysql:database=@{[DBNAME]}",'username','pa +ssword') or die();
    But I agree that a global is just as obvious to both the reader and the compiler.
Re: DBI and Constant
by lachoy (Parson) on Feb 12, 2001 at 23:17 UTC

    For most databases, you can also do:

    my $dbh = DBI->connect( 'DBI:mysql:mysql', 'username', 'password' ) || die $DBI::errstr; $dbh->do( 'use ' . DBNAME );

    Another option is putting the entire DBI DSN in the constant:

    use constant DSN => 'DBI:mysql:database=devel'; ... my $dbh = DBI->connect( DSN, 'username', 'password' ) || die $DBI::errstr;

    Chris

    M-x auto-bs-mode

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://57868]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-06-29 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (96 votes). Check out past polls.

    Notices?