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
Ether
source share