![]() |
|
P is for Practical | |
PerlMonks |
Re:(3) Whether to use local()by shotgunefx (Parson) |
on Mar 15, 2002 at 19:01 UTC ( #152051=note: print w/replies, xml ) | Need Help?? |
You can use both globals (package vars) and lexicals under use strict though globals need to be fully qualified ( $My::Mail::Server= "255.255.255.255";) Though globals are usually (not always) the wrong way to go. The idea of lexicals are that they only exist in a scope thereby limiting access. What local does is let you temporarily save a value for a package var. It's a runtime trick and my is (mostly) a compile time trick. The lowdown is package vars exist in packages and any can be accessed from any package. my vars exist in the scope they where declared in and cannot be accessed outside so you don't have to worry about other subs/modules etc clobbering your values. You usually use local to save the value of $/ or $_ so you don't screw up code that calls your code. It has some other uses but that's one of the main one. There are probably a lot better explainations than mine and I'm sure if you do a search for lexical / package / global you will find a wealth of info here in the monestary. UPDATE here is a great FAQ on "Coping with Scoping" by dominus. It's a far better explaination than mine. -Lee "To be civilized is to deny one's nature."
In Section
Seekers of Perl Wisdom
|
|