my
restricts the scope of a variable. The scope of a variable is where it can be seen. Reducing a variable's scope to where the variable is needed is a fundamental aspect of good programming. It makes the code more readable and less error-prone, and results in a slew of derived benefits.
If you don't declare a variable using my
, a global variable will be created instead. This is to be avoided. Using use strict;
tells Perl you want to be prevented from implicitly creating global variables, which is why you should always use use strict;
(and use warnings;
) in your programs.
Related reading: Why use use strict;
and use warnings;
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…