Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found