This time the problem was the following line:
th = $dbh2->prepare("select c.name,c.extractexpression,c.valuetype from outputs as c where c.commandid='$cmdid'");
Note that there is a problem with the 'variable' at the front. Should have been:
$sth = $dbh2->prepare("select c.name,c.extractexpression,c.valuetype from outputs as c where c.commandid='$cmdid'");
So normal typo problem. Still, why no message about bareword?