$ perl -wE' use DBI; my $dbh = DBI->connect(...); $dbh->do("set wait_timeout=5"); my $sth = $dbh->prepare_cached("SELECT COUNT(*) FROM ..."); $sth->execute or die $dbh->errstr; say $sth->fetchrow_array; sleep 6; $sth->execute or die $dbh->errstr; say $sth->fetchrow_array; ' 0 DBD::mysql::st execute failed: MySQL server has gone away at -e line 9. MySQL server has gone away at -e line 9.