When you call die and give it a message that doesn't end with a newline, perl helpfully tacks on the file and line where you called die. Carp provides the 'croak' function, which does essentially the same thing, except instead of telling you the file and line where you called croak, it tells you the file and line where the function containing the croak was called.
#!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { croak "croaked!"; } __END__ croaked! at test10.pl line 8 main::do_something() called at test10.pl line 5
#!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { die "died!"; } __END__ died! at test10.pl line 8.
The preferred one is whichever gives you the effect you desire. When writing modules I generally to use die for fatal errors that indicate something is wrong with the method that I'm calling die from, so that the error message points at that method, but use croak for fatal errors that indicate that the method was used incorrectly, so that the error message points towards the caller of the method, rather than the method itself.
In reply to Re: Croak or Die?
by jasonk
in thread Croak or Die?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |