I see, so a 'local' variable is destroyed when the program leaves a block, where 'my' is almost like C's 'static'? -- a global variable with local scope and namespace?
You have them backwards. "my" creates lexical variables, which are destroyed at the end of the block (unless references to them remain elsewhere). "local" gives local scope to a global variable.
Ok, so you can use 'local' on a special variable to make it reset when the code leaves the block. Very cool! Perl has some very interesting features!
When do you use 'local' with a user-defined variable? Is it simply a mechanism for a kind of namespace overloading?
You generally don't use 'local' with a user-defined variable. Probably the only place I could see it being useful would be to do the same thing that you would with a special variable...
If you have a global user-defined variable, you could local it in a smaller block of code so you don't lose the original value of it.
It's a habit that perl coders get into once they start using "strict". For short scripts you don't have to use "strict" or "my". But if you get into the habit of using them it really does make life easier. Especially if many people contribute code to a script (such as open source scripts).
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.