Scoped Declarations
Like global declarations, lexically scoped declarations have an
effect at the time of compilation. Unlike global declarations,
lexically scoped declarations only apply from the point of the
declaration through the end of the innermost enclosing scope (block,
file, or eval—whichever comes
first). That’s why we call them lexically scoped, though perhaps
“textually scoped” would be more accurate, since lexical scoping has
little to do with lexicons. But computer scientists the world over
know what “lexically scoped” means, so we perpetuate the usage
here.
Perl also supports dynamically scoped declarations. A dynamic scope also extends to the end of the innermost enclosing block, but in this case, “enclosing” is defined dynamically at runtime rather than textually at compile time. To put it another way, blocks nest dynamically by invoking other blocks, not by including them. This nesting of dynamic scopes may correlate somewhat to the nesting of lexical scopes, but the two are generally not identical, especially when any subroutines have been invoked.
We mentioned that some aspects of use could be considered global declarations,
but other aspects of use are
lexically scoped. In particular, use not only imports package symbols, it
also implements various magical compiler hints, known as pragmas (or if you’re into classical
Greek, pragmata). Most pragmas are lexically
scoped, including the strict pragma we mention from time to time. See the later
section Pragmas. (Hence, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access