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

use strict;
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.).

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.