lua-users home
lua-l archive

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


Hi all,

In another thread, Graham mentioned strict.lua, which forces any
variable to be declared before it's used. This email discusses how to
use it if your Lua was installed by your Linux distribution instead of
by a Lua tarball.

When you RTFM strict.lua, evvrybody shows you the code of strict.lua
itself, and nobody shows you how to incorporate it. They all say it's
"in your Lua distribution's etc directory", which helps not a bit if
your Lua was installed by your distro's package manager, because you
very likely won't have an etc directory. So here's what I did...

First I made this test program:

=====================================
#!/usr/bin/lua
require ('strict')
print("arr assign follows")
--a, b = "A", "B"
print(a)
print(b)
a, b = b, a
print(a)
print(b)
=====================================
Notice I commented out the declarations of a and b.

Then I ran it:
=====================================
slitt@mydesk:~$ ./test2.lua 
/usr/bin/lua: ./test2.lua:2: module 'strict' not found:
        no field package.preload['strict']
        no file './strict.lua'
        no file '/usr/local/share/lua/5.1/strict.lua'
        no file '/usr/local/share/lua/5.1/strict/init.lua'
        no file '/usr/local/lib/lua/5.1/strict.lua'
        no file '/usr/local/lib/lua/5.1/strict/init.lua'
        no file '/usr/share/lua/5.1/strict.lua'
        no file '/usr/share/lua/5.1/strict/init.lua'
        no file './strict.so'
        no file '/usr/local/lib/lua/5.1/strict.so'
        no file '/usr/lib/lua/5.1/strict.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        ./test2.lua:2: in main chunk
        [C]: ?
slitt@mydesk:~$
=====================================
Obviously it failed to find strict.lua, but now I have a list
of places to put strict.lua once I download it. I decided to put it
in /usr/local/share/lua/5.1/.

Now I did the following:

* lua -v  to find out my version
* Download my version's tarball
* Copy strict.lua out of tarball and into /usr/local/share/lua/5.1/strict.lua
* chmod root.root /usr/local/share/lua/5.1/strict.lua
* run the program and note that it errors out on undeclared a
* Comment out require('strict') and note that now it prints nils
* Uncomment both the require and the declarations, and note that it now
  works.
* Change the declarations to assign nil to both, and note that now it
  runs but prints nils (as expected). As long as you declare the
  variables, even with nils, the program runs and if there's an error
  produces a runtime error. But if you use a variable that has not been
  declared, it produces an informative compile time error.

Thanks

SteveT