Is it wrong...croak vs die...carp vs warn
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. Robert -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
On 8/13/07, Robert Hicks <[EMAIL PROTECTED]> 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. > > Robert The question of whether to use croak/carp or die/warn is one of where the error is. If the caller of the function passed in bad data croak/carp is the right choice. If the problem is with the current code or things it is calling then die/warn is the right choice. For instance, say I have a library function that takes the name of a file to write to and that file is not writable, this is not the library function's fault. It is the fault of the person who called the library function. If we were to use die then the file and line number would report the location in the library where the code failed, but this won't help the user find where he passed bad information. In this case croak is the appropriate choice. Keeping with this same example, lets say that this function also copies a file to another location and then does a chksum to make sure that the files are still the same and the chksum comes back wrong. In this case the issue is probably with the library code, so telling the user that his/her call to the function failed is fairly useless (he/she can't change his/her code to fix the problem), so die would be the right answer. However, if you really want to get the best picture of what was going on you should use confess/cluck instead of croak/carp or die/warn. It will spit out a full stack trace. Most of the time this is overkill (the problem is usually with either the caller of the function or the function itself), but it can be handy. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
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/
Re: Is it wrong...croak vs die...carp vs warn
On 8/13/07, Robert Hicks <[EMAIL PROTECTED]> 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. If to Carp is wrong, I don't want to be right. But you don't use them because they're "a little better", but because they're "the right tool for the job". Using croak or carp reports the error itself in much the same way as die or warn would, but they point the finger of blame at your module's caller. That is to say, if my code calls a subroutine in your module, and your subroutine in turn calls croak, then the error message is going to identify the bug as having occurred in MY code, instead of yours. The line number and file name report where I called your code. (See the Carp docs for the full story.) So, if you're trying to say, "you've misused my module", then carp and croak are the way to do it. If, on the other hand, you're trying to say, "something unexpected happened, and my code wasn't prepared to deal with it", perhaps warn and die will be more appropriate. It all depends, in the end, upon how you can best assist the person who will be unfortunate enough to see the error message. Cheers! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
--- Paul Lalli <[EMAIL PROTECTED]> wrote: > 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 > >From the Perl Review and my understanding as well, use Carp with keywords carp and croak is supposed to provide additional detail in your errors and warnings. Also similar to stating 'use diagnostics;' Pinpoint customers who are looking for what you sell. http://searchmarketing.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
On Aug 13, 4:33 pm, [EMAIL PROTECTED] (Oryann9) wrote: > From the Perl Review and my understanding as well, use > Carp with keywords carp and croak is supposed to > provide additional detail in your errors and warnings. Your understanding is wrong. They do not provide additional details. They provide *different* details. See the other three responses in this thread, from Chas, myself, and Tom. > Also similar to stating 'use diagnostics;' no. Carp has nothing to do with the diagnostics pragma. They are completely unrelated. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
On Aug 13, 9:33 pm, [EMAIL PROTECTED] (Oryann9) wrote: > From the Perl Review and my understanding as well, use > Carp with keywords carp and croak is supposed to > provide additional detail in your errors and warnings. Perl Review eh? That's foy isn't it? I'm just reviewing his book "Mastering Perl" at the moment and the section on carp/croak seems to be predicated on (or, at least, likely to impart) similar misconceptions. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Is it wrong...croak vs die...carp vs warn
--- "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Aug 13, 9:33 pm, [EMAIL PROTECTED] (Oryann9) > wrote: > > > From the Perl Review and my understanding as well, > use > > Carp with keywords carp and croak is supposed to > > provide additional detail in your errors and > warnings. > > Perl Review eh? That's foy isn't it? I'm just > reviewing his book > "Mastering Perl" at the moment and the section on > carp/croak seems to > be predicated on (or, at least, likely to impart) > similar > misconceptions. > Yes I was only stating what I read, but now I know the facts. cheers, Luggage? GPS? Comic books? Check out fitting gifts for grads at Yahoo! Search http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/