On Aug 13, 2:39 pm, [EMAIL PROTECTED] (Robert Hicks) wrote:
> I typically "use Carp;" and change my "die" and "warn" statements to
> "croak" and "carp". I have read a couple places croak/carp are a little
> better at telling you what and where than the standard die/warn.

There is no right or wrong here.  croak() and die() are used for two
different purposes.  You use each one when it's appropriate to use
them.

In a subroutine or method, you use croak/carp if the error is
something the caller of your code did wrong.  You use die/warn if the
error is something unexpected in your code.  For example:

package MyClass;
sub new {
   my $class = shift;
   my $name = shift or croak "Must pass name parameter to new()
method!";
   open my $cfg_fh, '<', 'MyClass.cfg' or die "Unable to open config
file: $!";
   chomp(my $attr = <$cfg_fh>);
   my $ref = { name => $name, attr => $attr };
   return bless $ref, $class;
}

If the user forgets to pass the name parameter to your class, you need
to tell the user which call to new() it was that has the error.  The
user doesn't care that it happened in line 4 of your module.

If the configuration file cannot be opened, you want to know where in
your module you attempted to open it so you can see if it has the
right name or whatnot.  You don't care where in the calling code new()
was called.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to