So this is probably a redundant question that has been answered before, and believe me I have tried answering it myself I have Modern perl and Perl programming by orileys and there is a lot of stress put on variable declaration I just can't wrap my foiled brain around the subject one line in the book that got my attention was on minimizing the use of global vars maybe if someone could answer this question the rest of this will come together for me? lol sorry but Im rather confused by it all.

Limiting the scope of a variable to the chunk of code where it is needed (a block, a subroutine, a package) helps avoiding bugs that are often insidious and difficult to track.

It also helps maintaining the code. Suppose you have in a long program global variables with very common names such as $line, $file, $count, $size, $sum, @list, etc. You must change the program and have to add a newsubroutine to do something new. If you don't declare locally your variables, you might unexpectedly use a variable name that is used somewhere else in the program and that is likely to break everything, as the $line variable will no longer contain the expected value. If you make your new variables local (or, more precisely lexically scoped) to the new subroutine, then you don't have this danger of side effects: locally in the subroutine, your local variable will contain what you expect, but once you are executing code outside of the subroutine, the variable will contain what the writed of the original code expected it to contain.

Of course, using the 'use strict;' and 'use warnings;' pragmas is part of the same strategy to prevent this type of insidious side effects.

Laurent provided excellent reasons for limiting the scope of perl variables. I am not sure that you understand what we mean by global and lexical variables.

The difference is primarily in the scope rules. When we do not use strict, all variables default to global. Their scope is the entire file (in fact, even other files if we prefix them with the package name). When we use strict, all variables must be declared. Variables declared with the keyword my are lexical variables which have the scope of the smallest enclosing block.

On extremely rare occasions, we need a global variable when strict is in effect. We can declare them with either use vars or our. Refer perldoc -f our for an explanation of the difference. Good Luck, Bill

The function do_something has the nasty feature of returning different values depending on how the program deals with the global variable $var and that can be a nasty feature for several reasons already mentioned in previous posts.