Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a stupidly basic question. In Perl if you declare a variable do you have to specifically intialize it? For example if I want to use the variable 'i' in my while loop.
my $i; while ($i<5) { $i++; }
will 'i' automatically be set to zero and incremented from there or do I have to say my $i=0; Is it a similar case with the ".=" on non-intialized vars?
Thanks.

Replies are listed 'Best First'.
Re: Intialization
by swiftone (Curate) on Jan 17, 2001 at 02:10 UTC
    You do not have to pre-initialize your variables, but if you have warnings on (and of course you do), you will be warned if you try to use the value of the undeclared variable (Note that auto increment/decrement is an exception to this).

    A few tests with perl -w should reveal all of this and the answers to similar questions in the same line.

Re: Intialization
by Fastolfe (Vicar) on Jan 17, 2001 at 02:12 UTC
    Scalars in Perl are automatically "initialized" to be undefined. In a numeric context, this is zero, and in a string context, this is an empty string. So for all intents and purposes, this behavior is probably acceptable. Similarly, arrays and hashes are set up empty.

    It will, however, generate warnings if you have warnings enabled if you attempt to use a new scalar in its undefined state as if it were defined (such as in math, or in string concatenation). Generally it's safer to initialize the variable when you declare it, unless you plan on explicitely assigning a value to it later. At the very minimum it helps you track down bugs in your code if you're 100% certain about the starting state of a variable.

    my $i = 0; my $str = ""; my $var; ... $var = 10;
Re: Intialization
by btrott (Parson) on Jan 17, 2001 at 02:14 UTC
    When used in a numeric context (like the above), $i will automatically take on the value of 0. When used in a string context, an undef value will take on the value of the empty string.

    However, I'm pretty sure that in some versions of Perl you get "unitialized value" warnings (when -w is turned on) when using an undef value in concatenation or in a numeric expression.

    This is somewhat confirmed by this thread on clpm.

    Also, note that if you have -w turned on, you'll get an "unitialized value" warning when you compare $i against 5 the first time through your while loop. See:

    % perl -w my $i; while ($i<5) { $i++; } <Ctrl-D> Use of uninitialized value at - line 2.
    Personally I find this rather annoying. :)
Re (tilly) 1: Intialization
by tilly (Archbishop) on Jan 18, 2001 at 06:57 UTC
    Minor style point. That loop is better written as:
    foreach my $i (0..4) { # Whatever }
    In fact usually you are better off just looping over arrays directly. See perlsyn for more.
Re: Intialization
by Beatnik (Parson) on Jan 17, 2001 at 02:35 UTC
    Recently I saw someone try to use the following line to predefine some scalars. He knew there were no var types to define, but didn't quite know what else to do.

    $var1,$var2,$var3,$var4;

    this is ofcourse quickly fixed by inserting a my, but basically it's pretty useless. Namespaces allocations will happen anyway, when the scalars are refered to for the first time.

    my($var1,$var2,$var3,$var4);

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.