Zack Lee
Zack Lee

Reputation: 3074

Is it possible to modify the Lua script to require?

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++.

Upvotes: 4

Views: 892

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

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.

Upvotes: 4

Related Questions