I have the error attribute for every class for explaining to the caller what's gone wrong. And the caller should do:

my $result = $object->method(); unless(defined($result)) { $log->error("Something's gone wrong when called $object->method(): + " . $object->error_message); # ...doing something with that... }

When I need to return undef as a result (for example, if the method hasn't found the result - not because of an error occuried), I have to do something like that:

my $result = $object->method(); if($object->has_error) { $log->error("Something's gone wrong when called $object->method(): + " . $object->error_message); # ...doing something with that... } # ...doing something else, understanding that the method just doesn't +have anything to pass to us...

Are there people who prefer to pre-suppose all their methods are dying every time when something goes wrong, so all methods should be called through eval{}/try{}/...?

Sometimes I'm thinking about it as about a better way. For example it would allow me to return undef when nothing wrong have happened, so I'd prefer to die() with some explaination that would be accessible as the $@ variable.

So I consider about something that would look like that:

my $result = eval { $object->method() }; if($@) { $log->error("Something's gone wrong when called $object->method(): + $@"); # ...doing something with that... } # ...doing something else, understanding that the method just doesn't +have anything to pass to us...

Maybe it would be better to try some of TryCatch mechanisms from CPAN.

Anyway, using die() from the method really looks a bit odd. Who really does that? Have you been always doing it that way or started to do it that way some time ago?

Thank you!

V.Melnik

In reply to 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.