my
myTYPEEXPR:ATTRIBUTESmyEXPR:ATTRIBUTESmyTYPEEXPRmyEXPR
This operator declares one or more private variables to exist
only within the innermost enclosing block, subroutine, eval, or file. If more than one variable is
listed, the list must be placed in parentheses because the operator
binds more tightly than commas. Only simple scalars or complete arrays
and hashes may be declared this way.
The variable name cannot be package qualified, because package
variables are all globally accessible through their corresponding symbol
table, and lexical variables are unrelated to any symbol table. Unlike
local, then, this operator has
nothing to do with global variables, other than hiding any other
variable of the same name from view within its scope (that is, where the
private variable exists). A global variable can always be accessed
through its package-qualified form, however, or through a symbolic
reference.
A private variable’s scope does not start until the statement after its declaration. The variable’s scope extends into any enclosed blocks thereafter, up to the end of the scope of the variable itself.
However, this means that any subroutines you call from within the scope of a private variable cannot see the private variable unless the block that defines the subroutine itself is also textually enclosed within the scope of that variable. That sounds complicated, but it’s not once you get the hang of it. The technical term for this is lexical scoping, so we often call these ...
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