Jump to content

Perl Programming/Keywords/state

From Wikibooks, open books for an open world
Previous: stat Keywords Next: study

The state keyword

[edit | edit source]

state declares a lexically scoped variable like my, but variables declared with state will never be reinitialized

If the variable list VARIABLES of state has more than one element, the list has to be placed in brackets. In such a list, undef can be used as a placeholder.

If more than one state with the same name are declared within the same lexical scope, Perl issues a warning, as it is the case with multiple declarations, but they are seen as redundant.

state variables can only be used with the use feature "state" pragma, unless it is written fully as CORE::state.

Syntax

[edit | edit source]
  state VARIABLES
  state TYPE VARIABLES
  state VARIABLES : ATTRIBUTES
  state TYPE VARIABLES : ATTRIBUTES

See also

[edit | edit source]
Previous: stat Keywords Next: study