Your problems are many.
inside the foreach loop means your variables vanish right after you declare them. Not what you intended. Second, you are violating
(${$key} is an unstrict ref). Third there are many better ways to do this, but I'm left wondering why you would want to. Why not just use $query->param("whatever") when you need it? Why duplicate it like that? To make your code more legible? Well then that loop isn't going to help. If you really want to get a copy of the params while shaking the $query then use a hash: