Did you try looking at the contents of $dbh->errstr() after your $dbh->prepare(.. ) ?
And I'm not sure if you can change the value of $_ (using s///), maybe you should copy the contents to another variable first.
C.
Comment on Re: Script being caught up, somewhere. Causing infinite loop.