http://qs1969.pair.com?node_id=94051


in reply to The difference between my and local

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.

--
<http://www.dave.org.uk>

Perl Training in the UK <http://www.iterative-software.com>

  • Comment on Re: The difference between my and local