Reputation: 94920
Perl automatically initializes variables to undef
by default.
Is there a way to override this default behavior and tell the Perl interpreter to initialize variables to zero
(or some other fixed value)?
Upvotes: 8
Views: 11890
Reputation: 1
Why would I not simply initialize a variable just prior to its first use?
Answer: I keep lots of "stock code" lying about. The whole "just declare it" when first used doesn't really work with my production style. I might write something where "my $FirstVariable = 0;" is included as the first line of a code block; then later, insert something above that block that also references $FirstVariable. Now I've broken my routine by moving blocks of code about.
If I "declare" all the variables I'll be needing at the top, then I don't have to worry about rearranging blocks of code killing the procedure because of preceeding necessary variable declarations.
Also, if the variable declarations are at the top, I can easily comment one declaration out, then try running the routine. This can be handy for troubleshooting large, complex programs because it will produce a list, complete with line numbers, of every place that the [now undeclared] variable is referenced. When I'm done looking everything over, I just delete the comment character and I'm back in operation!
Upvotes: 0
Reputation: 46207
Do you have a concrete reason for wanting to do this, or is it simply "because Code Complete says I should"?
If the former, please share the reason and we can discuss properly Perly ways to accomplish 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 of... and I hate to use this phrase... best practices. Guidelines appropriate for one language do not necessarily apply to the other. "Always initialize variables (if possible) when you declare them" is a sound practice in C, but generally unnecessary in Perl.
Upvotes: 1
Reputation: 118148
The recommendation in Code Complete is important for language such as C because when you have
int f(void) {
int counter;
}
the value of counter
is whatever happens to occupy that memory.
In Perl, when you declare a variable using
my $counter;
there is no doubt that the value of $counter
is undef
not some random garbage.
Therefore, the motivation behind the recommendation, i.e. to ensure that all variables start out with known values, is automatically satisfied in Perl and it is not necessary to do anything.
What you do with counters is to increment or decrement them. The result of:
my $counter;
# ...
++ $counter;
is well defined in Perl. $counter
will hold the value 1
.
Finally, I would argue that, in most cases, counters are not necessary in Perl and code making extensive use of counter variables may need to be rewritten.
Upvotes: 13
Reputation: 40152
As far as I know, this is not possible (and shouldn't be, its even more dangerous than $[
).
You can initialize your variables as follows to cut down on boilerplate:
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);
Upvotes: 12
Reputation: 53986
No. Doing this can lead to some very scary and hard-to-decipher bugs, so it's not a good idea to change behaviour like this anyway.
In Perl, you can declare variables right when you need them for the first time, so there isn't generally a need to declare them first (with or without initialization) and then using them later. Additionally, operators such as ++
will work with undefined values equally well as zero, so you don't need to initialize counters at all:
# this is perfectly legal:
my $counter;
while ($some_loop)
{
$counter++;
}
However, I can insert a plug for Moose by mentioning that you can achieve automatic initialization of attributes 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 has value: initial value
number has value: 0
Upvotes: 5