title: Subclassing the Perl DBI, overwriting the database handler in the calling script upon disconnection
body: <p>I have this problem:</p>
  
  <p>pseudocode:</p>
  
  <pre><code>package a;
  
  @ISA = qw(DBI);
  
  package a::db;
  @ISA = qw(DBI::db);
      sub prepare{
        #gets a $dbh object, which we can overwrite upon disconnection
        #so that the calling script gets a fresh and working dbh, 
        #through $_[0] = $newdbh;
      }
  
  package a::st;
  @ISA = qw(DBI::st);
  
  sub execute{
    #gets a $sth object which we can rescue upon disconnection 
    #before the execute, but the calling script will have a faulty $dbh
  }
  </code></pre>
  
  <p>calling script:</p>
  
  <pre><code>use a;
  my $dbh = a-&gt;connect;
  my $sth = $dbh-&gt;prepare("select 1");
  $dbh-&gt;disconnect; #I detect this in the execute function
  $sth-&gt;execute; #disconnect detected, reconnect -&gt; reprepare and execute again 
                 #(is done in the module a) = we're up and running!
  </code></pre>
  
  <p>Is there any way to affect the $dbh object without having to call $dbh->prepare again in the calling script?</p>
  
  <p>My problem is also that I want to do this seamlessly because I want to fix the database handling in many projects by just using my new database module.</p>
  
  <p>Is it at all possible?</p>
  
  <p>Or could I maybe make the statement handler from a::prepare part of the ::db package as well? Which in turn would mean that the database handler from a::connect would have to be a statement handler as well, so that when I use connect I get an object with access to both prepare and execute. Then I could modify $_[0] in the a::execute to the new dbh object, which would mean that after the reconnect in the a::execute, the calling script would have a valid $dbh. Tried a lot of ways of doing this but I suspect the inner magic of DBI is making it hard...</p>