Note that your problem has nothing to do with "variable scoping". There is only one package variable named $database. Neither the variable nor its name ever go out of scope before the end of the program. The value of that variable changes when the required code is executed as other monks have explained.