in reply to DBI: pass $dbh from a sub

The warn on the DBI->connect call is useless because you are telling DBI to RaiseError.
Wrap that call with an eval to trap errors.

Replies are listed 'Best First'.
Re^2: DBI: pass $dbh from a sub
by iburrell (Chaplain) on Jul 29, 2004 at 16:27 UTC
    The DBI->connect method does not follow the RaiseError property. It returns undef on an error and sets the $DBI::errstr. The return value needs to be tested, although it is more useful to use die instead of warn.
      The DBI->connect method does not follow the RaiseError property. It returns undef on an error...
      It used to be that way, but hasn't been for quite a while. Check the changes log.
        yes, this was fixed some time ago, the Changes file in the DBI distribution says...
         Changes in DBI 0.91,	10th December 1997
          ...
          DBI->connect(..., { RaiseError=>1 }) now croaks if connect fails.
          ...
        
      It doesn't behave that way for me.
      The code...
      use DBI; eval { $dbh = DBI->connect('dbi:Oracle:dev', 'bad', 'pass', {RaiseError => + 1, PrintError => 0}) || warn "warning"; print "after connect\n"; }; if ($@) { print "Ugh!\n"; }
      will not print out "warning" or "after connect"
      it prints out "Ugh!"
      # perl testit Ugh!
      But I avoided "die" because I don't want my program to end at that point; doesn't "die" exit the program?
        ...doesn't "die" exit the program?
        Not when it is in an eval block. eval is perl's way of "trying" a block of code, and die is like throwing an exception, and checking $@ afterward is like catching the exception.