|We don't bite newbies here... much|
strict.pmby tye (Cardinal)
|on Sep 13, 2000 at 22:46 UTC||Need Help??|
Item Description: Adds strictness that can make Perl code easier to maintain
Review Synopsis: Put "use strict;" near the top of any Perl code that you plan to keep
There are (currently) three options for "strictness". Though you should usually use all three by placing:
near the top of each of your Perl files. This will help to make your source code easier to maintain.
use strict "vars"
If you use strict "vars", then you will get an error if you ever use a variable without "declaring" it. You can "declare" a variable via:
Detection of undeclared variables happens at compile time.
use strict "subs"
If you use strict "subs", then you will get an error for most uses of "barewords". Without this strictness, Perl will often interpret an unadorned identifier as a string:
It is good to use this strictness because forgetting to "adorn" an identifier is a common mistake. Perhaps you meant to write:
There are lots of ways to adorn an identifier so that it won't be a bareword:
And there are several ways you are expected to use barewords that will not be complained about even if you use strict "subs":
Detection of barewords happens at compile time. This is particularly nice because you can make a policy of making sure many of your subroutines are declared before you use them (especially in the case of constants which are usually imported from modules) and them call them as barewords (no parens and no &) and then Perl will detect typos in these names at compile time for you.
use strict "refs"
If you use strict "refs", then you will get an error if you try to access a variable by "computing" its name. This is called a symbolic reference. For example:
Symbolic references were often used in Perl4. Perl5 has real references (often created with the backslash operator, \) that should be used instead (or perhaps you should be using a hash and computing key names instead of computing variable names). Perl5 also has lexical variables (see the my operator) that can't be accessed via symbolic references. Catching symbolic reference is good because a common mistake is having a variable that should contain a real reference but doesn't. Dereferencing such a variable might silently fetch you garbage without this strictness.
Detection of using symbolic references happens at run time.
If you have one of those truely rare cases where you need to do symbolic references in Perl5, then you can delcare no strict "refs" for just that chunk of code or use eval.
Updated to cover a few more cases.- tye (but my friends call me "Tye")