in reply to where to declare a variable...

Scoping is probably the most important part of good code. It helps allow for all aspects of good coding including polymorphism, inheritance, and even eventual OO'ification. A simple rule of thumb is that of the curly brakets. a "my"ed variable is only "visible" to the code in the same set of outermost curly brackets. Given this example:
sub main { my $blah; if ($blah =~/(\d+)\s{2}/) { my $test = $1; } }
anything outside of the if {} will not see or be affected by the assignment of $test. but anything within sub main {} will be able to see $blah because it has been globally instantiated. Hope that helps.