First; how it should work: Let's imagine your module is Food::Product. Food::Product creates instances (objects). Let's try to instance a food product.. (code untested)
use Food::Product; my $p = new Food::Product('Tomato') or die("Could not instance Food::Product: ($Food::Product::errstr)" +); $p->isle(14); $p->stock(450); $p->sell(450) or die($p->errstr); $p->sell(40) or die($p->errstr);
Example of what is inside Food::Product
package Food::Product; use strict; use LEOCHARRE::Class2; # you do this your way use vars qw/$errstr/; __PACKAGE__->make_accessor_setget(qw/isle stock errst/); sub new { my($class,$arg)=@_; $arg or $errstr = "Missing valid arg" and return; return { name => $arg }, $class; } sub sell { my($self,$count)=@_; $count or $self->errstr('missing how much to sell ammount') and return; my $now = $self->stock or $self->errstr('no stock left to sell from') and return; my $left_over = ( $now - $count ) ... some other rationalle $self->stock($left_over); }

So in above examples.

Check out DBI, has some similarities.
I used to fumble with this, wondering if maybe $error was better a label.. but errstr is pretty freaking common.

So, to answer more directly. In c, it's common to return if something is up- in perl, we return when it's all good. So, store any messages in the class or object as a separate value. Do NOT assume the coder wants to access further information if something fails. Just make it possible to get more information. Whatever you do- don't return only if there's an error etc- that would be poor form in perl.


In reply to Re: How to return an appropriate error from module? by leocharre
in thread How to return an appropriate error from module? by zrajm

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.