I am learning Perl for school, and currently am learning about the use of the my
keyword and about scoping in Perl. (For reference, I was looking at How should I use the "my" keyword in Perl? .) Hopefully this question hasn't already been asked elsewhere, but if not...why is Perl's default behavior the way that it is?
It seems to me that a C-style default scoping makes the most sense...you declare a variable inside a block, the variable exists inside that block, and once you leave that block, that variable is no longer accessible. Why is it that in Perl, to specify this behavior, you must use the my
keyword? It seems that limiting a variable's scope to only where it is used would be good standard behavior, and using my
all the time seems to be very redundant and like it would contribute to cluttering of code.
Seems a bit like walking in to the grocery store and immediately loudly declaring your preferred brand of such-and-such before continuing with your shopping, just in case anyone around you was curious (which they probably weren't).
(Potential duplicate, this question might get taken down... Why declare Perl variable with "my" at file scope? .)
my
you can see intention of variable declaration vs. variable (re)assignment. This enables warnings of duplicate vars when declaring inside the same block (which is IMO a good thing). As a side note, javascript behaves in the same manner. – Suckerfish