lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


John Passaniti wrote:
> 
> I'm confused.  What's going on here?  I typed the following into a file and
> ran it with the "lua" command:
> 
>      local x = 42
>      print(x)
> 
> The result was "42" was printed.  Then I tried the same thing interactively
> from the keyboard, and the result this time was nil.
> 
> The Lua documentation states that local variables may be declared anywhere
> inside a block.  Since running the code from a file worked, does that imply
> files have a implicit block around them?  The second question is what
> happened to "x" in the interactive case.  No error was reported by Lua, so
> I suppose it did what I told it.  But since the value was nil when I
> printed it, where did "x" go and will whatever memory it allocated be
> reclaimed?

The command prompt executes every line as a block, and therefor the
local x has only scope in the line that its entered in.. If you type

local x = 42\
print(x)

at the prompt it will work.. the \ is a linefeed without executing the
command..

When executing a file a global "local" is for that file.

/Erik