On 2006-04-13 09:59:02 -0400, Linda Ding wrote:
> We have a requirement here that we can't hard code any oracle database 
> instance 
> name for security reasons.
[...]
> my $dbh = DBI->connect( 'dbi:Oracle:orcl',
>                       'jeffrey',
>                       'jeffspassword',
>                     );

I'd be a lot more worried about the password than the instance name in
this case.

> Is there any way we can pass the connection string 
> (for example, the oracle tnsname alias), or put the instance name in a 
> configuration file

This question sounds like "can I read files in perl?" and I'm sorely
tempted to answer "yes, of course" and leave it at that. 

But I'm assuming that you want to know how other people solve this (very
common and also rather trivial) problem, so I'm posting the code I use
in DBI programs:

sub init {
    my ($self, %opts) = @_;

    my $cred_file = $opts{credential_file} || $ENV{DBI_CREDENTIAL_FILE} || 
"default";
    if ($cred_file !~ m{/}) {
        $cred_file = "$ENV{HOME}/.dbi/$cred_file";
    }

    $self->{dbh} = DBI->connect(_read_cred($cred_file),
                           { RaiseError => 1, AutoCommit => 0 });

    # ... more initialization stuff ...
}

# read credits from file
sub _read_cred {
    my ($fn) = @_;

    open(FN, "<$fn") or die "cannot open $fn: $!";
    my $line = <FN>; 
    close(FN);
    my @cred = split(/[\s\n]/, $line); 
    return @cred;
}

This is taken from a module which manages mail messages in a database.
init is an object method which connects to the database and does some
other initialization stuff. 

It can be called like 

    $store->init(credential_file => 
'/var/lib/www/offline/webmail/dbi/connect_data');

in which case it will read that file. If no file is specified it will
use the environment variable DBI_CREDENTIAL_FILE instead or fall back to
"default" if that isn't set either. If the filename doesn't start with a
slash it is relative to ~/.dbi. So I can configure various databases
in my ~/.dbi directory and run my scripts against each of them by just
changing the DBI_CREDENTIAL_FILE env variable.

        hp

-- 
   _  | Peter J. Holzer    | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR       | I'd be programming in Java.
| |   | [EMAIL PROTECTED]      | I don't, and I'm not.
__/   | http://www.hjp.at/ |   -- Jesse Erlbaum on dbi-users

Attachment: pgpTw9aZY499q.pgp
Description: PGP signature

Reply via email to