Re: The difference between my and localby davorg (Chancellor)
|on Jul 05, 2001 at 14:05 UTC||Need Help??|
In my experience you can only really explain the difference between my and local once you've explained the difference between package and lexical variables.
A package variable is associated with a package (hence the name!) It lives in the package's symbol table and can be accessed from anywhere within the package. A package variable can also be qualified with its package name (e.g. $my_package::variable) and, in this form, can be accessed from anywhere in the program.
A lexical variable is associated with a block of code. It lives in a pad and can only be accessed from within the block.
local only works with package variables. It stores the existing value of the variable and gives it a new, undef, value. The previous value of the variable is restored when the innermost enclosing block of code is exited.
my creates a new lexical variable. The variable only exists until the innermost enclosing block of code is exited.--
Perl Training in the UK <http://www.iterative-software.com>