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
179   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       undesireable 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 desireable: 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

CHANGE LOG

257       3.51 Added $CGI::Carp::TO_BROWSER
258
259       1.29 Patch from Peter Whaite to fix the unfixable problem of CGI::Carp
260            not behaving correctly in an eval() context.
261
262       1.05 carpout() added and minor corrections by Marc Hedlund
263            <hedlund@best.com> on 11/26/95.
264
265       1.06 fatalsToBrowser() no longer aborts for fatal errors within
266            eval() statements.
267
268       1.08 set_message() added and carpout() expanded to allow for FileHandle
269            objects.
270
271       1.09 set_message() now allows users to pass a code REFERENCE for
272            really custom error messages.  croak and carp are now
273            exported by default.  Thanks to Gunther Birznieks for the
274            patches.
275
276       1.10 Patch from Chris Dean (ctdean@cogit.com) to allow
277            module to run correctly under mod_perl.
278
279       1.11 Changed order of &gt; and &lt; escapes.
280
281       1.12 Changed die() on line 217 to CORE::die to avoid -w warning.
282
283       1.13 Added cluck() to make the module orthogonal with Carp.
284            More mod_perl related fixes.
285
286       1.20 Patch from Ilmari Karonen (perl@itz.pp.sci.fi):  Added
287            warningsToBrowser().  Replaced <CODE> tags with <PRE> in
288            fatalsToBrowser() output.
289
290       1.23 ineval() now checks both $^S and inspects the message for the
291       "eval" pattern
292            (hack alert!) in order to accommodate various combinations of Perl
293       and
294            mod_perl.
295
296       1.24 Patch from Scott Gifford (sgifford@suspectclass.com): Add support
297            for overriding program name.
298
299       1.26 Replaced CORE::GLOBAL::die with the evil $SIG{__DIE__} because the
300            former isn't working in some people's hands.  There is no such
301       thing
302            as reliable exception handling in Perl.
303
304       1.27 Replaced tell STDOUT with bytes=tell STDOUT.
305

AUTHORS

307       Copyright 1995-2002, Lincoln D. Stein.  All rights reserved.
308
309       This library is free software; you can redistribute it and/or modify it
310       under the same terms as Perl itself.
311

SEE ALSO

313       Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,
314       CGI::Response.
315
316
317
318perl v5.16.3                      2012-11-03                      CGI::Carp(3)
Impressum