On Friday, June 28, 2002, at 01:30 , Octavian Rasnita wrote: [..]
The canonical Answer is up at: http://perl.plover.com/FAQs/Namespaces.html for the bored try the home version silly alternative: http://www.wetware.com/drieux/CS/Proj/GlobalGame/ what one wants to do is protect against 'side effect' first and foremost. cf: http://www.wetware.com/drieux/pbl/bloopers/globalSideEffect.txt the following bit of code may help $test = "This is a Test"; PrintThis(); IncrThis(); PrintThis(); #--------------- # the subs sub PrintThis { print $test, "\n";} sub IncrThis { $test++; } If you have never had that fun - put it in a file and run it without the -w and use strict - then use strict - and notice that it will if you add the -w - it will give you no warning. if you add the 'use strict' - it will call out all three usages of $test - so the average person will do the cool thing and put the my or our before my $test = "This is a Test"; but danger will robinson - that still does not change the output. We still take a 'string' print it, and then force it to being a number and incrementing it. In a simple piece of code that is 'easy' to "spot" - the problem get's almost unmanagable in some 10,000 lines of code. > I am learning about scoping now and I don't understand something yet. > I've seen that it is better not to have variables that are global if they > are not necessary. A part of the reason that you want to avoid having any more 'global' variables than you absolutely need is to protect yourself in the 'long run' - as noted above. One of the reasons that I am a phan of perl modules is so that the 'core code' - the stuff in main::* is limited to only that part which needs to be dealt with in this specific piece of code. The other solution to keep in mind is that if you do 'old c style' and/or pascal style coding for your subs - sub MySub { my ($arg1, $arg2, $arg3) = @_; my ($retval, $that_bit, $the_other_bit, @array); .... } You 'localize' the values that you plan to play with inside the scope of that function - and know that they are NOT the one's that you got from 'above' somewhere in the scope of this file. Likewise you ONLY play with things that were cleanly passed into you. Do this and you isolate most of your problems to begin with. Also you are on the fast track to being able to rip out the functions that you suddenly notice are getting cut and pasted around the suite of applications you are writting. > Can you tell me why is it better to have only local variables if this is > possible? let's try to make sure we understand the difference between my $var; local $var; so that we do not throw this 'local variable' around too loosely and have it step on ourselves. sub doFoo { local $var = 123; ... } will actually fail unless in the same name space you have actually 'allocated' $var with either a my or our ( which came in with 5.6 ) > Does they require less memory, or only for an easier understanding of the > code? in the long run less memory. The most important chunk of memory is between your ears, and tracking wandering global side effect is a leading cause of a loss of memory and sanity... ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]