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 run time 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
but also implements various magical compiler hints, known as
pragmas (or if you're into classical forms,
pragmata). Most pragmas are lexically scoped,
including the use strict 'vars' pragma which forces you to declare your variables before ...
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