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)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The recommendation in Code Complete is important for language such as C because when you have
the value of
counteris whatever happens to occupy that memory.In Perl, when you declare a variable using
there is no doubt that the value of
$counterisundefnot 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:
is well defined in Perl.
$counterwill hold the value1.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.