4
votes

When I call require 'name' in Lua, the name can be either a preloaded module name or a file that exists in a current working directory.

I have the following two questions:

A. I would like to know if it's possible to find out whether a preloaded module or a file will be required right before it will be required.

B. And if it's a file, I want to modify the script which will be required (by prepending/appending some code on top of existing one) and then require the modified script finally.

Are A and B both possible?

P.S.: I'm using Lua with C++.

1
@Yunnosch Sorry I edited that part. - Zack Lee
Now I get it, good edit. I can however not answer, sorry. - Yunnosch
In case you didn't know, require 'name' is the same as require('name'). In other words, it just calls whatever function value that the require variable refers to at the time the statement is executed. - Tom Blodget

1 Answers

4
votes

Are A and B both possible?

Yes, as you can write your own "require" function that does what you need (including everything you describe). You can also look at package.searchers, as registering your function as one of the searchers may be enough to implement what you want.