How can I automatically initialize all scalar variables in Perl?

Perl automatically initializes variables to undef by default.

Is there a way to override this default behavior and tell the Perl interpreter to initialize the variables to zero (or some other fixed value)?

+7
variables initialization perl
source share
4 answers

The recommendation in Code Complete is important for a language like C, because when you

 int f(void) { int counter; } 

counter value is what happens to occupy this memory.

In Perl, when you declare a variable with

 my $counter; 

There is no doubt that the value of $counter undef not random garbage.

Therefore, the motivation of the recommendation, that is, ensuring that all variables start with known values, is automatically performed in Perl, and nothing needs to be done.

What you do with counters is to increase or decrease them. Result:

 my $counter; # ... ++ $counter; 

well defined in Perl. $counter will hold the value 1 .

Finally, I would say that in most cases, counters are not needed in Perl, and code that requires extensive use of counting variables may need to be rewritten.

+12
source share

As far as I know, this is impossible (and should not be, it is even more dangerous than $[ ).

You can initialize your variables as follows to shorten the pattern:

 my ($x, $y, $z) = (0) x 3; 

or move initialization to a function:

 sub zero {$_ = 0 for @_} zero my ($x, $y, $z); 

or even:

 $_ = 0 for my ($x, $y, $z); 
+11
source share

Not. This can lead to some very scary and hard-to-reach mistakes, so changing behavior like this is not recommended.

In Perl, you can declare variables immediately when you need it the first time, so there is usually no need to declare them first (with or without initialization) and then use them later. In addition, operators such as ++ will work equally well with undefined values ​​equal to zero, so you do not need to initialize counters at all:

 # this is perfectly legal: my $counter; while ($some_loop) { $counter++; } 

However, I can insert the firmware for Moose , indicating that you can achieve automatic attribute initialization in your Moose classes:

 package MyClass; use strict; use warnings; use Moose; has some_string => ( is => 'rw', isa => 'Str', default => 'initial value', ); has some_number => ( is => 'rw', isa => 'Int', default => 0, ); __PACKAGE__->meta->make_immutable; 1; package main; my $object = MyClass->new; print "string has value: ", $object->some_string, "\n"; print "number has value: ", $object->some_number, "\n"; 

prints:

string matters: initial value
number matters: 0

+5
source share

Do you have a specific reason for this, or is it just "because Code Complete says I should"?

If the first, please share this reason, and we can discuss the Persian paths correctly to achieve your real goal.

If the latter, please remember that Code Complete is a set of guidelines for programming in C, not Perl. Perl is not C and has its own set of strengths and weaknesses, which also means that it has a different set ... and I hate using this phrase ... best practices. Guidelines appropriate for one language do not necessarily apply to another. "Always initialize variables (if possible) when you declare them" is good practice in C, but not needed in Perl.

0
source share

All Articles