in reply to scoping problems
If you have syntax errors and errors about undeclared variables, fix the syntax errors first. Sometimes the errors about undeclared variables are caused by mis-parses which again come from the syntax errors, so they go away once you fix the syntax errors. (Sometimes, not always).
In Section
Seekers of Perl Wisdom