This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wilma456's profile picture

load() with large code

Started by Wilma456, 18 November 2016 - 05:32 PM
Wilma456 #1
Posted 18 November 2016 - 06:32 PM
load() does not work with large code. For example:

file = fs.open("/rom/programs/edit","r")
load(file.readAll())("example")
This give me a string error. The same problem with other programs. Have anybody a solution for this? In the programm who I'm need it, the code is just a string and not are file like the example.
TheRockettek #2
Posted 18 November 2016 - 06:34 PM
you can do loadfile(path) i believe…
H4X0RZ #3
Posted 18 November 2016 - 07:31 PM
Maybe the code is malformed in some way? It should give you more than just a "string error".

Try using load instead of loadstring
Sewbacca #4
Posted 19 November 2016 - 12:17 AM
Maybe the code is malformed in some way? It should give you more than just a "string error".

Try using load instead of loadstring
He did.

I think the problem is the environment.
Shell is using a special environment to run programs with the shell and multishell API.
For some reasons, the shell API isn't global, so you have to add the API into the environment:

load(sFile, 'edit', setmetatable({shell = shell, multishell = multishell}, {__index = _G}))
Note, you need this code just if you wanna use the shell API.
Edited on 18 November 2016 - 11:18 PM
Dog #5
Posted 19 November 2016 - 02:15 AM
I know nothing about load, but in the code given by OP there are two discreet sets of brackets on the load line - is that even valid for load? To me it appears that he's attempting to load file.readAll() then he has "example" enclosed in a separate set of brackets.
H4X0RZ #6
Posted 19 November 2016 - 02:56 AM
<p>
I know nothing about load, but in the code given by OP there are two discreet sets of brackets on the load line - is that even valid for load?  To me it appears that he's attempting to load file.readAll() then he has "example" enclosed in a separate set of brackets.

Load returns a function (which is the string loaded as code) which you either save in some variable or execute directly.
local function foo() return function(bar)  print(bar) end end foo()("bar") --> prints bar
Damn mobile Firefox. Messes up the code…
Edited on 19 November 2016 - 01:58 AM
Dog #7
Posted 19 November 2016 - 03:12 AM
Oh, wow - I didn't know that was possible - thanks, H4X0RZ, I learned something new today :)/>
H4X0RZ #8
Posted 19 November 2016 - 11:15 AM
<p>
Oh, wow - I didn't know that was possible - thanks, H4X0RZ, I learned something new today :)/>
Btw, this allows you to do some funny stuff :D/>
I "like" "pancakes"
is (theoretically) valid syntax: it's a function taking one string as parameter, returning another function with yet another string as parameter.
Edited on 19 November 2016 - 10:15 AM