in reply to To initialise or not to initialise?

On the other hand it can be useful to initialise values, especially global ones, with a default value, so that if you need to debug later you could print out the variable to see if it changes.

my ($title, $page, $chapter) = "somethingglobal";

print ("$title");
sub ...
my $title = "somethingelocalhere";
print ("$title");

Also if you declare and initialise at the start of the program or of each sub, it is easier to read and maintain the code, especially to non-perl programmers.
  • Comment on Re: To initialise or not to initialise?

Replies are listed 'Best First'.
Re^2: To initialise or not to initialise?
by Fletch (Chancellor) on Jun 24, 2004 at 11:25 UTC
    my ($title, $page, $chapter) = "somethingglobal";

    You've only initialized $title. You probably mean ... = ("somethingglobal") x 3;</pedant>

      I learn something new today, thanks Fletch :)

      Sometimes I wanted to do that but I didn't know what the syntax is.

        There are several ways to do that. Here are two more.
        my ($foo, $bar, $baz) = map "default", 1..3; $_="default" for my ($foo, $bar, $baz);
        As obscure as the last one is, I'd prefer it over the others if you think that the variable list will expand since you don't have to synchronize the number of variables with a hard-coded constant.

        That is, if I wanted to achieve this effect. Which I never have needed to.