Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: To initialise or not to initialise?

by Anonymous Monk
on Jun 24, 2004 at 10:34 UTC ( #369280=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://369280]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-10-18 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (74 votes). Check out past polls.

    Notices?