state
state EXPR state TYPE EXPR state EXPR : ATTRS state TYPE EXPR : ATTRS
The state declarator introduces a lexically scoped variable, just as my does. However, the contents of state
variables persist across calls to the same routine; such variables can
only be initialized once the first time the scope is entered and will
never be reinitialized, unlike lexical variables, which are
reinitialized each time their enclosing scope is entered.
When a closure is cloned, it is considered a new subroutine, so any state variables will be initialized in the new clone on first call. State variables are not static in the sense a C programmer would think of it, unless the routine itself is static.
State variables are enabled only when the use
feature "state" pragma is in effect. See the section feature in Chapter 29. Only initialization of
scalar state variables is fully supported at this time, though you may
always use a scalar reference to an array or hash.
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