.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "CGI::Carp 3pm"
.TH CGI::Carp 3pm "2020-02-08" "perl v5.30.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
CGI::Carp \- CGI routines for writing to the HTTPD (or other) error log
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&    use CGI::Carp;
\&
\&    croak "We\*(Aqre outta here!";
\&    confess "It was my fault: $!";
\&    carp "It was your fault!";   
\&    warn "I\*(Aqm confused";
\&    die  "I\*(Aqm dying.\en";
\&
\&    use CGI::Carp qw(cluck);
\&    cluck "I wouldn\*(Aqt do that if I were you";
\&
\&    use CGI::Carp qw(fatalsToBrowser);
\&    die "Fatal error messages are now sent to browser";
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\s-1CGI\s0 scripts have a nasty habit of leaving warning messages in the error
logs that are neither time stamped nor fully identified.  Tracking down
the script that caused the error is a pain.  This fixes that.  Replace
the usual
.PP
.Vb 1
\&    use Carp;
.Ve
.PP
with
.PP
.Vb 1
\&    use CGI::Carp
.Ve
.PP
The standard \fBwarn()\fR, die (), \fBcroak()\fR, \fBconfess()\fR and \fBcarp()\fR calls will
be replaced with functions that write time-stamped messages to the
\&\s-1HTTP\s0 server error log.
.PP
For example:
.PP
.Vb 3
\&   [Fri Nov 17 21:40:43 1995] test.pl: I\*(Aqm confused at test.pl line 3.
\&   [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.
\&   [Fri Nov 17 21:40:43 1995] test.pl: I\*(Aqm dying.
.Ve
.SH "REDIRECTING ERROR MESSAGES"
.IX Header "REDIRECTING ERROR MESSAGES"
By default, error messages are sent to \s-1STDERR.\s0  Most \s-1HTTPD\s0 servers
direct \s-1STDERR\s0 to the server's error log.  Some applications may wish
to keep private error logs, distinct from the server's error log, or
they may wish to direct error messages to \s-1STDOUT\s0 so that the browser
will receive them.
.PP
The \f(CW\*(C`carpout()\*(C'\fR function is provided for this purpose.  Since
\&\fBcarpout()\fR is not exported by default, you must import it explicitly by
saying
.PP
.Vb 1
\&   use CGI::Carp qw(carpout);
.Ve
.PP
The \fBcarpout()\fR function requires one argument, a reference to an open
filehandle for writing errors.  It should be called in a \f(CW\*(C`BEGIN\*(C'\fR
block at the top of the \s-1CGI\s0 application so that compiler errors will
be caught.  Example:
.PP
.Vb 6
\&   BEGIN {
\&     use CGI::Carp qw(carpout);
\&     open(LOG, ">>/usr/local/cgi\-logs/mycgi\-log") or
\&       die("Unable to open mycgi\-log: $!\en");
\&     carpout(LOG);
\&   }
.Ve
.PP
\&\fBcarpout()\fR does not handle file locking on the log for you at this
point.  Also, note that \fBcarpout()\fR does not work with in-memory file
handles, although a patch would be welcome to address that.
.PP
The real \s-1STDERR\s0 is not closed \*(-- it is moved to CGI::Carp::SAVEERR.
Some servers, when dealing with \s-1CGI\s0 scripts, close their connection to
the browser when the script closes \s-1STDOUT\s0 and \s-1STDERR.\s0
CGI::Carp::SAVEERR is there to prevent this from happening
prematurely.
.PP
You can pass filehandles to \fBcarpout()\fR in a variety of ways.  The \*(L"correct\*(R"
way according to Tom Christiansen is to pass a reference to a filehandle
\&\s-1GLOB:\s0
.PP
.Vb 1
\&    carpout(\e*LOG);
.Ve
.PP
This looks weird to mere mortals however, so the following syntaxes are
accepted as well:
.PP
.Vb 5
\&    carpout(LOG);
\&    carpout(main::LOG);
\&    carpout(main\*(AqLOG);
\&    carpout(\eLOG);
\&    carpout(\e\*(Aqmain::LOG\*(Aq);
\&
\&    ... and so on
.Ve
.PP
FileHandle and other objects work as well.
.PP
Use of \fBcarpout()\fR is not great for performance, so it is recommended
for debugging purposes or for moderate-use applications.  A future
version of this module may delay redirecting \s-1STDERR\s0 until one of the
CGI::Carp methods is called to prevent the performance hit.
.SH "MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW"
.IX Header "MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW"
If you want to send fatal (die, confess) errors to the browser, import
the special \*(L"fatalsToBrowser\*(R" subroutine:
.PP
.Vb 2
\&    use CGI::Carp qw(fatalsToBrowser);
\&    die "Bad error here";
.Ve
.PP
Fatal errors will now be echoed to the browser as well as to the log.
CGI::Carp arranges to send a minimal \s-1HTTP\s0 header to the browser so
that even errors that occur in the early compile phase will be seen.
Nonfatal errors will still be directed to the log file only (unless
redirected with carpout).
.PP
Note that fatalsToBrowser may \fBnot\fR work well with mod_perl version 2.0
and higher.
.SS "Changing the default message"
.IX Subsection "Changing the default message"
By default, the software error message is followed by a note to
contact the Webmaster by e\-mail with the time and date of the error.
If this message is not to your liking, you can change it using the
\&\fBset_message()\fR routine.  This is not imported by default; you should
import it on the \fBuse()\fR line:
.PP
.Vb 2
\&    use CGI::Carp qw(fatalsToBrowser set_message);
\&    set_message("It\*(Aqs not a bug, it\*(Aqs a feature!");
.Ve
.PP
You may also pass in a code reference in order to create a custom
error message.  At run time, your code will be called with the text
of the error message that caused the script to die.  Example:
.PP
.Vb 9
\&    use CGI::Carp qw(fatalsToBrowser set_message);
\&    BEGIN {
\&       sub handle_errors {
\&          my $msg = shift;
\&          print "<h1>Oh gosh</h1>";
\&          print "<p>Got an error: $msg</p>";
\&      }
\&      set_message(\e&handle_errors);
\&    }
.Ve
.PP
In order to correctly intercept compile-time errors, you should call
\&\fBset_message()\fR from within a BEGIN{} block.
.SH "DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS"
.IX Header "DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS"
If fatalsToBrowser in conjunction with set_message does not provide 
you with all of the functionality you need, you can go one step 
further by specifying a function to be executed any time a script
calls \*(L"die\*(R", has a syntax error, or dies unexpectedly at runtime
with a line like \*(L"undef\->\fBexplode()\fR;\*(R".
.PP
.Vb 7
\&    use CGI::Carp qw(set_die_handler);
\&    BEGIN {
\&       sub handle_errors {
\&          my $msg = shift;
\&          print "content\-type: text/html\en\en";
\&          print "<h1>Oh gosh</h1>";
\&          print "<p>Got an error: $msg</p>";
\&
\&          #proceed to send an email to a system administrator,
\&          #write a detailed message to the browser and/or a log,
\&          #etc....
\&      }
\&      set_die_handler(\e&handle_errors);
\&    }
.Ve
.PP
Notice that if you use \fBset_die_handler()\fR, you must handle sending
\&\s-1HTML\s0 headers to the browser yourself if you are printing a message.
.PP
If you use \fBset_die_handler()\fR, you will most likely interfere with 
the behavior of fatalsToBrowser, so you must use this or that, not 
both.
.PP
Using \fBset_die_handler()\fR sets SIG{_\|_DIE_\|_} (as does fatalsToBrowser),
and there is only one SIG{_\|_DIE_\|_}. This means that if you are 
attempting to set SIG{_\|_DIE_\|_} yourself, you may interfere with 
this module's functionality, or this module may interfere with 
your module's functionality.
.SH "SUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW"
.IX Header "SUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW"
A problem sometimes encountered when using fatalsToBrowser is
when a \f(CW\*(C`die()\*(C'\fR is done inside an \f(CW\*(C`eval\*(C'\fR body or expression.
Even though the
fatalsToBrower support takes precautions to avoid this,
you still may get the error message printed to \s-1STDOUT.\s0
This may have some undesirable effects when the purpose of doing the
eval is to determine which of several algorithms is to be used.
.PP
By setting \f(CW$CGI::Carp::TO_BROWSER\fR to 0 you can suppress printing
the \f(CW\*(C`die\*(C'\fR messages but without all of the complexity of using
\&\f(CW\*(C`set_die_handler\*(C'\fR.  You can localize this effect to inside \f(CW\*(C`eval\*(C'\fR
bodies if this is desirable: For example:
.PP
.Vb 5
\& eval {
\&   local $CGI::Carp::TO_BROWSER = 0;
\&   die "Fatal error messages not sent browser"
\& }
\& # $@ will contain error message
.Ve
.SH "MAKING WARNINGS APPEAR AS HTML COMMENTS"
.IX Header "MAKING WARNINGS APPEAR AS HTML COMMENTS"
It is also possible to make non-fatal errors appear as \s-1HTML\s0 comments
embedded in the output of your program.  To enable this feature,
export the new \*(L"warningsToBrowser\*(R" subroutine.  Since sending warnings
to the browser before the \s-1HTTP\s0 headers have been sent would cause an
error, any warnings are stored in an internal buffer until you call
the \fBwarningsToBrowser()\fR subroutine with a true argument:
.PP
.Vb 4
\&    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
\&    use CGI qw(:standard);
\&    print header();
\&    warningsToBrowser(1);
.Ve
.PP
You may also give a false argument to \fBwarningsToBrowser()\fR to prevent
warnings from being sent to the browser while you are printing some
content where \s-1HTML\s0 comments are not allowed:
.PP
.Vb 5
\&    warningsToBrowser(0);    # disable warnings
\&    print "<script type=\e"text/javascript\e"><!\-\-\en";
\&    print_some_javascript_code();
\&    print "//\-\-></script>\en";
\&    warningsToBrowser(1);    # re\-enable warnings
.Ve
.PP
Note: In this respect \fBwarningsToBrowser()\fR differs fundamentally from
\&\fBfatalsToBrowser()\fR, which you should never call yourself!
.SH "OVERRIDING THE NAME OF THE PROGRAM"
.IX Header "OVERRIDING THE NAME OF THE PROGRAM"
CGI::Carp includes the name of the program that generated the error or
warning in the messages written to the log and the browser window.
Sometimes, Perl can get confused about what the actual name of the
executed program was.  In these cases, you can override the program
name that CGI::Carp will use for all messages.
.PP
The quick way to do that is to tell CGI::Carp the name of the program
in its use statement.  You can do that by adding
\&\*(L"name=cgi_carp_log_name\*(R" to your \*(L"use\*(R" statement.  For example:
.PP
.Vb 1
\&    use CGI::Carp qw(name=cgi_carp_log_name);
.Ve
.PP
\&.  If you want to change the program name partway through the program,
you can use the \f(CW\*(C`set_progname()\*(C'\fR function instead.  It is not
exported by default, you must import it explicitly by saying
.PP
.Vb 1
\&    use CGI::Carp qw(set_progname);
.Ve
.PP
Once you've done that, you can change the logged name of the program
at any time by calling
.PP
.Vb 1
\&    set_progname(new_program_name);
.Ve
.PP
You can set the program back to the default by calling
.PP
.Vb 1
\&    set_progname(undef);
.Ve
.PP
Note that this override doesn't happen until after the program has
compiled, so any compile-time errors will still show up with the
non-overridden program name
.SH "TURNING OFF TIMESTAMPS IN MESSAGES"
.IX Header "TURNING OFF TIMESTAMPS IN MESSAGES"
If your web server automatically adds a timestamp to each log line,
you may not need CGI::Carp to add its own. You can disable timestamping
by importing \*(L"noTimestamp\*(R":
.PP
.Vb 1
\&    use CGI::Carp qw(noTimestamp);
.Ve
.PP
Alternatively you can set \f(CW$CGI::Carp::NO_TIMESTAMP\fR to 1.
.PP
Note that the name of the program is still automatically included in
the message.
.SH "GETTING THE FULL PATH OF THE SCRIPT IN MESSAGES"
.IX Header "GETTING THE FULL PATH OF THE SCRIPT IN MESSAGES"
Set \f(CW$CGI::Carp::FULL_PATH\fR to 1.
.SH "AUTHOR INFORMATION"
.IX Header "AUTHOR INFORMATION"
The \s-1CGI\s0.pm distribution is copyright 1995\-2007, Lincoln D. Stein. It is
distributed under the Artistic License 2.0. It is currently
maintained by Lee Johnson with help from many contributors.
.PP
Address bug reports and comments to: https://github.com/leejo/CGI.pm/issues
.PP
The original bug tracker can be found at: https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm
.PP
When sending bug reports, please provide the version of \s-1CGI\s0.pm, the version of
Perl, the name and version of your Web server, and the name and version of the
operating system you are using.  If the problem is even remotely browser
dependent, please provide information about the affected browsers as well.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Carp, CGI::Base, CGI::BasePlus, CGI::Request,
CGI::MiniSvr, CGI::Form, CGI::Response.
