Path: Computer > Computer > Lua Related Stuff > LuaStrict > LuaStrict Example Code

LuaStrict Example Code

A few simple examples to demonstrate how to use the LuaStrict module.

StrictWeak.lua: shows the basics

require('Strict')
-- The so-called 'weak' mode is the default after require'ing Strict
-- 'Weak' means globals have to be initialised in the main part of a
-- program before they can be used (similar to strict.lua)
-- So z, below, is correctly initialised, whereas y is not
function test1()
&local e
&print(z)      -- z has been initialised, so OK
&e=os.exit     -- os.exit exists, so OK
&e=string.exit -- not there, error
end
function test2()
&y=9  -- if y wasn't initialised in main part, this is an error
&print(y)
end
z=0
print(pcall(test1))
-- y=1
print(pcall(test2))
y=2 -- initialise and be happy
print(pcall(test2))

StrictStrong.lua: using the strong mode

require('Strict')
-- 'Import' a couple of functions into globals, for convenience
declareGlobal,isDeclared=Strict.declareGlobal,Strict.isDeclared
-- ... then switch from default (weak) to strong checking
-- 'Strong' means that *all* global variables have to be declared
--  via a call to declareGlobal()
Strict.strong=true
-- Show is a global and as such has to be declared
declareGlobal('show')
function show(b)
&if b then return 'on' else return 'off' end
end
--[[ NB: the code above and this are equivalent:
declareGlobal('show',function (b)
&if b then return 'on' else return 'off' end
end)
--]]
-- Switch off strict (ie neither weak nor strong)
Strict.strict=false
print('Strict mode is now '..show(Strict.strict))
-- Use a few globals
x=97
y=98
z=99
print('-> '..x)
print('-> '..y)
print('-> '..z)
-- Define a global function
function square(n)
&print('-> '..n*n)
end
-- Switch back on
Strict.strict=true
print('Strict mode is now '..show(Strict.strict))
declareGlobal('Bond',007)
print('-> '..Bond)
x=997
y=998
z=999
square(x)
square(y)
square(z)
square(Bond)
-- check is a local, no declaration needed
local function check(n)
&if isDeclared(n) then print(n..' is declared')
&else print(n..' is not declared') end
end
check('Q')
declareGlobal('Q','clever')
check('Q')
declareGlobal('M')
print(M)   -- M may be nil
check('M') -- ... but it's still declared
notDeclared=0

StrictCustomHandler.lua: shows how to use a custom handler

-- This shows how to (mis-)use a custom handler function
require('Strict')
Strict.strong=true
-- Set new handler function
Strict.handler=function (module,name,f)
&-- Normally, a handler would terminate the script in an appropriate manner
&print('Dunno what '..Strict.getModulename(module)..'.'..name..' means...')
end
-- Simple way to import declareGlobal even after having set Strict.strong
local declareGlobal=Strict.declareGlobal
-- Easier to define a function as local than to declareGlobal() it:-)
local function show(n)
&if n==nil then print('Oops!')
&else print(n) end
end
declareGlobal('x',7)
y=123   -- this triggers the first message
show(x)
show(y) -- triggers second message
Strict.handler=function (module,name,v)
&-- This handler is even more dodgy though it should (and seems to) work
&if v~=nil then -- if there's a value why not auto-declare name?
&&print(name..' undeclared, will now be forced into existence...')
&&declareGlobal(name,v,module) -- this doesn't declare 'name'!
&end
end
show(y) -- no joy
y=123   -- this will trigger the code in the handler above
show(y) -- ... and now all is well

StrictDeclare.lua: a simpler way to declare multiple variables

-- This quicky shows a perhaps more convenient approach to declare
-- globals than using a number of calls to Strict.declareGlobal()
require('Strict')
Strict.strong=true
local function declareGlobal(t,m)
&for k,v in pairs(t) do Strict.declareGlobal(k,v,m) end
end
declareGlobal {
&x=97, y=98, z=99, show=function (...) print(...) end, s='test'
}
show(show,x,y,z,s)


$updated from: LuaStrict Example Code.htxt Thu 27 Apr 2017 10:06:49 thomasl (By Thomas Lauer)$