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.)


In reply to Re^3: What is an ordinary statement? by RonW
in thread What is an ordinary statement? by ntj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.