http://qs321.pair.com?node_id=11101068


in reply to Re^2: What is an ordinary statement?
in thread What is an ordinary statement?

The first sequence compiles because $string has been declared before func is defined. The compiler doesn't care about the value a variable might contain, it only cares whether it knows about the variable by the time the variable is used. In this sequence, since the compiler already knows about the variable when it compiles the body of func, the compiler generates code to use that variable. If the variable wasn't already known, then it would either emit an error, if strict is in force, or it would auto-declare the variable as a package variable in the current package.

Of course, when the sequence is run, $string will not have a value assigned. If warnings are in effect, then Perl might complain that $string's value is undefined. However, the value will be treated as "" in this sequence.

Strictly speaking, Perl should complain the value is uninitialized, but Perl doesn't distinguish between uninitialized and the undefined value. But, at least Perl has the concept of the undefined value. Perl also has the concept of "not a number", or "NaN", which, mathematically, is defined as an "unrepresentable value".

(The undefined value is useful as an "invalid data" marker in some contexts. Without undefined, you have to use some other value as "invalid data" and hope it doesn't occur in any data set your program processes. I have seen some (non-Perl) programmers use "not a number" as "invalid measurement". Unfortunately for one of those, a data source was using that to indicate calculation errors. In other words, values that are unrepresentable. More importantly, this meant that the field technicians wasted time looking for the wrong problem.)