BostonAreaHuman
BostonAreaHuman

Reputation: 1461

My Perl DBI->connect retuning wrong values when failing to connect

I have the folllowing perl code:

my $dbo_prd = DBI->connect(
    "dbi:Oracle:host=$db_srv_prd;port=1521;sid=$db_sid_prd",
    $db_user_prd,
    $db_pass_prd
) ||  warn &senderror("TREE_STRUCTURE.Could not connect to $db_srv_prd: $DBI::errstr\n");

print "\n\nconnection:" . $dbo_prd . "\n";
if ($dbo_prd != 1){
    print "in prod prepare\n\n";
    my $query1_prd = $dbo_prd->prepare(
        "INSERT INTO CMSV2.CMS_INBOX VALUES (
            'vmsdk', (SELECT SYSDATE from DUAL), 'NODE_TREE_UPDATE',?,?,?,?,NULL,NULL
        )"
    ) || warn &senderror("TREE_STRUCTURE.Could not prepare to $db_srv_prd: $DBI::errstr\n");
}

The issues that I'm having is that I thought if the connect didn't work it would set the connection $dbo_prd to undef but it is setting it to 1? This is not what the documentation states anywhere.

The connection is being printed out as "1" when it fails and a hash which it is supposed to if it succeeds.

Upvotes: 0

Views: 62

Answers (1)

ysth
ysth

Reputation: 98398

You have a precedence error here:

|| warn

That is assigning the return value of warn to your variable when the connect fails. Use this instead:

or warn

(warn, like print, returns 1 if successful.)

Always use the low precedence and/or for flow control between what are essentially different expressions; only use the high precedence &&/|| within what is essentially all one expression (e.g. my $foo = $bar eq 'a' || $bar eq 'b').

Upvotes: 7

Related Questions