our
ourTYPEEXPR:ATTRIBUTESourEXPR:ATTRIBUTESourTYPEEXPRourEXPR
An our declares one or more variables to be valid globals within the
enclosing block, file, or eval. That
is, our has the same rules as a
my declaration for determination of
visibility, but does not create a new private variable; it merely allows
unfettered access to the existing package global. If more than one value
is listed, the list must be placed in parentheses.
The primary use of an our
declaration is to hide the variable from the effects of a use strict "vars" declaration; since the
variable is masquerading as a my
variable, you are permitted to use the declared global variable without
qualifying it with its package. However, just like the my variable, this only works within the
lexical scope of the our declaration.
In this respect, it differs from use
vars, which affects the entire package and is not lexically
scoped.
our is also like my in that you are allowed to declare
variables with a TYPE and with
ATTRIBUTES. Here is the syntax:
our Dog $spot :ears(short) :tail(long);
As of this writing, it’s not entirely clear what that will mean.
Attributes could affect either the global or the local interpretation of
$spot. On the one hand, it would be
most like my variables for attributes
to warp the current local view of $spot without interfering with other views of
the global in other places. On the other hand, if one module declares
$spot to be a Dog, and another declares $spot to be a Cat, you could end ...
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