Lua Unofficial FAQ Addition

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua Unofficial FAQ Addition

Rob Hoelz-2
Hello list,

I assume the maintainer of luafaq.org lurks this list, so I have a
question/answer I'd like added to the FAQ.

"How do I tell require to search relative to the current module?"

I've seen this question asked a dozen times on the list; it'd be nice
to be able to direct people to a consistent answer.

-Rob

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

steve donovan
On Wed, Oct 5, 2011 at 7:36 PM, Rob Hoelz <[hidden email]> wrote:
> "How do I tell require to search relative to the current module?"

It's a good question, because it illuminates the central role of package.path.

The solution I use is to look at _G.arg[0] and work out the base path
from that; this seems to work fine and is the basis for
app.require_here() in Penlight:  Then we modify package.(c)path
accordingly.

https://github.com/stevedonovan/Penlight/blob/master/lua/pl/app.lua

(If anybody _could_ see a potential problem with this method I would
be grateful)

I've seen people use debug information of the main script to find this
out, but I can't see any advantages of this.  It would not work if
debug information had been stripped, for instance.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

Patrick Rapin
> The solution I use is to look at _G.arg[0] and work out the base path
>from that; this seems to work fine
> (If anybody _could_ see a potential problem with this method I would
> be grateful)

If you first launch 'lua' standalone interpreter and then run script
with dofile
(this is how I do most of the time), it doesn't work. The table arg is not
defined in this case.

Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

steve donovan
On Thu, Oct 6, 2011 at 10:10 AM, Patrick Rapin <[hidden email]> wrote:
> (this is how I do most of the time), it doesn't work. The table arg is not
> defined in this case.

OK, so app.require_here() needs to fallback to using debug information
in this case.

But for the purposes of giving a 'simple' answer, I would say
_G.arg[0] is sufficient.  We can always break the answer into two
parts (a) find out where your script is and (b) patch package.path
accordingly.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

Patrick Donnelly
In reply to this post by steve donovan
On Thu, Oct 6, 2011 at 4:04 AM, steve donovan <[hidden email]> wrote:
> On Wed, Oct 5, 2011 at 7:36 PM, Rob Hoelz <[hidden email]> wrote:
>> "How do I tell require to search relative to the current module?"
>
> It's a good question, because it illuminates the central role of package.path.
>
> The solution I use is to look at _G.arg[0] and work out the base path
> from that; this seems to work fine and is the basis for
> app.require_here() in Penlight:  Then we modify package.(c)path
> accordingly.

Hmm, I interpreted this question to mean requiring sub-modules. For
example, instead of socket.lua doing:

require "socket.http"

it can instead do

require "http"

The solution for this is a new package search loader I would imagine.
Maybe this isn't what the OP wanted though.

--
- Patrick Donnelly

Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

Rob Hoelz-2
On Thu, 6 Oct 2011 11:53:27 -0400
Patrick Donnelly <[hidden email]> wrote:

> On Thu, Oct 6, 2011 at 4:04 AM, steve donovan
> <[hidden email]> wrote:
> > On Wed, Oct 5, 2011 at 7:36 PM, Rob Hoelz <[hidden email]> wrote:
> >> "How do I tell require to search relative to the current module?"
> >
> > It's a good question, because it illuminates the central role of
> > package.path.
> >
> > The solution I use is to look at _G.arg[0] and work out the base
> > path from that; this seems to work fine and is the basis for
> > app.require_here() in Penlight:  Then we modify package.(c)path
> > accordingly.
>
> Hmm, I interpreted this question to mean requiring sub-modules. For
> example, instead of socket.lua doing:
>
> require "socket.http"
>
> it can instead do
>
> require "http"
>
> The solution for this is a new package search loader I would imagine.
> Maybe this isn't what the OP wanted though.
>
That's usually how I interpret the question when someone wants to know
how to implement modules relative to the current module.


signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

steve donovan
On Thu, Oct 6, 2011 at 5:59 PM, Rob Hoelz <[hidden email]> wrote:
> That's usually how I interpret the question when someone wants to know
> how to implement modules relative to the current module.

Ah now I see - I misread 'program' for 'module'  (I've noticed people
worrying about that as well)

So the idea is to make a more relocatable package... it could be done
by further package.path hacking but this kind of global change could
cause ... interesting side-effects when other packages use require()

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Lua Unofficial FAQ Addition

Dimiter 'malkia' Stanev
In reply to this post by Rob Hoelz-2
What I'm doing is this:

Make a folder named "1", and put in there "1/a.lua" and "1/b.lua"

-- 1/a.lua
print("_G.arg[0] = [".._G.arg[0]..']')
print("      ... = [".. tostring(... and ... or "<null>") ..']')

and then

-- 1/b.lua
print("require('1/a')")
require( "1/a" )
print()
print("dofile('1/a')")
dofile( "1/a.lua" )

Experiment with it. So basically it boils down to _G.arg[0] and ...
... is only not null when a module was required. Here is what it prints
here:


E:\p\lua-testing\1>cd ..

E:\p\lua-testing>lua 1/a.lua
_G.arg[0] = [1/a.lua]
       ... = [<null>]

E:\p\lua-testing>lua 1/b.lua
require('1/a')
_G.arg[0] = [1/b.lua]
       ... = [1/a]

dofile('1/a')
_G.arg[0] = [1/b.lua]
       ... = [<null>]

E:\p\lua-testing>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
 > dofile('1/a.lua')
_G.arg[0] = [<null>]
       ... = [<null>]
 > dofile('1/b.lua')
require('1/a')
_G.arg[0] = [<null>]
       ... = [1/a]

dofile('1/a')
_G.arg[0] = [<null>]
       ... = [<null>]
 > require('1/a')
 > require('1/b')
require('1/a')

dofile('1/a')
_G.arg[0] = [<null>]
       ... = [<null>]

On 10/5/2011 10:36 AM, Rob Hoelz wrote:

> Hello list,
>
> I assume the maintainer of luafaq.org lurks this list, so I have a
> question/answer I'd like added to the FAQ.
>
> "How do I tell require to search relative to the current module?"
>
> I've seen this question asked a dozen times on the list; it'd be nice
> to be able to direct people to a consistent answer.
>
> -Rob