in reply to DBI query where table name is a variable
You need to set $table before you call $dbh->prepare.
At that point, the table name gets expanded into the SQL string, and it will not change, even if you re-assign to $table.
In Section
Seekers of Perl Wisdom