Oh, yes, that's exactly why I use die() only in object-builders, because I have a habit to new() through eval(). I've got this habit, because a certain number of other people's classes sometimes could die() in the builder().

But I think the descision to die() can have only the main module. Subroutines call other subroutine, if someone have an error occuried - okay, let them to return the special sign to the caller and the caller will report to its' caller and so on - until the process pop up to the main module. And the main module sends the long error message to the error stream: "Can't sub1(): Can't sub2(): Can't sub3(): Can't open connect to the database: ...".

I've made a tricky Error-class for my own usage. It has the method error(), when I call it with some parameter, it stores this parameter as the error message and returns undef, so I can do return($self->error("Something bad has happened")) and the caller will get undef. When it gets the undef value, it checks for $caller->has_error(). So I can be sure whenever the method has got an error or it just doesn't find any records in the database.

It's not worse than try/catch, but it let other people to call methods of my classes without being afraid to die().

V.Melnik

In reply to Re^2: Using die() in methods by v_melnik
in thread Using die() in methods by v_melnik

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.