# perldoc > CGI::Carp

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

## Quick Reference
- `use CGI::Carp; die "message";` — timestamped log messages
- `use CGI::Carp qw(fatalsToBrowser); die "message";` — fatal errors sent to browser
- `use CGI::Carp qw(carpout); BEGIN { open LOG, ... ; carpout(LOG); }` — redirect errors to filehandle
- `use CGI::Carp qw(set_message); set_message("custom text");` — customize fatal error message
- `use CGI::Carp qw(warningsToBrowser); ... warningsToBrowser(1);` — embed warnings as HTML comments
- `use CGI::Carp qw(cluck); cluck "trace";` — warn with stack trace
- `use CGI::Carp qw(noTimestamp);` — disable auto-timestamps
- `local $CGI::Carp::TO_BROWSER = 0;` — suppress die messages inside eval

## Name
CGI::Carp — CGI routines for writing timestamped, program-identified messages to the HTTPD (or other) error log

## Synopsis
perl
use CGI::Carp;

croak "We're outta here!";
confess "It was my fault: $!";
carp "It was your fault!";
warn "I'm confused";
die  "I'm dying.\n";

use CGI::Carp qw(cluck fatalsToBrowser warningsToBrowser carpout set_message set_die_handler set_progname noTimestamp);
cluck "Stack trace";
fatalsToBrowser(1);
warningsToBrowser(1);
carpout(\*LOG);
set_message("Error occurred");
set_die_handler(\&handler);
set_progname("mycgi.pl");
## Options
### Exported Error Functions
- `carp "message"` — warn with timestamp and program name (replaces `warn`)
- `croak "message"` — die with timestamp (replaces `die`)
- `confess "message"` — die with full stack trace
- `cluck "message"` — warn with stack trace (must import explicitly)

### Special Imports
- `fatalsToBrowser` — send fatal `die`/`confess` to browser with minimal HTTP header; use `import qw(fatalsToBrowser)`
- `warningsToBrowser` — buffer non-fatal warnings until `warningsToBrowser(1)` is called, then output as HTML comments; use `warningsToBrowser(0)` to disable
- `carpout(FILEHANDLE)` — redirect all error output to given filehandle; must be imported and called in `BEGIN` block. Original STDERR saved in `CGI::Carp::SAVEERR`
- `set_message("string"|sub { ... })` — override the default fatal error message shown to browser; accepts text or coderef; call in `BEGIN`
- `set_die_handler(\&coderef)` — define a custom handler for all `die`/syntax errors; must handle HTTP headers yourself. Conflicts with `fatalsToBrowser`.
- `set_progname("name")` — change the logged program name; `set_progname(undef)` resets to default. Alternatively use `use CGI::Carp qw(name=cgi_carp_log_name)`
- `noTimestamp` — disable timestamp prefix in log messages

### Package Variables
- `$CGI::Carp::TO_BROWSER` — set to 0 to suppress printing die messages to browser (useful inside `eval`)
- `$CGI::Carp::NO_TIMESTAMP` — set to 1 to disable timestamps (same as `noTimestamp`)
- `$CGI::Carp::FULL_PATH` — set to 1 to include full script path in messages

## Examples
perl
# Basic usage: all standard errors now timestamped
use CGI::Carp;
warn "Low memory";
die "Failed to connect";

# Send fatal errors to browser
use CGI::Carp qw(fatalsToBrowser);
die "Invalid input - exiting";

# Custom fatal message with coderef
use CGI::Carp qw(fatalsToBrowser set_message);
BEGIN {
    set_message(sub {
        my $msg = shift;
        print "<h1>Error</h1><p>$msg</p>";
    });
}

# Redirect errors to a custom log file
use CGI::Carp qw(carpout);
BEGIN {
    open my $log, ">>", "/var/log/mycgi.log" or die $!;
    carpout($log);
}

# Embed warnings as HTML comments
use CGI::Carp qw(warningsToBrowser);
print header();
warningsToBrowser(1);  # start outputting
warn "Something fishy";
warningsToBrowser(0);  # pause output

# Suppress die messages inside eval
eval {
    local $CGI::Carp::TO_BROWSER = 0;
    die "Not shown to browser";
};

# Change the logged program name at runtime
use CGI::Carp qw(set_progname);
set_progname("worker.pl");
## See Also
- [Carp](https://www.chedong.com/phpMan.php/perldoc/Carp)
- [CGI::Base](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ABase)
- [CGI::BasePlus](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ABasePlus)
- [CGI::Request](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ARequest)
- [CGI::MiniSvr](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AMiniSvr)
- [CGI::Form](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AForm)
- [CGI::Response](https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3AResponse)