Hi.
I tried to write a Scribunto module in Lua today on Meta-Wiki called "Sort": https://meta.wikimedia.org/wiki/Module:Sort. I struggled a lot to get an environment set up where I could easily test blocks of code, so I'm looking for any help/guidance I can get.
The debug console seems finicky. Its line numbers never seem to make sense. For example, I input this into a fresh console:
--- function p.asc(frame) return frame.args[1] end ---
Then I try ...
= print(p.asc()) Lua error in console input at line 6: attempt to index local 'frame' (a nil value).
I have no idea what line 6 refers to. Earlier when I was testing it was saying line 14. The line numbers never seem to match up to what I think I'm testing.
If I paste this into the console:
--- local p = {}
function p.asc(frame) return frame.args[1] end
function p.desc(frame) return p.args[1] end
return p ---
I get "Lua error in console input at line 18: 'end' expected (to close 'function' at line 1) near 'return'."
There's a note on the edit screen about using mw.log(), but with various inputs, I still can't seem to get anything working properly with the debug console. For example:
--- function p.asc(frame) t = {} t['bar'] = 'baz' mw.log(t) return frame.args[1] end ---
This prints nothing at all when pasted into the debug console. If I try "= print(t)" or "print(t)" I get "nil".
Related to this, even when I'm able to get something to print or mw.log(), I usually end up printing the strings "table" or "nil", rather than the contents of a table. Is there a var_dump equivalent for Lua that can be used? Lua seems to be very focused on tables for storage, but dumping what's currently in a table at particular points in the code feels excruciating. I feel like I must be missing something. This is related to https://bugzilla.wikimedia.org/show_bug.cgi?id=48173.
Is there a better way to write/debug Lua modules? Any help writing these modules (or simply getting a working sort module on Meta-Wiki) would be appreciated.
MZMcBride