Scope modifiers: Difference between revisions
m
→{{header|Raku}}: Fix comments: Perl 6 --> Raku
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: Fix comments: Perl 6 --> Raku) |
||
Line 1,306:
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my $lexical-variable;
our $package-variable;
Line 1,314:
Function definitions are intrinsically lexical by default, but allow for forward references, unlike any other declaration.
Package variables, declared with <tt>our</tt>, are de-emphasized. Unlike in Perl 5, almost no built-ins use package declarations for anything other than type names, and most of Perl 5's global punctuational variables become dynamic variables instead, with the final recourse in the GLOBAL and PROCESS packages. The per-interpreter GLOBAL package is mainly for users; all predefined process-wide information is stored in the PROCESS symbol table instead. The <tt>our</tt> declarator actually just declares an alias to a variable of the same name in the current package, and is in a sense just permission to use that global variable in the current scope as if it were a lexical. Type and constant declarations, including enums, are intrinsically considered "our" declarations, since
State variables, declared with <tt>state</tt>, are persistent lexicals that are not re-initialized on each function entry, but retain their previous value. An initializer is run only the first time through. State variables are similar but not identical to C static variables; in
The <tt>has</tt> declarator is for declaring items in object scope.
Method declarations are implicitly in "has" scope.
In Perl 5, dynamic scoping is done via "local" to temporarily change the value of a global variable. This mechanism is still specced for
<lang perl6>sub a {
my $*dyn = 'a';
|