People keep giving me examples with carp instead of warn. Why? What makes carp better than warn?
相关问题
- $ENV{$variable} in perl
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Change first key of multi-dimensional Hash in perl
- How do I get a filehandle from the command line?
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- What is a good way to deploy a Perl application?
- Speeding up Selenium Webdriver
carp gives you more info as to where the message comes from (context)
produces
kinda silly for this small program but comes in handy when you want to know who called the method that's carp'ing.
carp works better for debugging within modules. If you are only writing a simple script, there is no benefit. From the Carp documenation:
I use
warn
for scripts and simple programs, andCarp
inside any modules. TheCarp
subroutines use the filename and line number where your current subroutine was called so it's easier to find who's causing the problem (not just where the problem manifested itself).Damian recommends
Carp
instead ofwarn
in "Reporting Failure" in Perl Best Practices, but doesn't make the distinction between scripts as top-level code constructs and modules as components that programs use.I've mostly not cared about that lately because I've been using Log::Log4perl to handle all of that.
Carp
reports errors from the caller's perspective. This is useful for modules where you typically want to warn about incorrect usage (e.g. a missing argument) and identify the place where the error occurred as opposed to where it was detected. This is especially important for utility functions that might be used in many places.Most authors use
warn
in scripts andcarp
in modules. Occasionally I usewarn
inside a module when I want the error message to reflect a problem in the module's implementation (e.g. a case that it should support but doesn't.) It's arguable thatcluck
would be better in such situations as it provides a full stack backtrace.