lua-users home
lua-l archive

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


On Tue, Nov 9, 2010 at 7:43 PM, sagasw <sagasw@gmail.com> wrote:
> Hi,
>
> When I try to use the json4lua, encounter a naive issue that make trouble
> for me.
>
> I download the json4lua 0.9.30 from http://json.luaforge.net/
> indeed the latest version is 0.9.50, but I don't think it is the point.
>
> I just use lua for windows, and place the folder json under e:\Lua\5.1\lua\.
> So under folder E:\Lua\5.1\lua\json, I could see 3 lua files "json.lua
> rpc.lua rpcserver.lua"
>
> some testing code like:
>
> ----------------------------------------
> json = require("json")
> testString = [[ { "one":1 , "two":2, "primes":[2,3,5,7] } ]]
> o = json.decode(testString)
> table.foreach(o,print)
> print ("Primes are:")
> table.foreach(o.primes,print)
>
> require ("json.rpc")
> result, error = json.rpc.call("http://jsolait.net/testj.py","echo","Test
> echo!")
> print(result)
> ----------------------------------------
>
>
> and the result like:
> ----------------------------------------
> one    1
> primes    table: 00630588
> two    2
> Primes are:
> 1    2
> 2    3
> 3    5
> 4    7
> lua: E:\lua\5.1\lua\json\rpc.lua:30: attempt to call global 'require' (a nil
> value)
> stack traceback:
>     E:\lua\5.1\lua\json\rpc.lua:30: in main chunk
>     [C]: in function 'require'
>     first.lua:8: in main chunk
>     [C]: ?
>>Exit code: 1
>
>
> You could see the test for json returns correct result.
> But failed to call rpc.lua, the rpc.lua looks like following, and failed
> part is the line "require('json')":
>
> module('json.rpc')
> local json = require('json')
> local http = require("socket.http")
>
> Could anyone tell me how to fix the issue? What should I do or make some
> setting change?
> thanks,
When you call 'module' you are changing the local environment so the
'require' function goes away.  You can perform the 'require' calls
right before module and you might be safe... so long as you don't call
any globals inside the json.rpc module

-- 
Thomas Harning Jr.