Filename | /usr/share/perl/5.14/CGI/Carp.pm |
Statements | Executed 28 statements in 3.99ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 3.14ms | 3.37ms | BEGIN@335 | CGI::Carp::
1 | 1 | 1 | 1.06ms | 1.13ms | BEGIN@333 | CGI::Carp::
1 | 1 | 1 | 55µs | 258µs | import | CGI::Carp::
1 | 1 | 1 | 10µs | 10µs | BEGIN@340 | CGI::Carp::
1 | 1 | 1 | 3µs | 3µs | CORE:match (opcode) | CGI::Carp::
0 | 0 | 0 | 0s | 0s | _longmess | CGI::Carp::
0 | 0 | 0 | 0s | 0s | _warn | CGI::Carp::
0 | 0 | 0 | 0s | 0s | carp | CGI::Carp::
0 | 0 | 0 | 0s | 0s | carpout | CGI::Carp::
0 | 0 | 0 | 0s | 0s | cluck | CGI::Carp::
0 | 0 | 0 | 0s | 0s | confess | CGI::Carp::
0 | 0 | 0 | 0s | 0s | croak | CGI::Carp::
0 | 0 | 0 | 0s | 0s | die | CGI::Carp::
0 | 0 | 0 | 0s | 0s | fatalsToBrowser | CGI::Carp::
0 | 0 | 0 | 0s | 0s | id | CGI::Carp::
0 | 0 | 0 | 0s | 0s | ineval | CGI::Carp::
0 | 0 | 0 | 0s | 0s | realdie | CGI::Carp::
0 | 0 | 0 | 0s | 0s | realwarn | CGI::Carp::
0 | 0 | 0 | 0s | 0s | set_die_handler | CGI::Carp::
0 | 0 | 0 | 0s | 0s | set_message | CGI::Carp::
0 | 0 | 0 | 0s | 0s | set_progname | CGI::Carp::
0 | 0 | 0 | 0s | 0s | stamp | CGI::Carp::
0 | 0 | 0 | 0s | 0s | to_filehandle | CGI::Carp::
0 | 0 | 0 | 0s | 0s | warn | CGI::Carp::
0 | 0 | 0 | 0s | 0s | warningsToBrowser | CGI::Carp::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package CGI::Carp; | ||||
2 | |||||
3 | =head1 NAME | ||||
4 | |||||
5 | B<CGI::Carp> - CGI routines for writing to the HTTPD (or other) error log | ||||
6 | |||||
7 | =head1 SYNOPSIS | ||||
8 | |||||
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 | |||||
23 | =head1 DESCRIPTION | ||||
24 | |||||
25 | CGI scripts have a nasty habit of leaving warning messages in the error | ||||
26 | logs that are neither time stamped nor fully identified. Tracking down | ||||
27 | the script that caused the error is a pain. This fixes that. Replace | ||||
28 | the usual | ||||
29 | |||||
30 | use Carp; | ||||
31 | |||||
32 | with | ||||
33 | |||||
34 | use CGI::Carp | ||||
35 | |||||
36 | And the standard warn(), die (), croak(), confess() and carp() calls | ||||
37 | will automagically be replaced with functions that write out nicely | ||||
38 | time-stamped messages to the HTTP server error log. | ||||
39 | |||||
40 | For example: | ||||
41 | |||||
42 | [Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3. | ||||
43 | [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied. | ||||
44 | [Fri Nov 17 21:40:43 1995] test.pl: I'm dying. | ||||
45 | |||||
46 | =head1 REDIRECTING ERROR MESSAGES | ||||
47 | |||||
48 | By default, error messages are sent to STDERR. Most HTTPD servers | ||||
49 | direct STDERR to the server's error log. Some applications may wish | ||||
50 | to keep private error logs, distinct from the server's error log, or | ||||
51 | they may wish to direct error messages to STDOUT so that the browser | ||||
52 | will receive them. | ||||
53 | |||||
54 | The C<carpout()> function is provided for this purpose. Since | ||||
55 | carpout() is not exported by default, you must import it explicitly by | ||||
56 | saying | ||||
57 | |||||
58 | use CGI::Carp qw(carpout); | ||||
59 | |||||
60 | The carpout() function requires one argument, which should be a | ||||
61 | reference to an open filehandle for writing errors. It should be | ||||
62 | called in a C<BEGIN> block at the top of the CGI application so that | ||||
63 | compiler errors will be caught. Example: | ||||
64 | |||||
65 | BEGIN { | ||||
66 | use CGI::Carp qw(carpout); | ||||
67 | open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or | ||||
68 | die("Unable to open mycgi-log: $!\n"); | ||||
69 | carpout(LOG); | ||||
70 | } | ||||
71 | |||||
72 | carpout() does not handle file locking on the log for you at this point. | ||||
73 | Also, note that carpout() does not work with in-memory file handles, although | ||||
74 | a patch would be welcome to address that. | ||||
75 | |||||
76 | The real STDERR is not closed -- it is moved to CGI::Carp::SAVEERR. Some | ||||
77 | servers, when dealing with CGI scripts, close their connection to the | ||||
78 | browser when the script closes STDOUT and STDERR. CGI::Carp::SAVEERR is there to | ||||
79 | prevent this from happening prematurely. | ||||
80 | |||||
81 | You can pass filehandles to carpout() in a variety of ways. The "correct" | ||||
82 | way according to Tom Christiansen is to pass a reference to a filehandle | ||||
83 | GLOB: | ||||
84 | |||||
85 | carpout(\*LOG); | ||||
86 | |||||
87 | This looks weird to mere mortals however, so the following syntaxes are | ||||
88 | accepted as well: | ||||
89 | |||||
90 | carpout(LOG); | ||||
91 | carpout(main::LOG); | ||||
92 | carpout(main'LOG); | ||||
93 | carpout(\LOG); | ||||
94 | carpout(\'main::LOG'); | ||||
95 | |||||
96 | ... and so on | ||||
97 | |||||
98 | FileHandle and other objects work as well. | ||||
99 | |||||
100 | Use of carpout() is not great for performance, so it is recommended | ||||
101 | for debugging purposes or for moderate-use applications. A future | ||||
102 | version of this module may delay redirecting STDERR until one of the | ||||
103 | CGI::Carp methods is called to prevent the performance hit. | ||||
104 | |||||
105 | =head1 MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW | ||||
106 | |||||
107 | If you want to send fatal (die, confess) errors to the browser, ask to | ||||
108 | import the special "fatalsToBrowser" subroutine: | ||||
109 | |||||
110 | use CGI::Carp qw(fatalsToBrowser); | ||||
111 | die "Bad error here"; | ||||
112 | |||||
113 | Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp | ||||
114 | arranges to send a minimal HTTP header to the browser so that even errors that | ||||
115 | occur in the early compile phase will be seen. | ||||
116 | Nonfatal errors will still be directed to the log file only (unless redirected | ||||
117 | with carpout). | ||||
118 | |||||
119 | Note that fatalsToBrowser may B<not> work well with mod_perl version 2.0 | ||||
120 | and higher. | ||||
121 | |||||
122 | =head2 Changing the default message | ||||
123 | |||||
124 | By default, the software error message is followed by a note to | ||||
125 | contact the Webmaster by e-mail with the time and date of the error. | ||||
126 | If this message is not to your liking, you can change it using the | ||||
127 | set_message() routine. This is not imported by default; you should | ||||
128 | import it on the use() line: | ||||
129 | |||||
130 | use CGI::Carp qw(fatalsToBrowser set_message); | ||||
131 | set_message("It's not a bug, it's a feature!"); | ||||
132 | |||||
133 | You may also pass in a code reference in order to create a custom | ||||
134 | error message. At run time, your code will be called with the text | ||||
135 | of the error message that caused the script to die. Example: | ||||
136 | |||||
137 | use CGI::Carp qw(fatalsToBrowser set_message); | ||||
138 | BEGIN { | ||||
139 | sub handle_errors { | ||||
140 | my $msg = shift; | ||||
141 | print "<h1>Oh gosh</h1>"; | ||||
142 | print "<p>Got an error: $msg</p>"; | ||||
143 | } | ||||
144 | set_message(\&handle_errors); | ||||
145 | } | ||||
146 | |||||
147 | In order to correctly intercept compile-time errors, you should call | ||||
148 | set_message() from within a BEGIN{} block. | ||||
149 | |||||
150 | =head1 DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS | ||||
151 | |||||
152 | If fatalsToBrowser in conjunction with set_message does not provide | ||||
153 | you with all of the functionality you need, you can go one step | ||||
154 | further by specifying a function to be executed any time a script | ||||
155 | calls "die", has a syntax error, or dies unexpectedly at runtime | ||||
156 | with a line like "undef->explode();". | ||||
157 | |||||
158 | use CGI::Carp qw(set_die_handler); | ||||
159 | BEGIN { | ||||
160 | sub handle_errors { | ||||
161 | my $msg = shift; | ||||
162 | print "content-type: text/html\n\n"; | ||||
163 | print "<h1>Oh gosh</h1>"; | ||||
164 | print "<p>Got an error: $msg</p>"; | ||||
165 | |||||
166 | #proceed to send an email to a system administrator, | ||||
167 | #write a detailed message to the browser and/or a log, | ||||
168 | #etc.... | ||||
169 | } | ||||
170 | set_die_handler(\&handle_errors); | ||||
171 | } | ||||
172 | |||||
173 | Notice that if you use set_die_handler(), you must handle sending | ||||
174 | HTML headers to the browser yourself if you are printing a message. | ||||
175 | |||||
176 | If you use set_die_handler(), you will most likely interfere with | ||||
177 | the behavior of fatalsToBrowser, so you must use this or that, not | ||||
178 | both. | ||||
179 | |||||
180 | Using set_die_handler() sets SIG{__DIE__} (as does fatalsToBrowser), | ||||
181 | and there is only one SIG{__DIE__}. This means that if you are | ||||
182 | attempting to set SIG{__DIE__} yourself, you may interfere with | ||||
183 | this module's functionality, or this module may interfere with | ||||
184 | your module's functionality. | ||||
185 | |||||
186 | =head2 SUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW | ||||
187 | |||||
188 | A problem sometimes encountered when using fatalsToBrowser is | ||||
189 | when a C<die()> is done inside an C<eval> body or expression. | ||||
190 | Even though the | ||||
191 | fatalsToBrower support takes precautions to avoid this, | ||||
192 | you still may get the error message printed to STDOUT. | ||||
193 | This may have some undesireable effects when the purpose of doing the | ||||
194 | eval is to determine which of several algorithms is to be used. | ||||
195 | |||||
196 | By setting C<$CGI::Carp::TO_BROWSER> to 0 you can suppress printing the C<die> messages | ||||
197 | but without all of the complexity of using C<set_die_handler>. | ||||
198 | You can localize this effect to inside C<eval> bodies if this is desireable: | ||||
199 | For example: | ||||
200 | |||||
201 | eval { | ||||
202 | local $CGI::Carp::TO_BROWSER = 0; | ||||
203 | die "Fatal error messages not sent browser" | ||||
204 | } | ||||
205 | # $@ will contain error message | ||||
206 | |||||
207 | |||||
208 | =head1 MAKING WARNINGS APPEAR AS HTML COMMENTS | ||||
209 | |||||
210 | It is now also possible to make non-fatal errors appear as HTML | ||||
211 | comments embedded in the output of your program. To enable this | ||||
212 | feature, export the new "warningsToBrowser" subroutine. Since sending | ||||
213 | warnings to the browser before the HTTP headers have been sent would | ||||
214 | cause an error, any warnings are stored in an internal buffer until | ||||
215 | you call the warningsToBrowser() subroutine with a true argument: | ||||
216 | |||||
217 | use CGI::Carp qw(fatalsToBrowser warningsToBrowser); | ||||
218 | use CGI qw(:standard); | ||||
219 | print header(); | ||||
220 | warningsToBrowser(1); | ||||
221 | |||||
222 | You may also give a false argument to warningsToBrowser() to prevent | ||||
223 | warnings from being sent to the browser while you are printing some | ||||
224 | content where HTML comments are not allowed: | ||||
225 | |||||
226 | warningsToBrowser(0); # disable warnings | ||||
227 | print "<script type=\"text/javascript\"><!--\n"; | ||||
228 | print_some_javascript_code(); | ||||
229 | print "//--></script>\n"; | ||||
230 | warningsToBrowser(1); # re-enable warnings | ||||
231 | |||||
232 | Note: In this respect warningsToBrowser() differs fundamentally from | ||||
233 | fatalsToBrowser(), which you should never call yourself! | ||||
234 | |||||
235 | =head1 OVERRIDING THE NAME OF THE PROGRAM | ||||
236 | |||||
237 | CGI::Carp includes the name of the program that generated the error or | ||||
238 | warning in the messages written to the log and the browser window. | ||||
239 | Sometimes, Perl can get confused about what the actual name of the | ||||
240 | executed program was. In these cases, you can override the program | ||||
241 | name that CGI::Carp will use for all messages. | ||||
242 | |||||
243 | The quick way to do that is to tell CGI::Carp the name of the program | ||||
244 | in its use statement. You can do that by adding | ||||
245 | "name=cgi_carp_log_name" to your "use" statement. For example: | ||||
246 | |||||
247 | use CGI::Carp qw(name=cgi_carp_log_name); | ||||
248 | |||||
249 | . If you want to change the program name partway through the program, | ||||
250 | you can use the C<set_progname()> function instead. It is not | ||||
251 | exported by default, you must import it explicitly by saying | ||||
252 | |||||
253 | use CGI::Carp qw(set_progname); | ||||
254 | |||||
255 | Once you've done that, you can change the logged name of the program | ||||
256 | at any time by calling | ||||
257 | |||||
258 | set_progname(new_program_name); | ||||
259 | |||||
260 | You can set the program back to the default by calling | ||||
261 | |||||
262 | set_progname(undef); | ||||
263 | |||||
264 | Note that this override doesn't happen until after the program has | ||||
265 | compiled, so any compile-time errors will still show up with the | ||||
266 | non-overridden program name | ||||
267 | |||||
268 | =head1 CHANGE LOG | ||||
269 | |||||
270 | 3.51 Added $CGI::Carp::TO_BROWSER | ||||
271 | |||||
272 | 1.29 Patch from Peter Whaite to fix the unfixable problem of CGI::Carp | ||||
273 | not behaving correctly in an eval() context. | ||||
274 | |||||
275 | 1.05 carpout() added and minor corrections by Marc Hedlund | ||||
276 | <hedlund@best.com> on 11/26/95. | ||||
277 | |||||
278 | 1.06 fatalsToBrowser() no longer aborts for fatal errors within | ||||
279 | eval() statements. | ||||
280 | |||||
281 | 1.08 set_message() added and carpout() expanded to allow for FileHandle | ||||
282 | objects. | ||||
283 | |||||
284 | 1.09 set_message() now allows users to pass a code REFERENCE for | ||||
285 | really custom error messages. croak and carp are now | ||||
286 | exported by default. Thanks to Gunther Birznieks for the | ||||
287 | patches. | ||||
288 | |||||
289 | 1.10 Patch from Chris Dean (ctdean@cogit.com) to allow | ||||
290 | module to run correctly under mod_perl. | ||||
291 | |||||
292 | 1.11 Changed order of > and < escapes. | ||||
293 | |||||
294 | 1.12 Changed die() on line 217 to CORE::die to avoid B<-w> warning. | ||||
295 | |||||
296 | 1.13 Added cluck() to make the module orthogonal with Carp. | ||||
297 | More mod_perl related fixes. | ||||
298 | |||||
299 | 1.20 Patch from Ilmari Karonen (perl@itz.pp.sci.fi): Added | ||||
300 | warningsToBrowser(). Replaced <CODE> tags with <PRE> in | ||||
301 | fatalsToBrowser() output. | ||||
302 | |||||
303 | 1.23 ineval() now checks both $^S and inspects the message for the "eval" pattern | ||||
304 | (hack alert!) in order to accommodate various combinations of Perl and | ||||
305 | mod_perl. | ||||
306 | |||||
307 | 1.24 Patch from Scott Gifford (sgifford@suspectclass.com): Add support | ||||
308 | for overriding program name. | ||||
309 | |||||
310 | 1.26 Replaced CORE::GLOBAL::die with the evil $SIG{__DIE__} because the | ||||
311 | former isn't working in some people's hands. There is no such thing | ||||
312 | as reliable exception handling in Perl. | ||||
313 | |||||
314 | 1.27 Replaced tell STDOUT with bytes=tell STDOUT. | ||||
315 | |||||
316 | =head1 AUTHORS | ||||
317 | |||||
318 | Copyright 1995-2002, Lincoln D. Stein. All rights reserved. | ||||
319 | |||||
320 | This library is free software; you can redistribute it and/or modify | ||||
321 | it under the same terms as Perl itself. | ||||
322 | |||||
323 | Address bug reports and comments to: lstein@cshl.org | ||||
324 | |||||
325 | =head1 SEE ALSO | ||||
326 | |||||
327 | Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form, | ||||
328 | CGI::Response | ||||
329 | |||||
330 | =cut | ||||
331 | |||||
332 | 1 | 18µs | require 5.000; | ||
333 | 2 | 1.07ms | 2 | 1.20ms | # spent 1.13ms (1.06+66µs) within CGI::Carp::BEGIN@333 which was called:
# once (1.06ms+66µs) by main::BEGIN@11 at line 333 # spent 1.13ms making 1 call to CGI::Carp::BEGIN@333
# spent 66µs making 1 call to Exporter::import |
334 | #use Carp; | ||||
335 | # spent 3.37ms (3.14+230µs) within CGI::Carp::BEGIN@335 which was called:
# once (3.14ms+230µs) by main::BEGIN@11 at line 338 | ||||
336 | 2 | 136µs | require Carp; | ||
337 | *CORE::GLOBAL::die = \&CGI::Carp::die; | ||||
338 | 1 | 32µs | 1 | 3.37ms | } # spent 3.37ms making 1 call to CGI::Carp::BEGIN@335 |
339 | |||||
340 | 2 | 2.64ms | 1 | 10µs | # spent 10µs within CGI::Carp::BEGIN@340 which was called:
# once (10µs+0s) by main::BEGIN@11 at line 340 # spent 10µs making 1 call to CGI::Carp::BEGIN@340 |
341 | |||||
342 | 1 | 8µs | @ISA = qw(Exporter); | ||
343 | 1 | 2µs | @EXPORT = qw(confess croak carp); | ||
344 | 1 | 3µs | @EXPORT_OK = qw(carpout fatalsToBrowser warningsToBrowser wrap set_message set_die_handler set_progname cluck ^name= die); | ||
345 | |||||
346 | 1 | 4µs | $main::SIG{__WARN__}=\&CGI::Carp::warn; | ||
347 | |||||
348 | 1 | 2µs | $CGI::Carp::VERSION = '3.51'; | ||
349 | 1 | 1µs | $CGI::Carp::CUSTOM_MSG = undef; | ||
350 | 1 | 1µs | $CGI::Carp::DIE_HANDLER = undef; | ||
351 | 1 | 1µs | $CGI::Carp::TO_BROWSER = 1; | ||
352 | |||||
353 | |||||
354 | # fancy import routine detects and handles 'errorWrap' specially. | ||||
355 | # spent 258µs (55+204) within CGI::Carp::import which was called:
# once (55µs+204µs) by main::BEGIN@11 at line 11 of view | ||||
356 | 12 | 57µs | my $pkg = shift; | ||
357 | my(%routines); | ||||
358 | my(@name); | ||||
359 | 1 | 3µs | if (@name=grep(/^name=/,@_)) # spent 3µs making 1 call to CGI::Carp::CORE:match | ||
360 | { | ||||
361 | my($n) = (split(/=/,$name[0]))[1]; | ||||
362 | set_progname($n); | ||||
363 | @_=grep(!/^name=/,@_); | ||||
364 | } | ||||
365 | |||||
366 | grep($routines{$_}++,@_,@EXPORT); | ||||
367 | $WRAP++ if $routines{'fatalsToBrowser'} || $routines{'wrap'}; | ||||
368 | $WARN++ if $routines{'warningsToBrowser'}; | ||||
369 | my($oldlevel) = $Exporter::ExportLevel; | ||||
370 | $Exporter::ExportLevel = 1; | ||||
371 | 1 | 201µs | Exporter::import($pkg,keys %routines); # spent 201µs making 1 call to Exporter::import | ||
372 | $Exporter::ExportLevel = $oldlevel; | ||||
373 | $main::SIG{__DIE__} =\&CGI::Carp::die if $routines{'fatalsToBrowser'}; | ||||
374 | # $pkg->export('CORE::GLOBAL','die'); | ||||
375 | } | ||||
376 | |||||
377 | # These are the originals | ||||
378 | sub realwarn { CORE::warn(@_); } | ||||
379 | sub realdie { CORE::die(@_); } | ||||
380 | |||||
381 | sub id { | ||||
382 | my $level = shift; | ||||
383 | my($pack,$file,$line,$sub) = caller($level); | ||||
384 | my($dev,$dirs,$id) = File::Spec->splitpath($file); | ||||
385 | return ($file,$line,$id); | ||||
386 | } | ||||
387 | |||||
388 | sub stamp { | ||||
389 | my $time = scalar(localtime); | ||||
390 | my $frame = 0; | ||||
391 | my ($id,$pack,$file,$dev,$dirs); | ||||
392 | if (defined($CGI::Carp::PROGNAME)) { | ||||
393 | $id = $CGI::Carp::PROGNAME; | ||||
394 | } else { | ||||
395 | do { | ||||
396 | $id = $file; | ||||
397 | ($pack,$file) = caller($frame++); | ||||
398 | } until !$file; | ||||
399 | } | ||||
400 | ($dev,$dirs,$id) = File::Spec->splitpath($id); | ||||
401 | return "[$time] $id: "; | ||||
402 | } | ||||
403 | |||||
404 | sub set_progname { | ||||
405 | $CGI::Carp::PROGNAME = shift; | ||||
406 | return $CGI::Carp::PROGNAME; | ||||
407 | } | ||||
408 | |||||
409 | |||||
410 | sub warn { | ||||
411 | my $message = shift; | ||||
412 | my($file,$line,$id) = id(1); | ||||
413 | $message .= " at $file line $line.\n" unless $message=~/\n$/; | ||||
414 | _warn($message) if $WARN; | ||||
415 | my $stamp = stamp; | ||||
416 | $message=~s/^/$stamp/gm; | ||||
417 | realwarn $message; | ||||
418 | } | ||||
419 | |||||
420 | sub _warn { | ||||
421 | my $msg = shift; | ||||
422 | if ($EMIT_WARNINGS) { | ||||
423 | # We need to mangle the message a bit to make it a valid HTML | ||||
424 | # comment. This is done by substituting similar-looking ISO | ||||
425 | # 8859-1 characters for <, > and -. This is a hack. | ||||
426 | $msg =~ tr/<>-/\253\273\255/; | ||||
427 | chomp $msg; | ||||
428 | print STDOUT "<!-- warning: $msg -->\n"; | ||||
429 | } else { | ||||
430 | push @WARNINGS, $msg; | ||||
431 | } | ||||
432 | } | ||||
433 | |||||
434 | |||||
435 | # The mod_perl package Apache::Registry loads CGI programs by calling | ||||
436 | # eval. These evals don't count when looking at the stack backtrace. | ||||
437 | sub _longmess { | ||||
438 | my $message = Carp::longmess(); | ||||
439 | $message =~ s,eval[^\n]+(ModPerl|Apache)/(?:Registry|Dispatch)\w*\.pm.*,,s | ||||
440 | if exists $ENV{MOD_PERL}; | ||||
441 | return $message; | ||||
442 | } | ||||
443 | |||||
444 | sub ineval { | ||||
445 | (exists $ENV{MOD_PERL} ? 0 : $^S) || _longmess() =~ /eval [\{\']/m | ||||
446 | } | ||||
447 | |||||
448 | sub die { | ||||
449 | # if no argument is passed, propagate $@ like | ||||
450 | # the real die | ||||
451 | my ($arg,@rest) = @_ ? @_ | ||||
452 | : $@ ? "$@\t...propagated" | ||||
453 | : "Died" | ||||
454 | ; | ||||
455 | |||||
456 | &$DIE_HANDLER($arg,@rest) if $DIE_HANDLER; | ||||
457 | |||||
458 | # the "$arg" is done on purpose! | ||||
459 | # if called as die( $object, 'string' ), | ||||
460 | # all is stringified, just like with | ||||
461 | # the real 'die' | ||||
462 | $arg = join '' => "$arg", @rest if @rest; | ||||
463 | |||||
464 | my($file,$line,$id) = id(1); | ||||
465 | |||||
466 | $arg .= " at $file line $line.\n" unless ref $arg or $arg=~/\n$/; | ||||
467 | |||||
468 | realdie $arg if ineval(); | ||||
469 | &fatalsToBrowser($arg) if ($WRAP and $CGI::Carp::TO_BROWSER); | ||||
470 | |||||
471 | $arg=~s/^/ stamp() /gme if $arg =~ /\n$/ or not exists $ENV{MOD_PERL}; | ||||
472 | |||||
473 | $arg .= "\n" unless $arg =~ /\n$/; | ||||
474 | |||||
475 | realdie $arg; | ||||
476 | } | ||||
477 | |||||
478 | sub set_message { | ||||
479 | $CGI::Carp::CUSTOM_MSG = shift; | ||||
480 | return $CGI::Carp::CUSTOM_MSG; | ||||
481 | } | ||||
482 | |||||
483 | sub set_die_handler { | ||||
484 | |||||
485 | my ($handler) = shift; | ||||
486 | |||||
487 | #setting SIG{__DIE__} here is necessary to catch runtime | ||||
488 | #errors which are not called by literally saying "die", | ||||
489 | #such as the line "undef->explode();". however, doing this | ||||
490 | #will interfere with fatalsToBrowser, which also sets | ||||
491 | #SIG{__DIE__} in the import() function above (or the | ||||
492 | #import() function above may interfere with this). for | ||||
493 | #this reason, you should choose to either set the die | ||||
494 | #handler here, or use fatalsToBrowser, not both. | ||||
495 | $main::SIG{__DIE__} = $handler; | ||||
496 | |||||
497 | $CGI::Carp::DIE_HANDLER = $handler; | ||||
498 | |||||
499 | return $CGI::Carp::DIE_HANDLER; | ||||
500 | } | ||||
501 | |||||
502 | sub confess { CGI::Carp::die Carp::longmess @_; } | ||||
503 | sub croak { CGI::Carp::die Carp::shortmess @_; } | ||||
504 | sub carp { CGI::Carp::warn Carp::shortmess @_; } | ||||
505 | sub cluck { CGI::Carp::warn Carp::longmess @_; } | ||||
506 | |||||
507 | # We have to be ready to accept a filehandle as a reference | ||||
508 | # or a string. | ||||
509 | sub carpout { | ||||
510 | my($in) = @_; | ||||
511 | my($no) = fileno(to_filehandle($in)); | ||||
512 | realdie("Invalid filehandle $in\n") unless defined $no; | ||||
513 | |||||
514 | open(SAVEERR, ">&STDERR"); | ||||
515 | open(STDERR, ">&$no") or | ||||
516 | ( print SAVEERR "Unable to redirect STDERR: $!\n" and exit(1) ); | ||||
517 | } | ||||
518 | |||||
519 | sub warningsToBrowser { | ||||
520 | $EMIT_WARNINGS = @_ ? shift : 1; | ||||
521 | _warn(shift @WARNINGS) while $EMIT_WARNINGS and @WARNINGS; | ||||
522 | } | ||||
523 | |||||
524 | # headers | ||||
525 | sub fatalsToBrowser { | ||||
526 | my $msg = shift; | ||||
527 | |||||
528 | $msg = "$msg" if ref $msg; | ||||
529 | |||||
530 | $msg=~s/&/&/g; | ||||
531 | $msg=~s/>/>/g; | ||||
532 | $msg=~s/</</g; | ||||
533 | $msg=~s/"/"/g; | ||||
534 | |||||
535 | my($wm) = $ENV{SERVER_ADMIN} ? | ||||
536 | qq[the webmaster (<a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>)] : | ||||
537 | "this site's webmaster"; | ||||
538 | my ($outer_message) = <<END; | ||||
539 | For help, please send mail to $wm, giving this error message | ||||
540 | and the time and date of the error. | ||||
541 | END | ||||
542 | ; | ||||
543 | my $mod_perl = exists $ENV{MOD_PERL}; | ||||
544 | |||||
545 | if ($CUSTOM_MSG) { | ||||
546 | if (ref($CUSTOM_MSG) eq 'CODE') { | ||||
547 | print STDOUT "Content-type: text/html\n\n" | ||||
548 | unless $mod_perl; | ||||
549 | eval { | ||||
550 | &$CUSTOM_MSG($msg); # nicer to perl 5.003 users | ||||
551 | }; | ||||
552 | if ($@) { print STDERR q(error while executing the error handler: $@); } | ||||
553 | |||||
554 | return; | ||||
555 | } else { | ||||
556 | $outer_message = $CUSTOM_MSG; | ||||
557 | } | ||||
558 | } | ||||
559 | |||||
560 | my $mess = <<END; | ||||
561 | <h1>Software error:</h1> | ||||
562 | <pre>$msg</pre> | ||||
563 | <p> | ||||
564 | $outer_message | ||||
565 | </p> | ||||
566 | END | ||||
567 | ; | ||||
568 | |||||
569 | if ($mod_perl) { | ||||
570 | my $r; | ||||
571 | if ($ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2) { | ||||
572 | $mod_perl = 2; | ||||
573 | require Apache2::RequestRec; | ||||
574 | require Apache2::RequestIO; | ||||
575 | require Apache2::RequestUtil; | ||||
576 | require APR::Pool; | ||||
577 | require ModPerl::Util; | ||||
578 | require Apache2::Response; | ||||
579 | $r = Apache2::RequestUtil->request; | ||||
580 | } | ||||
581 | else { | ||||
582 | $r = Apache->request; | ||||
583 | } | ||||
584 | # If bytes have already been sent, then | ||||
585 | # we print the message out directly. | ||||
586 | # Otherwise we make a custom error | ||||
587 | # handler to produce the doc for us. | ||||
588 | if ($r->bytes_sent) { | ||||
589 | $r->print($mess); | ||||
590 | $mod_perl == 2 ? ModPerl::Util::exit(0) : $r->exit; | ||||
591 | } else { | ||||
592 | # MSIE won't display a custom 500 response unless it is >512 bytes! | ||||
593 | if ($ENV{HTTP_USER_AGENT} =~ /MSIE/) { | ||||
594 | $mess = "<!-- " . (' ' x 513) . " -->\n$mess"; | ||||
595 | } | ||||
596 | $r->custom_response(500,$mess); | ||||
597 | } | ||||
598 | } else { | ||||
599 | my $bytes_written = eval{tell STDOUT}; | ||||
600 | if (defined $bytes_written && $bytes_written > 0) { | ||||
601 | print STDOUT $mess; | ||||
602 | } | ||||
603 | else { | ||||
604 | print STDOUT "Status: 500\n"; | ||||
605 | print STDOUT "Content-type: text/html\n\n"; | ||||
606 | print STDOUT $mess; | ||||
607 | } | ||||
608 | } | ||||
609 | |||||
610 | warningsToBrowser(1); # emit warnings before dying | ||||
611 | } | ||||
612 | |||||
613 | # Cut and paste from CGI.pm so that we don't have the overhead of | ||||
614 | # always loading the entire CGI module. | ||||
615 | sub to_filehandle { | ||||
616 | my $thingy = shift; | ||||
617 | return undef unless $thingy; | ||||
618 | return $thingy if UNIVERSAL::isa($thingy,'GLOB'); | ||||
619 | return $thingy if UNIVERSAL::isa($thingy,'FileHandle'); | ||||
620 | if (!ref($thingy)) { | ||||
621 | my $caller = 1; | ||||
622 | while (my $package = caller($caller++)) { | ||||
623 | my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy"; | ||||
624 | return $tmp if defined(fileno($tmp)); | ||||
625 | } | ||||
626 | } | ||||
627 | return undef; | ||||
628 | } | ||||
629 | |||||
630 | 1 | 13µs | 1; | ||
# spent 3µs within CGI::Carp::CORE:match which was called:
# once (3µs+0s) by CGI::Carp::import at line 359 |