Hi all,

I'm quite used to declaring variables as follows:

my ($title, $page, $chapter);
That is, without initialising them with default values.

I also find it quite common in other people's code that I've come across.

I'm wondering, after reading about variables being set to old values under mod_perl, if it might be better in general to initialise each and every variable. So, instead of the above, we now have:

my $title = ''; my $page = 0; my $chapter = 0;
It's more work definitely but it generally makes the code more readable (for instance, if I've no idea what 'page' means, I at least know the variable $page is going to be used for some numerical value). There're probably also some gains in performance as the interpreter doesn't have to make guesses about those values.

Is it merely a preference thing? Or is one method better than the other?

I look forward to reading your views. Many thanks in advance :)

update: It's been very enlightening reading your comments on the node. It seems not initialising the variables is the normal thing to do in most circumstances. Thanks once again for your sharing :)


In reply to To initialise or not to initialise? by kiat

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.