1CGI::Carp(3)          User Contributed Perl Documentation         CGI::Carp(3)
2
3
4

NAME

6       CGI::Carp - CGI routines for writing to the HTTPD (or other) error log
7

SYNOPSIS

9           use CGI::Carp;
10
11           croak "We're outta here!";
12           confess "It was my fault: $!";
13           carp "It was your fault!";
14           warn "I'm confused";
15           die  "I'm dying.\n";
16
17           use CGI::Carp qw(cluck);
18           cluck "I wouldn't do that if I were you";
19
20           use CGI::Carp qw(fatalsToBrowser);
21           die "Fatal error messages are now sent to browser";
22

DESCRIPTION

24       CGI scripts have a nasty habit of leaving warning messages in the error
25       logs that are neither time stamped nor fully identified.  Tracking down
26       the script that caused the error is a pain.  This fixes that.  Replace
27       the usual
28
29           use Carp;
30
31       with
32
33           use CGI::Carp
34
35       The standard warn(), die (), croak(), confess() and carp() calls will
36       be replaced with functions that write time-stamped messages to the HTTP
37       server error log.
38
39       For example:
40
41          [Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3.
42          [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.
43          [Fri Nov 17 21:40:43 1995] test.pl: I'm dying.
44

REDIRECTING ERROR MESSAGES

46       By default, error messages are sent to STDERR.  Most HTTPD servers
47       direct STDERR to the server's error log.  Some applications may wish to
48       keep private error logs, distinct from the server's error log, or they
49       may wish to direct error messages to STDOUT so that the browser will
50       receive them.
51
52       The "carpout()" function is provided for this purpose.  Since carpout()
53       is not exported by default, you must import it explicitly by saying
54
55          use CGI::Carp qw(carpout);
56
57       The carpout() function requires one argument, a reference to an open
58       filehandle for writing errors.  It should be called in a "BEGIN" block
59       at the top of the CGI application so that compiler errors will be
60       caught.  Example:
61
62          BEGIN {
63            use CGI::Carp qw(carpout);
64            open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
65              die("Unable to open mycgi-log: $!\n");
66            carpout(LOG);
67          }
68
69       carpout() does not handle file locking on the log for you at this
70       point.  Also, note that carpout() does not work with in-memory file
71       handles, although a patch would be welcome to address that.
72
73       The real STDERR is not closed -- it is moved to CGI::Carp::SAVEERR.
74       Some servers, when dealing with CGI scripts, close their connection to
75       the browser when the script closes STDOUT and STDERR.
76       CGI::Carp::SAVEERR is there to prevent this from happening prematurely.
77
78       You can pass filehandles to carpout() in a variety of ways.  The
79       "correct" way according to Tom Christiansen is to pass a reference to a
80       filehandle GLOB:
81
82           carpout(\*LOG);
83
84       This looks weird to mere mortals however, so the following syntaxes are
85       accepted as well:
86
87           carpout(LOG);
88           carpout(main::LOG);
89           carpout(main'LOG);
90           carpout(\LOG);
91           carpout(\'main::LOG');
92
93           ... and so on
94
95       FileHandle and other objects work as well.
96
97       Use of carpout() is not great for performance, so it is recommended for
98       debugging purposes or for moderate-use applications.  A future version
99       of this module may delay redirecting STDERR until one of the CGI::Carp
100       methods is called to prevent the performance hit.
101

MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW

103       If you want to send fatal (die, confess) errors to the browser, import
104       the special "fatalsToBrowser" subroutine:
105
106           use CGI::Carp qw(fatalsToBrowser);
107           die "Bad error here";
108
109       Fatal errors will now be echoed to the browser as well as to the log.
110       CGI::Carp arranges to send a minimal HTTP header to the browser so that
111       even errors that occur in the early compile phase will be seen.
112       Nonfatal errors will still be directed to the log file only (unless
113       redirected with carpout).
114
115       Note that fatalsToBrowser may not work well with mod_perl version 2.0
116       and higher.
117
118   Changing the default message
119       By default, the software error message is followed by a note to contact
120       the Webmaster by e-mail with the time and date of the error.  If this
121       message is not to your liking, you can change it using the
122       set_message() routine.  This is not imported by default; you should
123       import it on the use() line:
124
125           use CGI::Carp qw(fatalsToBrowser set_message);
126           set_message("It's not a bug, it's a feature!");
127
128       You may also pass in a code reference in order to create a custom error
129       message.  At run time, your code will be called with the text of the
130       error message that caused the script to die.  Example:
131
132           use CGI::Carp qw(fatalsToBrowser set_message);
133           BEGIN {
134              sub handle_errors {
135                 my $msg = shift;
136                 print "<h1>Oh gosh</h1>";
137                 print "<p>Got an error: $msg</p>";
138             }
139             set_message(\&handle_errors);
140           }
141
142       In order to correctly intercept compile-time errors, you should call
143       set_message() from within a BEGIN{} block.
144

DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS

146       If fatalsToBrowser in conjunction with set_message does not provide you
147       with all of the functionality you need, you can go one step further by
148       specifying a function to be executed any time a script calls "die", has
149       a syntax error, or dies unexpectedly at runtime with a line like
150       "undef->explode();".
151
152           use CGI::Carp qw(set_die_handler);
153           BEGIN {
154              sub handle_errors {
155                 my $msg = shift;
156                 print "content-type: text/html\n\n";
157                 print "<h1>Oh gosh</h1>";
158                 print "<p>Got an error: $msg</p>";
159
160                 #proceed to send an email to a system administrator,
161                 #write a detailed message to the browser and/or a log,
162                 #etc....
163             }
164             set_die_handler(\&handle_errors);
165           }
166
167       Notice that if you use set_die_handler(), you must handle sending HTML
168       headers to the browser yourself if you are printing a message.
169
170       If you use set_die_handler(), you will most likely interfere with the
171       behavior of fatalsToBrowser, so you must use this or that, not both.
172
173       Using set_die_handler() sets SIG{__DIE__} (as does fatalsToBrowser),
174       and there is only one SIG{__DIE__}. This means that if you are
175       attempting to set SIG{__DIE__} yourself, you may interfere with this
176       module's functionality, or this module may interfere with your module's
177       functionality.
178

SUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW

180       A problem sometimes encountered when using fatalsToBrowser is when a
181       "die()" is done inside an "eval" body or expression.  Even though the
182       fatalsToBrower support takes precautions to avoid this, you still may
183       get the error message printed to STDOUT.  This may have some
184       undesirable effects when the purpose of doing the eval is to determine
185       which of several algorithms is to be used.
186
187       By setting $CGI::Carp::TO_BROWSER to 0 you can suppress printing the
188       "die" messages but without all of the complexity of using
189       "set_die_handler".  You can localize this effect to inside "eval"
190       bodies if this is desirable: For example:
191
192        eval {
193          local $CGI::Carp::TO_BROWSER = 0;
194          die "Fatal error messages not sent browser"
195        }
196        # $@ will contain error message
197

MAKING WARNINGS APPEAR AS HTML COMMENTS

199       It is also possible to make non-fatal errors appear as HTML comments
200       embedded in the output of your program.  To enable this feature, export
201       the new "warningsToBrowser" subroutine.  Since sending warnings to the
202       browser before the HTTP headers have been sent would cause an error,
203       any warnings are stored in an internal buffer until you call the
204       warningsToBrowser() subroutine with a true argument:
205
206           use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
207           use CGI qw(:standard);
208           print header();
209           warningsToBrowser(1);
210
211       You may also give a false argument to warningsToBrowser() to prevent
212       warnings from being sent to the browser while you are printing some
213       content where HTML comments are not allowed:
214
215           warningsToBrowser(0);    # disable warnings
216           print "<script type=\"text/javascript\"><!--\n";
217           print_some_javascript_code();
218           print "//--></script>\n";
219           warningsToBrowser(1);    # re-enable warnings
220
221       Note: In this respect warningsToBrowser() differs fundamentally from
222       fatalsToBrowser(), which you should never call yourself!
223

OVERRIDING THE NAME OF THE PROGRAM

225       CGI::Carp includes the name of the program that generated the error or
226       warning in the messages written to the log and the browser window.
227       Sometimes, Perl can get confused about what the actual name of the
228       executed program was.  In these cases, you can override the program
229       name that CGI::Carp will use for all messages.
230
231       The quick way to do that is to tell CGI::Carp the name of the program
232       in its use statement.  You can do that by adding
233       "name=cgi_carp_log_name" to your "use" statement.  For example:
234
235           use CGI::Carp qw(name=cgi_carp_log_name);
236
237       .  If you want to change the program name partway through the program,
238       you can use the "set_progname()" function instead.  It is not exported
239       by default, you must import it explicitly by saying
240
241           use CGI::Carp qw(set_progname);
242
243       Once you've done that, you can change the logged name of the program at
244       any time by calling
245
246           set_progname(new_program_name);
247
248       You can set the program back to the default by calling
249
250           set_progname(undef);
251
252       Note that this override doesn't happen until after the program has
253       compiled, so any compile-time errors will still show up with the non-
254       overridden program name
255

TURNING OFF TIMESTAMPS IN MESSAGES

257       If your web server automatically adds a timestamp to each log line, you
258       may not need CGI::Carp to add its own. You can disable timestamping by
259       importing "noTimestamp":
260
261           use CGI::Carp qw(noTimestamp);
262
263       Alternatively you can set $CGI::Carp::NO_TIMESTAMP to 1.
264
265       Note that the name of the program is still automatically included in
266       the message.
267

GETTING THE FULL PATH OF THE SCRIPT IN MESSAGES

269       Set $CGI::Carp::FULL_PATH to 1.
270

AUTHOR INFORMATION

272       The CGI.pm distribution is copyright 1995-2007, Lincoln D. Stein. It is
273       distributed under GPL and the Artistic License 2.0. It is currently
274       maintained by Lee Johnson with help from many contributors.
275
276       Address bug reports and comments to:
277       https://github.com/leejo/CGI.pm/issues
278
279       The original bug tracker can be found at:
280       https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm
281
282       When sending bug reports, please provide the version of CGI.pm, the
283       version of Perl, the name and version of your Web server, and the name
284       and version of the operating system you are using.  If the problem is
285       even remotely browser dependent, please provide information about the
286       affected browsers as well.
287

SEE ALSO

289       Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,
290       CGI::Response.
291
292
293
294perl v5.28.1                      2018-08-15                      CGI::Carp(3)
Impressum