Perl – Initial Values and Context Based Evaluation

It’s important to remember that in Perl all scalar variables have an initial value of the null string, “”. You do not need to define a value for scalar variables unless you are using “strict” mode. Knowing this, what do you think the output of the below code will be?

Undefined scalar variable
Since the variable “$undefined_var” is undefined, the Perl interpreter assigns the null string default value. Because the null string is being used in an addition operation, Perl converts the null string to 0 and adds it to 5; the result is stored in the variable $result.

Output from using undefined variable

Output from using undefined variable

This entry was posted in Perl, Programming Languages, Technology and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


eight − = 2