When you call die and give it a message that doesn't end with a newline, perl helpfully tacks on the file and line where you called die. Carp provides the 'croak' function, which does essentially the same thing, except instead of telling you the file and line where you called croak, it tells you the file and line where the function containing the croak was called.

#!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { croak "croaked!"; } __END__ croaked! at test10.pl line 8 main::do_something() called at test10.pl line 5
#!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { die "died!"; } __END__ died! at test10.pl line 8.

The preferred one is whichever gives you the effect you desire. When writing modules I generally to use die for fatal errors that indicate something is wrong with the method that I'm calling die from, so that the error message points at that method, but use croak for fatal errors that indicate that the method was used incorrectly, so that the error message points towards the caller of the method, rather than the method itself.


www.jasonkohles.com
We're not surrounded, we're in a target-rich environment!

In reply to Re: Croak or Die? by jasonk
in thread Croak or Die? by Anonymous Monk

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.