You might be better off looking at $r->servername in a PerlPostReadRequest handler or something similar, and then setting the db connect value in $r->pnotes so that you don't pollute ENV across all requests.
Even better, run a separate apache server for qa. If you get a bug in there, you run the risk of connecting your qa site to the production database, which can have undesirable consequences. This approach also has the added bonus that you will be able to use Apache::DBI for persistent DB connections, whereas I don't think you can do that with the example you have shown.