What is the equivalent of die() command of perl

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

What is the equivalent of die() command of perl

Anurag Sharma
There is a Die command in perl which terminates the program immediately and prints the message

Die("Message");

Is there any equivalent in Lua.. If yes, what is it?

Also, is there any tutorial or guide that teaches Lua to perl programmers.

thanks
Anurag
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

steve donovan
On Thu, Sep 10, 2009 at 1:54 PM, Anurag Sharma
<[hidden email]> wrote:
> There is a Die command in perl which terminates the program immediately and
> prints the message
>
> Die("Message");
>
> Is there any equivalent in Lua.. If yes, what is it?

Well error("message") would work, but you would also get a stack
trace, which is not always so friendly for your users. So people will
often do this, which gives a message and no stacktrace.

function die (msg)
  io.stderr:write(msg,'\n')
  os.exit(1)
end

You can say things like this:

a = myfunction() or die 'myfunction failed!'

(note that the () is not necessary for functions with single string arguments)

But you can't do this

myfunction() or die 'finished!'

Because expressions in Lua cannot be statements.

steve d.
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

DracoBlue
In reply to this post by Anurag Sharma
Hello,

you can actually do the following:
print("Message")
os.exit()

to receive the same result.

Actually if I also want a stacktrace I usually use:
error("Message")
whats more like an "exception".

- Jan

Anurag Sharma schrieb:

> There is a Die command in perl which terminates the program
> immediately and prints the message
>
> Die("Message");
>
> Is there any equivalent in Lua.. If yes, what is it?
>
> Also, is there any tutorial or guide that teaches Lua to perl programmers.
>
> thanks
> Anurag

Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

steve donovan
In reply to this post by Anurag Sharma
On Thu, Sep 10, 2009 at 1:54 PM, Anurag Sharma
<[hidden email]> wrote:
> Is there any equivalent in Lua.. If yes, what is it?

Lua also uses assert() a lot. For instance,

assert(Myfunction(),"we failed")

More interesting is this:

f = assert(io.open(file))
...

This actually gives a sensible error trace, because if io.open fails
it returns two values, nil and an error message. The second parameter
of assert() is the message string, so it works as expected.

steve d.
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Luiz Henrique de Figueiredo
In reply to this post by steve donovan
> Well error("message") would work, but you would also get a stack
> trace, which is not always so friendly for your users.

You could do this:
        debug.traceback=nil
        error("message")
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Jerome Vuarand
2009/9/10 Luiz Henrique de Figueiredo <[hidden email]>:
>> Well error("message") would work, but you would also get a stack
>> trace, which is not always so friendly for your users.
>
> You could do this:
>        debug.traceback=nil
>        error("message")

Also if the script is embedded in a host application and run through
lua_pcall you don't get the stack trace unless an errfunc is explictly
provided.
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Miles Bader-2
In reply to this post by steve donovan
steve donovan <[hidden email]> writes:
> f = assert(io.open(file))
> ...
> This actually gives a sensible error trace, because if io.open fails
> it returns two values, nil and an error message. The second parameter
> of assert() is the message string, so it works as expected.

Gee, never noticed that before... and lots of lua functions have exactly
the right return profile to do this!

-Miles

--
Genealogy, n. An account of one's descent from an ancestor who did not
particularly care to trace his own.
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Pan, Shi Zhu
In reply to this post by steve donovan
You will get a stack backtrace only when you have not catch the lua
exception (use lua_pcall() in your C program to call your lua script
and all exception catched, or use pcall() in your lua program).

In fact you should always catch the lua exception in your host program
since many lua functions may call error() and you should at least
catch them in your main().

On Thu, Sep 10, 2009 at 8:00 PM, steve donovan
<[hidden email]> wrote:
> Well error("message") would work, but you would also get a stack
> trace, which is not always so friendly for your users. So people will
> often do this, which gives a message and no stacktrace.
>
Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Matthew M. Burke
In reply to this post by Anurag Sharma
Anurag Sharma wrote:
> Also, is there any tutorial or guide that teaches Lua to perl programmers.
>
>
>  
http://vtatila.kapsi.fi/luatut.html

And don't take this as a personal criticism, just a general rant :)
----  this is the first link returned by a google search for "lua for
perl programmers"



Matt

Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

Andrea D'Amore
In reply to this post by steve donovan

On 10/set/09, at 14:09, steve donovan wrote:

> f = assert(io.open(file))
> This actually gives a sensible error trace, because if io.open fails
> it returns two values, nil and an error message. The second parameter
> of assert() is the message string, so it works as expected.

Doesn't this apply when assert is used as a statement too?


--
Andrea

Reply | Threaded
Open this post in threaded view
|

Re: What is the equivalent of die() command of perl

steve donovan
On Mon, Sep 14, 2009 at 8:07 AM, Andrea D'Amore <[hidden email]> wrote:
> Doesn't this apply when assert is used as a statement too?

Sure.  Function calls are allowed to be statements.  The cool thing
about Lua's assert is that it can be used as a function.

You could say something Perlish like this:

f = io.open(file) or die 'cannot open'

But AFAIK there's no way then to use the actual error message returned.

steve d.