If your script starts out with something like:
#!/usr/bin/perl -w
then you will get a (often) helpful warning when you try to use an uninitialized value. And it is usually a very good idea to use that "-w", at least when developing your code.
So, yes, you should initialize your variables before you "read" values from them, just like in other languages.
I think what merlyn is referring to is that, unlike C, Perl will never give you a variable that contains "garbage" values ("random" data that was left on the stack) because you forgot to initialize it. But forgetting to initialize variables is still a common source of bugs (code that works the first time so passes simple tests but fails when used twice in one process, for example), hence the existance of the warning to help you prevent such bugs.
Since we're on the subject of "-w", let's also mention that you should almost always follow that "-w" line with the line
to make Perl much more likely to tell you if you make a typo (which also requires you to "declare" your variables via "my", "our", "use vars", etc.).use strict;
I'll also disagree with coreolyn on the suggestion to remove these precautions once a script is finished for the sake of speed. The speed gain is probably imperceptible. I will agree that removing "-w" can be a good idea since in the production environment you might run into warnings that didn't appear during development and these warning can cause problems in production. I particularly wouldn't remove the "-T" if you used it during development and the script has any security implications (such as a web CGI script). If there are things that "-T" will catch that you didn't find during development and testing, then you probably want those caught once the script is deployed. However, if there are absolutely no security implications with the script and it is very important that the script be resilient, then I'd remove the "-T" before deploying. The "use strict" probably won't find anything in production that it didn't find in development and testing, but I'd leave it in as the risk of forgetting to put it back each time you make changes is probably much greater than the risk that you'd notice the tiny load time.
[ So, should that be "a(n) (often) helpful" or "an (often) helpful" instead? ]
- tye (but my friends call me "Tye")In reply to Re: Variable Initialisation
by tye
in thread Variable Initialisation
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |