I rarely use Lua so this might be a trivial question but I just noticed that with Lua 5.3.2, in the interactive REPL I can write for example:
> 1 == 2 or error('numbers not equal')
Which is how I'd perform error handling in Bash or Perl (or course the statement to the left of the or
would be something more useful in real code, e.g. two variables).
But if I paste 1 == 2 or error('numbers not equal')
into some file foo.lua
and then execute lua foo.lua
this results in lua: foo.lua:1: unexpected symbol near '1'
.
I can accept that this is invalid syntax but I find it very strange that the REPL would accept this statement nevertheless. Can someone explain what is going on here? Are to some general rules as to what I can do in the REPL that I can't do in a script?
The Lua REPL tries to evaluates input as expressions.(*) If it succeeds, it prints the results. This is for convenience only.
(*) It does that by prepending return
to the input and trying to run that as a script.