our @ISA = qw/Exporter/; our @EXPORTER = qw/new/;

If you're writing object-oriented code, you should not be exporting anything. And you should sure as hell not be exporting new ever.

Don't use the one argument form of bless - use the two argument form. It's more friendly to people wanting to subclass your classes...

bless $self, $class;

Don't return undef to indicate a terminal failure. Just die with an error message. (Or better, Carp::croak or Carp::confess.)

use Carp qw/confess/; sub new { my ($class, @args) = @_; my $self = bless { name => 'new' }, $class; $self->loaddata; return $self; } sub loaddata { my ($self) = @_; # stuff happens if ($something_bad_happened) { confess "Error loading data!"; } }

Why? Mostly because it forces your caller to not ignore your error message. Right now you say you're calling your code like this...

my $obj = coolobject->new or die "$!";

... but really the caller shouldn't have to remember to detect errors and die. The die should just happen. (For this reason the default behaviour of many Perl built-ins, such as open, is pretty dumb. The autodie pragma fixes them.)

If the caller is really sure they want to ignore coolobject failures, then they can always wrap the object construction in eval like this:

my $obj = eval { coolobject->new }; # $obj might be undef
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: How do I report an error back to the user of my object? by tobyink
in thread How do I report an error back to the user of my object? by SomeNetworkGuy

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.