@@ -223,14 +223,15 @@ a function's formal parameter is equivalent to a local variable.)
223223
224224All chunks start with an implicit declaration @T{global *},
225225which declares all free names as global variables;
226- this implicit declaration becomes void inside the scope of any other
227- @Rw{global} declaration, regardless of the names being declared.
226+ this preambular declaration becomes void inside the scope of any other
227+ @Rw{global} declaration,
228+ as the following example illustrates:
228229@verbatim{
229230X = 1 -- Ok, global by default
230231do
231232 global Y -- voids implicit initial declaration
232- X = 1 -- ERROR, X not declared
233233 Y = 1 -- Ok, Y declared as global
234+ X = 1 -- ERROR, X not declared
234235end
235236X = 2 -- Ok, global by default again
236237}
@@ -1110,9 +1111,9 @@ and cannot be used as names:
11101111@index{reserved words}
11111112@verbatim{
11121113and break do else elseif end
1113- false for function goto if in
1114- local nil not or repeat return
1115- then true until while
1114+ false for function global goto if
1115+ in local nil not or repeat
1116+ return then true until while
11161117}
11171118
11181119Lua is a case-sensitive language:
@@ -1653,7 +1654,8 @@ The declaration for locals can include an initialization:
16531654@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}}
16541655@producname{stat}@producbody{@Rw{global} attnamelist}
16551656@producname{attnamelist}@producbody{
1656- @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}}
1657+ @bnfNter{Name} @bnfopt{attrib}
1658+ @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
16571659}
16581660If present, an initial assignment has the same semantics
16591661of a multiple assignment @see{assignment}.
@@ -1662,24 +1664,55 @@ Otherwise, all local variables are initialized with @nil.
16621664Each variable name may be postfixed by an attribute
16631665(a name between angle brackets):
16641666@Produc{
1665- @producname{attrib}@producbody{@bnfopt{@ bnfter{<} @bnfNter{Name} @bnfter{>} }}
1667+ @producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
16661668}
16671669There are two possible attributes:
16681670@id{const}, which declares a @emph{constant} or @emph{read-only} variable,
16691671@index{constant variable}
1670- that is, a variable that cannot be assigned to
1671- after its initialization;
1672+ that is, a variable that cannot be used as the left-hand side of an
1673+ assignment,
16721674and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
16731675A list of variables can contain at most one to-be-closed variable.
16741676Only local variables can have the @id{close} attribute.
16751677
1678+ Lua offers also a collective declaration for global variables:
1679+ @Produc{
1680+ @producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}}
1681+ }
1682+ This special form implicitly declares
1683+ as globals all names not explicitly declared previously.
1684+ In particular,
1685+ @T{global * <const>} implicitly declares
1686+ as read-only globals all names not explicitly declared previously;
1687+ see the following example:
1688+ @verbatim{
1689+ global X
1690+ global * <const>
1691+ print(math.pi) -- Ok, 'print' and 'math' are read-only
1692+ X = 1 -- Ok, declared as read-write
1693+ Y = 1 -- Error, Y is read-only
1694+ }
1695+
1696+ As noted in @See{globalenv},
1697+ all chunks start with an implicit declaration @T{global *},
1698+ but this preambular declaration becomes void inside
1699+ the scope of any other @Rw{global} declaration.
1700+ Therefore, a program that does not use global declarations
1701+ or start with @T{global *}
1702+ has free read-write access to any global;
1703+ a program that starts with @T{global * <const>}
1704+ has free read-only access to any global;
1705+ and a program that starts with any other global declaration
1706+ (e.g., @T{global none}) can only refer to declared variables.
1707+
16761708Note that, for global variables,
1677- the @emph{read-only} atribute is only a syntactical restriction :
1709+ the effect of any declaration is only syntactical:
16781710@verbatim{
1679- global X <const>
1680- X = 1 -- ERROR
1681- _ENV.X = 1 -- Ok
1682- foo() -- 'foo' can freely change the global X
1711+ global X <const>, _G
1712+ X = 1 -- ERROR
1713+ _ENV.X = 1 -- Ok
1714+ _G.print(X) -- Ok
1715+ foo() -- 'foo' can freely change any global
16831716}
16841717
16851718A chunk is also a block @see{chunks},
@@ -9453,7 +9486,12 @@ change between versions.
94539486@itemize{
94549487
94559488@item{
9456- The control variable in @Rw{for} loops are read only.
9489+ The word @Rw{global} is a reserved word.
9490+ Do not use it as a regular name.
9491+ }
9492+
9493+ @item{
9494+ The control variable in @Rw{for} loops is read only.
94579495If you need to change it,
94589496declare a local variable with the same name in the loop body.
94599497}
@@ -9582,12 +9620,14 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
95829620@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody
95839621@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
95849622@OrNL @Rw{global} attnamelist
9623+ @OrNL @Rw{global} @bnfter{*} @bnfopt{attrib}
95859624}
95869625
95879626@producname{attnamelist}@producbody{
9588- @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}}
9627+ @bnfNter{Name} @bnfopt{attrib}
9628+ @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
95899629
9590- @producname{attrib}@producbody{@bnfopt{@ bnfter{<} @bnfNter{Name} @bnfter{>} }}
9630+ @producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
95919631
95929632@producname{retstat}@producbody{@Rw{return}
95939633 @bnfopt{explist} @bnfopt{@bnfter{;}}}
0 commit comments