Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use require? #14

Closed
luoqi opened this issue Jan 22, 2011 · 2 comments
Closed

how to use require? #14

luoqi opened this issue Jan 22, 2011 · 2 comments

Comments

@luoqi
Copy link

luoqi commented Jan 22, 2011

i use require like:
local xxx = require('xxx')
but,it also haven't to work.

--test.lua
function a()
ngx.print("test.lua");
end;

a();

--concat.lua
local test=require("test");


at the first request,it print:
test.lua

and,the next time,have nothing print;

@agentzh
Copy link
Member

agentzh commented Jan 24, 2011

This is more like a Lua question. To understand why this happens, see http://www.lua.org/manual/5.1/manual.html#pdf-require

To quote the key sentence:

"The (require) function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module. "

That is, Lua's "require" will not re-load a module for the sake of performance.

So you're coding up Lua modules in a totally wrong way. We usually use Lua modules this way instead:

-- test.lua
module("test", package.seeall)
function a()
    print("test.lua")
end

And then in your content_by_lua_file directive's lua file:

-- concat.lua
local test = require("test")
test.a()

You will see the "test.lua" output on every request as you'd expect.

@luoqi
Copy link
Author

luoqi commented Jan 24, 2011

thank you!!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants