The variable $database is defined via my inside an if statement. Why can code outside of that block refer to it?
That looks very wrong, and (assuming there isn't a stray global named $database) I'm not able to replicate the behavior using 5.6.1. What version of Perl are you using?