- 论坛徽章:
- 7
|
Perlvim 发表于 2012-12-07 12:41 ![]()
default variable declaration is global, but local variable is more faster, Why Lua use global variable as default declaration?
following code could not run yesterday, but it is worked before
local filename = 'perlvim.lua'
local fh = io.open(filename, 'r')
local text = io.read("*all")
print(text)
Don't know what you mean by saying Lua use global variable as default declaration. It's a good practise to declare variables before using them, and when declaring them you should use local as most as you can.
Only you can use a variable before you declare them in lua, which makes it a global variable with a nil value, which is not desired at the most cases and should be avoided.
And one thing, assign a value to a variable is not the same as declare a variable. AFAIK, Lua only have one keyword for declare variables, which is 'local'.
Maybe you were thinking about something like following which works for me(Pay attention to the third line):
- local filename = 'perlvim.lua'
- local fh = io.open(filename, 'r')
- local text = fh:read("*all")
- print(text)
复制代码 |
|