# info > CGI::Carp

---
type: CommandReference
command: CGI::Carp
mode: perldoc
section: 
source: perldoc
---

## Quick Reference
- `use CGI::Carp;` — Enable timestamped error logging to STDERR.  
- `use CGI::Carp qw(fatalsToBrowser);` — Send fatal errors to browser and log.  
- `use CGI::Carp qw(carpout); carpout(\*LOG);` — Redirect error output to a file.  
- `use CGI::Carp qw(warningsToBrowser); warningsToBrowser(1);` — Show warnings as HTML comments.  
- `use CGI::Carp qw(set_message); set_message("Custom note.");` — Change the browser error message.  
- `use CGI::Carp qw(cluck); cluck "msg";` — Emit a warning with full stack trace.  
- `local $CGI::Carp::TO_BROWSER = 0;` inside `eval` — Suppress browser output for that block.  
- `use CGI::Carp qw(noTimestamp);` — Omit timestamps from log messages.

## Name
CGI::Carp — CGI routines for writing timestamped, identified messages to the HTTPD error log.

## Synopsis
perl
use CGI::Carp;                           # basic carp/croak/confess
use CGI::Carp qw(fatalsToBrowser cluck warningsToBrowser carpout set_message);

croak "We're outta here!";
confess "It was my fault: $!";
carp "It was your fault!";
cluck "I wouldn't do that if I were you";

# send fatal errors to browser
use CGI::Carp qw(fatalsToBrowser);
die "Fatal error messages are now sent to browser";
## Options and Functions

**Error output control**
- `carpout($fh)` — Redirect all errors/warnings to the given filehandle. Import explicitly, call in `BEGIN` block to catch compile-time errors. Accepts a reference (`\*LOG`) or a GLOB name.
- `fatalsToBrowser` — Import to have `die()`/`confess()` messages sent to the browser (minimal HTTP header added). Use with care under mod_perl 2.0+.
- `$CGI::Carp::TO_BROWSER` — Set to 0 to suppress browser output of fatal errors. Often localized inside `eval` blocks.
- `warningsToBrowser()` — After HTTP headers are sent, call `warningsToBrowser(1)` to embed non‑fatal warnings as HTML comments in the output. Call `warningsToBrowser(0)` to disable temporarily.
- `set_message($str)` — Replace the default browser note with your own string or a subroutine reference. Must be called in `BEGIN`.
- `set_die_handler(\&sub)` — Completely override the fatal‑error handler; you must print headers yourself. Conflicts with `fatalsToBrowser`.

**Log message formatting**
- `noTimestamp` — Import or set `$CGI::Carp::NO_TIMESTAMP = 1` to omit timestamps from log entries.
- `$CGI::Carp::FULL_PATH` — Set to 1 to include the script’s full path in messages.
- `name=NAME` — Import argument to override the program name shown in log messages.
- `set_progname($name)` — Change the program name at runtime. `set_progname(undef)` restores the default.

## Examples

### Basic usage with timestamps
perl
use CGI::Carp;
carp "Something happened";   # => [timestamp] script.pl: Something happened
### Redirecting errors to a private log file
perl
BEGIN {
    use CGI::Carp qw(carpout);
    open(my $log, ">>", "/usr/local/cgi-logs/mycgi-log") or die "$!";
    carpout($log);
}
### Sending fatal errors to the browser
perl
use CGI::Carp qw(fatalsToBrowser);
die "Database connection failed";   # appears in browser and log
### Customising the browser error message
perl
use CGI::Carp qw(fatalsToBrowser set_message);
set_message("Please contact support with the above details.");
die "Something broke";
### Suppressing die output inside eval
perl
eval {
    local $CGI::Carp::TO_BROWSER = 0;
    die "This will not appear in the browser";
};
### Displaying warnings as HTML comments
perl
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI qw(:standard);
print header();
warningsToBrowser(1);
# … any warnings issued from here appear as <!-- comment --> in HTML
### Overriding the program name
perl
use CGI::Carp qw(name=mycgi);
# or later:
use CGI::Carp qw(set_progname);
set_progname("batch_import");
## See Also
- [Carp](https://perldoc.perl.org/Carp)
- [CGI](https://metacpan.org/pod/CGI)
- [CGI::Base](https://metacpan.org/pod/CGI::Base)
- [CGI::Request](https://metacpan.org/pod/CGI::Request)