I'll defer to ikegami's (amended) reply for the differences between those two items. For completeness, I'll also mention another more rarely used option and explain their relative usefulness.

One may also wish to compare and contrast POSIX::_exit which exits the program immediately. It does not run destructors or END blocks and doesn't flush buffered I/O streams. Both die and exit do those things before they cause the program to exit to the OS.

The behavior usually wanted is that of die(). You might not be catching the condition now, but you may want to later.

Sometimes exit() is preferred. You might want to exit in a way that specifically always exits the program and ends the process. You might want to write your error condition somewhere other than STDERR then return a specific error code to the parent program. I've written some network server code that uses print() and exit() to do just that.

Only quite rarely will POSIX::_exit() be needed. Most people will probably never find a good reason to use it, but it's available in case it is needed.


In reply to Re: different between die and exit by mr_mischief
in thread different between die and exit by vinoth.ree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.