in reply to check if DBI is still connected

You want the ping method of the connection handle,

if ( not $dbh->ping ) { $dbh = my_reconnect() }
If you would like to make this automatic, with no connection made until the handle is used, and no other alteration to existing code, see my Lazy DBI Connection Handles.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: check if DBI is still connected
by jamroll (Beadle) on Jun 23, 2017 at 18:17 UTC
    yes...yes...but, what if i want to:
    my $dbh = "123456"; # just as a test, instead of DBI->connect(....); if ($dbh != valid_DBI_handle()) { print "please supply a PROPER reference to a DBI object, or whatever + it's called\n"; } else { # okay, dbh actually points to a real db connection print "dbh is truly valid"; }
    because most posts about this say to simply
    if (!$dbh) { die "no db connection!"; } else { $dbh->run_some_command(); }
    well, if $dbh eq 1, then we try to run_some_command will fail, and cause a bad user experience, to the point where the user's trust in the site dies along with the script!