[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Inferring the location of a Lua script
- From: Peter Cawley <lua@...>
- Date: Mon, 26 Apr 2010 16:42:04 +0100
On Mon, Apr 26, 2010 at 4:25 PM, Lorenzo Donati
<lorenzo.donati.bz@tiscali.it> wrote:
> I need a way to make a Lua script know it's exact location, i.e. its
> absolute file system path (under Win XP is enough) using pure Lua and no C.
> My purpose is to build applications in pure Lua (or wxLua) that can
> automatically find their modules/components/data without any installation or
> shell script launcher/
Getting the absolute file path is difficult, but you don't need it.
All you need is for a Lua file to be able to know how it was loaded,
and then tweak that to load other things which are "near" it. If a Lua
file is loaded with a relative path, then you don't need to transform
it into an absolute path, as you know that that relative path gives
you the current file, and you know ahead of time where your other Lua
scripts are relative to the current path. My generic solution is to
stick the following line at the top of any file which calls "require":
package.path = debug.getinfo(1,
"S").source:match[[^@?(.*[\/])[^\/]-$]] .."?.lua;".. package.path
This line causes "require" to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory, insert the
relative path between " and ?.lua