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

Hi, I need to run a report on several Oracle DB's why when I run the script -w complains that $username & $passwd are only used once? Why doesn't it get it right in my connect string? The actual passwd consists of characters such as <>? and other's. Tnx.
@sites = ("sid1", "sid2"); @ips = ("10.10.10.10"); $icnt = 0; $username = "user"; $passwd = "passwd"; foreach $site (@sites) { ################ print "Attempting to connect...\n"; $dbh = DBI->connect('dbi:Oracle:', q{$username/$passwd@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST= $ips[$icnt])(PORT=1521)) (CONNECT_DATA=(SID=$site)))}, "") || die "Can't connect vi SQLNET to +Database: $! $site"; print "Connection successful\n"; $dbh->disconnect();

Replies are listed 'Best First'.
Re: DBD Oracle doesn't login?
by valdez (Monsignor) on Oct 24, 2002 at 14:30 UTC

    Variables inside q{...} will not be expanded as you would expect; this is also the reason of the warnings. You should use qq{...} instead. And don't forget to put a back slash before @, or you'll get an error.

    Ciao, Valerio

Re: DBD Oracle doesn't login?
by robartes (Priest) on Oct 24, 2002 at 14:34 UTC
    Perl is absolutely right in claiming that $username and $passwd are only used once. In your connect string, you use the q{} operator, which does not interpolate variables, so you are passing $username as username and $password as password, instead of the contents of those variables. Use qq{} instead:
    $dbh = DBI->connect('dbi:Oracle:', qq{$username/$passwd@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST= $ips[$icnt])(PORT=1521)) (CONNECT_DATA=(SID=$site)))}, "") || die "Can't connect vi SQLNET to +Database: $! $site";
    This way, you're even connecting to the correct host as well :).

    CU
    Robartes-

Re: DBD Oracle doesn't login?
by Jasper (Chaplain) on Oct 24, 2002 at 14:32 UTC
    because you are using q{}. q{} behaves like single quotes, so those $password and $username in there aren't the variables but the strings \$password and \$username.

    Try qq{}, and see if that makes a difference.

    Jasper