in reply to Perl Style: Is initializing variables considered taboo?

One of the problems of mod_perl was that it didn't initialize variables; it simply reused the space. This meant that your uninitialized variables would have data from the previous incantation. I do believe that this has been fixed but there's no harm in initializing them anyway.

I always initialize them in case something like this happens again in the future. Just because you're not paranoid doesn't mean computers don't hate me. :)

Replies are listed 'Best First'.
Re^2: Perl Style: Is initializing variables considered taboo?
by ikegami (Patriarch) on Aug 22, 2010 at 21:03 UTC

    my $x; initialises. Any trailing = is just a normal assignment that occurs after the variable has already been created and initialised.

    The only situation I can think of is if you do

    my $x; sub foo { ...[ use $x ]... }

    If you use that as a Registry script, you'll have problems. Perl tells you about it with "will not stay shared" warnings. The fix is NOT to ignore the warning. You could fix it by switching to a package variables.

    our $x = undef; sub foo { ...[ use $x ]... }

    Unlike my, our doesn't initialise, so you have to initialise yourself.

    * — The implementation varies a little, but you shouldn't notice any difference unless you do something disallowed like my $x if $cond;