Log In  


Hello,

I'm wondering if include has a return value similar to require that would allow me to keep my various modules defined locally and not pollute my global environment, or if there is a trick to using require in Picotron. I'm keeping things pretty organized, but it would be really nice to have the flexibility of require. I thought Picotron used Lua 5.4? Or is it just a subset?

Thanks,
Mushky



3

To the best of my knowledge include doesn't do that on its own and we don't have access to the regular Lua require for whatever reason. But luckily @elgopher and @snowkittykira both wrote require functions. I haven't use them myself but they seem like they should do the job. Both in this thread:
https://www.lexaloffle.com/bbs/?tid=140784


Thanks! I'll take a look at those!


Maybe I'm misunderstanding the topic.
I'll go ahead and assume you mean you want to load an external lua and get the return value without polluting your namespace.

This is not perfect, but...
How about doing it like OOP?

This is the external module that provides the values, my_module.lua, and the object name itself is MyModule.

MyModule = {}
MyModule.new = function()
	local buf = {}

	buf.say = function()
		return "Hello, world!"
	end	

	return buf
end

This is the main part.
We created a scope with do end and processed it so that no instances remain.

include "my_module.lua"

hello = ""
do
	local mm = MyModule.new()
	hello = mm:say()
end

print(hello)
print(mm)

The execution result will be

Hello, world!
nil

Also, if you want to create a library somewhere and use a shared lua script. Here, let's assume that the lua script is saved in "/appdata/lib".
In that case, you can load it like this.
Specify the absolute path.

include "/appdata/lib/my_lib.lua"

Note that this method almost never works properly on the web, and that it requires some extra work to make it work if you want to distribute the cart.



[Please log in to post a comment]