Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How do I report an error back to the user of my object?

by tobyink (Canon)
on Jun 05, 2012 at 06:38 UTC ( [id://974413]=note: print w/replies, xml ) Need Help??


in reply to How do I report an error back to the user of my object?

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'

Replies are listed 'Best First'.
Re^2: How do I report an error back to the user of my object?
by kcott (Archbishop) on Jun 05, 2012 at 19:01 UTC
    use Carp qw/confess/;

    I might just point out that this code stops the importation of carp and croak.

    By default, Carp imports confess, carp and croak. So, unless you specifically wanted to exclude carp and croak, you can just write:

    use Carp;

    If you wanted to export a non-default symbol (e.g. cluck) but still include the defaults, you can write:

    use Carp qw{:DEFAULT cluck};

    See Exporter for a description of :DEFAULT and other Specialised Import Lists.

    -- Ken

      I might just point out that this code stops the importation of carp and croak.

      Indeed, but I was using neither of those so saw no need to import them.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://974413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found