No such thing as a small change | |
PerlMonks |
Re: How do I report an error back to the user of my object?by kcott (Archbishop) |
on Jun 04, 2012 at 22:32 UTC ( [id://974389]=note: print w/replies, xml ) | Need Help?? |
$! is a special global variable that you shouldn't be modifying yourself. Take a look at perlvar - Error Variables; in particular, the section describing when $! is meaningful and meaningless. The first thing I'd do would be to move the error handling code into the module by changing the if statement to:
Now users of your module don't need to type or die "$!" after every call to the constructor.
Your loaddata() routine may be performing operations which could also raise fatal exceptions, such as opening a file, connecting to a database, and so on. You can trap these errors with eval like this:
You may want to use croak instead of die - see Carp. The first sentence of the description: "The Carp routines are useful in your own modules because they act like die() or warn(), but with a message which is more likely to be useful to a user of your module.". One final point related to the use of bless in your code. You have the single-argument form (bless $self); the two-argument form (bless $self => $class) is preferred. -- Ken
In Section
Seekers of Perl Wisdom
|
|