Chapter 4. Statements and Declarations
A Perl program consists of a sequence of declarations and statements. A declaration may be placed anywhere a statement may be placed, but its primary effect occurs at compile time. A few declarations do double duty as ordinary statements, but most are totally transparent at runtime. After compilation, the main sequence of statements is executed just once.
In this chapter we cover statements before declarations, but we’d just like to mention a few of the more important declarations up front.
Unlike many programming languages, Perl does not (by default)
require variables to be explicitly declared; they spring into existence
upon their first use, whether you’ve declared them or not. However, if
you prefer, you may declare your variables using a declarator such as my, our, or state in front of the variable name wherever
it’s first mentioned, and then the compiler can be pretty sure that your
variable name isn’t a typo when you mention it again later.
If you try to use a value from a variable that’s never had a value
assigned to it, it’s quietly treated as 0 when used as a number, as "" (the null string) when used as a string, or
simply as false when used as a logical value. If you prefer to be warned
about using undefined values as though they were real strings or
numbers, the use warnings declaration
will take care of that.
Similarly, you can use the use
strict declaration to require yourself to declare all your variables in advance. If you use this, ...