Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Welcome to the Monastery
 
PerlMonks  

Re: The difference between my and local

by davorg (Chancellor)
on Jul 05, 2001 at 14:05 UTC ( #94051=note: print w/ replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://94051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-04-16 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (422 votes), past polls