using 'return' to halt an executable Lua script

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

using 'return' to halt an executable Lua script

Joseph Manning
Consider the following simple executable Lua script:

   #!/usr/bin/env lua5.3

   if #arg ~= 2 then
      print( arg[ 0 ] .. " : two parameters expected" )
      return
      end

   -- continue normal processing with two parameters
   ...
   ...

My question is about the use of 'return' to halt the script.

Is this totally valid?  Yes, it seems to work fine in practice,
but I have not seen any explicit confirmation or examples of its use
in either the Lua Reference Manual or Pil4 or elsewhere.

I think it *should* be valid, since the script contents forms a chunk,
which is treated as an (anonymous) function, and the 'return' from
inside this function should be fine.  Am I correct?

The alternative is, of course, to use 'os.exit( )' -- but in the above
context, does using 'return' basically achieve the same effect?

Thanks for any wisdom that you can impart!

-- Joseph

------------------------------------------------------------------------
Joseph Manning / Computer Science / UCC Cork Ireland / [hidden email]
------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: using 'return' to halt an executable Lua script

nobody
On 2018-09-14 22:24, Joseph Manning wrote:
> My question is about the use of 'return' to halt the script.
>
> I think it *should* be valid, since the script contents forms a chunk,
> which is treated as an (anonymous) function, and the 'return' from
> inside this function should be fine.  Am I correct?

Yes.

The only difference is that a plain `return` will always exit with an
exit code of zero in the vanilla Lua interpreter, but it is common to
signal errors with a non-zero exit code.  Thus, `os.exit( false )` (or
`os.exit( false, true )` if you rely on __gc cleanup) is usually better.

A common idiom is something like

   function die( reason )
     io.stderr:write( arg[0], ": ", reason, "\n" )
     os.exit( false, true )
   end

with which you can

   die "two parameters expected"

(This also sends the error message to stderr, so that if the output is
piped into another program, the user will still get to see it and it
won't confuse the other program.)

-- nobody

Reply | Threaded
Open this post in threaded view
|

Re: using 'return' to halt an executable Lua script

Andrew Gierth
In reply to this post by Joseph Manning
>>>>> "Joseph" == Joseph Manning <[hidden email]> writes:

 Joseph> The alternative is, of course, to use 'os.exit( )' -- but in
 Joseph> the above context, does using 'return' basically achieve the
 Joseph> same effect?

os.exit() doesn't close the Lua state before exiting, whereas returning
from the invoked chunk does.

$ lua53 -e 't = setmetatable({},{__gc=function() print "foo" end})'
foo
$

$ lua53 -e 't = setmetatable({},{__gc=function() print "foo" end}) os.exit(0)'
$

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: using 'return' to halt an executable Lua script

Sean Conner
It was thus said that the Great Andrew Gierth once stated:
> >>>>> "Joseph" == Joseph Manning <[hidden email]> writes:
>
>  Joseph> The alternative is, of course, to use 'os.exit( )' -- but in
>  Joseph> the above context, does using 'return' basically achieve the
>  Joseph> same effect?
>
> os.exit() doesn't close the Lua state before exiting, whereas returning
> from the invoked chunk does.

  An optional parameter to os.exit() was added to Lua 5.2 to indicate
closing of the Lua state.  

> $ lua53 -e 't = setmetatable({},{__gc=function() print "foo" end})'
> foo
> $
>
> $ lua53 -e 't = setmetatable({},{__gc=function() print "foo" end}) os.exit(0)'
> $

$ lua-53 -e 't = setmetatable({},{__gc=function() print "foo" end}) os.exit(0,true)'
true
$

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: using 'return' to halt an executable Lua script

Dirk Laurie-2
In reply to this post by Joseph Manning
Op Vr., 14 Sep. 2018 om 22:24 het Joseph Manning <[hidden email]> geskryf:

>    #!/usr/bin/env lua5.3
>
>    if #arg ~= 2 then
>       print( arg[ 0 ] .. " : two parameters expected" )
>       return
>       end
>
>    -- continue normal processing with two parameters
>    ...
>    ...
>
> My question is about the use of 'return' to halt the script.
>
> Is this totally valid?  Yes, it seems to work fine in practice,
> but I have not seen any explicit confirmation or examples of its use
> in either the Lua Reference Manual or Pil4 or elsewhere.

Well, it would be very strange if "lua xxx.lua" had a mechanism for
running a file different from "dofile", and in the case of the latter
the manual does say

Opens the named file and executes its contents as a Lua chunk. ...
Returns all values returned by the chunk.