{
    "mode": "perldoc",
    "parameter": "CGI::Carp",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/CGI%3A%3ACarp/json",
    "generated": "2026-06-13T22:59:09Z",
    "synopsis": "use CGI::Carp;\ncroak \"We're outta here!\";\nconfess \"It was my fault: $!\";\ncarp \"It was your fault!\";\nwarn \"I'm confused\";\ndie  \"I'm dying.\\n\";\nuse CGI::Carp qw(cluck);\ncluck \"I wouldn't do that if I were you\";\nuse CGI::Carp qw(fatalsToBrowser);\ndie \"Fatal error messages are now sent to browser\";",
    "sections": {
        "NAME": {
            "content": "CGI::Carp - CGI routines for writing to the HTTPD (or other) error log\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use CGI::Carp;\n\ncroak \"We're outta here!\";\nconfess \"It was my fault: $!\";\ncarp \"It was your fault!\";\nwarn \"I'm confused\";\ndie  \"I'm dying.\\n\";\n\nuse CGI::Carp qw(cluck);\ncluck \"I wouldn't do that if I were you\";\n\nuse CGI::Carp qw(fatalsToBrowser);\ndie \"Fatal error messages are now sent to browser\";\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "CGI scripts have a nasty habit of leaving warning messages in the error logs that are neither\ntime stamped nor fully identified. Tracking down the script that caused the error is a pain.\nThis fixes that. Replace the usual\n\nuse Carp;\n\nwith\n\nuse CGI::Carp\n\nThe standard warn(), die (), croak(), confess() and carp() calls will be replaced with functions\nthat write time-stamped messages to the HTTP server error log.\n\nFor example:\n\n[Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3.\n[Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.\n[Fri Nov 17 21:40:43 1995] test.pl: I'm dying.\n",
            "subsections": []
        },
        "REDIRECTING ERROR MESSAGES": {
            "content": "By default, error messages are sent to STDERR. Most HTTPD servers direct STDERR to the server's\nerror log. Some applications may wish to keep private error logs, distinct from the server's\nerror log, or they may wish to direct error messages to STDOUT so that the browser will receive\nthem.\n\nThe \"carpout()\" function is provided for this purpose. Since carpout() is not exported by\ndefault, you must import it explicitly by saying\n\nuse CGI::Carp qw(carpout);\n\nThe carpout() function requires one argument, a reference to an open filehandle for writing\nerrors. It should be called in a \"BEGIN\" block at the top of the CGI application so that\ncompiler errors will be caught. Example:\n\nBEGIN {\nuse CGI::Carp qw(carpout);\nopen(LOG, \">>/usr/local/cgi-logs/mycgi-log\") or\ndie(\"Unable to open mycgi-log: $!\\n\");\ncarpout(LOG);\n}\n",
            "subsections": [
                {
                    "name": "carpout",
                    "content": ""
                },
                {
                    "name": "carpout",
                    "content": "address that.\n\nThe real STDERR is not closed -- it is moved to CGI::Carp::SAVEERR. Some servers, when dealing\nwith CGI scripts, close their connection to the browser when the script closes STDOUT and\nSTDERR. CGI::Carp::SAVEERR is there to prevent this from happening prematurely.\n\nYou can pass filehandles to carpout() in a variety of ways. The \"correct\" way according to Tom\nChristiansen is to pass a reference to a filehandle GLOB:\n\ncarpout(\\*LOG);\n\nThis looks weird to mere mortals however, so the following syntaxes are accepted as well:\n\ncarpout(LOG);\ncarpout(main::LOG);\ncarpout(main'LOG);\ncarpout(\\LOG);\ncarpout(\\'main::LOG');\n\n... and so on\n\nFileHandle and other objects work as well.\n\nUse of carpout() is not great for performance, so it is recommended for debugging purposes or\nfor moderate-use applications. A future version of this module may delay redirecting STDERR\nuntil one of the CGI::Carp methods is called to prevent the performance hit.\n"
                }
            ]
        },
        "MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW": {
            "content": "If you want to send fatal (die, confess) errors to the browser, import the special\n\"fatalsToBrowser\" subroutine:\n\nuse CGI::Carp qw(fatalsToBrowser);\ndie \"Bad error here\";\n\nFatal errors will now be echoed to the browser as well as to the log. CGI::Carp arranges to send\na minimal HTTP header to the browser so that even errors that occur in the early compile phase\nwill be seen. Nonfatal errors will still be directed to the log file only (unless redirected\nwith carpout).\n\nNote that fatalsToBrowser may not work well with modperl version 2.0 and higher.\n",
            "subsections": [
                {
                    "name": "Changing the default message",
                    "content": "By default, the software error message is followed by a note to contact the Webmaster by e-mail\nwith the time and date of the error. If this message is not to your liking, you can change it\nusing the setmessage() routine. This is not imported by default; you should import it on the"
                },
                {
                    "name": "use",
                    "content": "use CGI::Carp qw(fatalsToBrowser setmessage);\nsetmessage(\"It's not a bug, it's a feature!\");\n\nYou may also pass in a code reference in order to create a custom error message. At run time,\nyour code will be called with the text of the error message that caused the script to die.\nExample:\n\nuse CGI::Carp qw(fatalsToBrowser setmessage);\nBEGIN {\nsub handleerrors {\nmy $msg = shift;\nprint \"<h1>Oh gosh</h1>\";\nprint \"<p>Got an error: $msg</p>\";\n}\nsetmessage(\\&handleerrors);\n}\n\nIn order to correctly intercept compile-time errors, you should call setmessage() from within a\nBEGIN{} block.\n\nDOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS\nIf fatalsToBrowser in conjunction with setmessage does not provide you with all of the\nfunctionality you need, you can go one step further by specifying a function to be executed any\ntime a script calls \"die\", has a syntax error, or dies unexpectedly at runtime with a line like\n\"undef->explode();\".\n\nuse CGI::Carp qw(setdiehandler);\nBEGIN {\nsub handleerrors {\nmy $msg = shift;\nprint \"content-type: text/html\\n\\n\";\nprint \"<h1>Oh gosh</h1>\";\nprint \"<p>Got an error: $msg</p>\";\n\n#proceed to send an email to a system administrator,\n#write a detailed message to the browser and/or a log,\n#etc....\n}\nsetdiehandler(\\&handleerrors);\n}\n\nNotice that if you use setdiehandler(), you must handle sending HTML headers to the browser\nyourself if you are printing a message.\n\nIf you use setdiehandler(), you will most likely interfere with the behavior of\nfatalsToBrowser, so you must use this or that, not both.\n\nUsing setdiehandler() sets SIG{DIE} (as does fatalsToBrowser), and there is only one\nSIG{DIE}. This means that if you are attempting to set SIG{DIE} yourself, you may\ninterfere with this module's functionality, or this module may interfere with your module's\nfunctionality.\n\nSUPPRESSING PERL ERRORS APPEARING IN THE BROWSER WINDOW\nA problem sometimes encountered when using fatalsToBrowser is when a \"die()\" is done inside an\n\"eval\" body or expression. Even though the fatalsToBrower support takes precautions to avoid\nthis, you still may get the error message printed to STDOUT. This may have some undesirable\neffects when the purpose of doing the eval is to determine which of several algorithms is to be\nused.\n\nBy setting $CGI::Carp::TOBROWSER to 0 you can suppress printing the \"die\" messages but without\nall of the complexity of using \"setdiehandler\". You can localize this effect to inside \"eval\"\nbodies if this is desirable: For example:\n\neval {\nlocal $CGI::Carp::TOBROWSER = 0;\ndie \"Fatal error messages not sent browser\"\n}\n# $@ will contain error message\n"
                }
            ]
        },
        "MAKING WARNINGS APPEAR AS HTML COMMENTS": {
            "content": "It is also possible to make non-fatal errors appear as HTML comments embedded in the output of\nyour program. To enable this feature, export the new \"warningsToBrowser\" subroutine. Since\nsending warnings to the browser before the HTTP headers have been sent would cause an error, any\nwarnings are stored in an internal buffer until you call the warningsToBrowser() subroutine with\na true argument:\n\nuse CGI::Carp qw(fatalsToBrowser warningsToBrowser);\nuse CGI qw(:standard);\nprint header();\nwarningsToBrowser(1);\n\nYou may also give a false argument to warningsToBrowser() to prevent warnings from being sent to\nthe browser while you are printing some content where HTML comments are not allowed:\n\nwarningsToBrowser(0);    # disable warnings\nprint \"<script type=\\\"text/javascript\\\"><!--\\n\";\nprintsomejavascriptcode();\nprint \"//--></script>\\n\";\nwarningsToBrowser(1);    # re-enable warnings\n\nNote: In this respect warningsToBrowser() differs fundamentally from fatalsToBrowser(), which\nyou should never call yourself!\n",
            "subsections": []
        },
        "OVERRIDING THE NAME OF THE PROGRAM": {
            "content": "CGI::Carp includes the name of the program that generated the error or warning in the messages\nwritten to the log and the browser window. Sometimes, Perl can get confused about what the\nactual name of the executed program was. In these cases, you can override the program name that\nCGI::Carp will use for all messages.\n\nThe quick way to do that is to tell CGI::Carp the name of the program in its use statement. You\ncan do that by adding \"name=cgicarplogname\" to your \"use\" statement. For example:\n\nuse CGI::Carp qw(name=cgicarplogname);\n\n. If you want to change the program name partway through the program, you can use the\n\"setprogname()\" function instead. It is not exported by default, you must import it explicitly\nby saying\n\nuse CGI::Carp qw(setprogname);\n\nOnce you've done that, you can change the logged name of the program at any time by calling\n\nsetprogname(newprogramname);\n\nYou can set the program back to the default by calling\n\nsetprogname(undef);\n\nNote that this override doesn't happen until after the program has compiled, so any compile-time\nerrors will still show up with the non-overridden program name\n",
            "subsections": []
        },
        "TURNING OFF TIMESTAMPS IN MESSAGES": {
            "content": "If your web server automatically adds a timestamp to each log line, you may not need CGI::Carp\nto add its own. You can disable timestamping by importing \"noTimestamp\":\n\nuse CGI::Carp qw(noTimestamp);\n\nAlternatively you can set $CGI::Carp::NOTIMESTAMP to 1.\n\nNote that the name of the program is still automatically included in the message.\n",
            "subsections": []
        },
        "GETTING THE FULL PATH OF THE SCRIPT IN MESSAGES": {
            "content": "Set $CGI::Carp::FULLPATH to 1.\n",
            "subsections": []
        },
        "AUTHOR INFORMATION": {
            "content": "The CGI.pm distribution is copyright 1995-2007, Lincoln D. Stein. It is distributed under the\nArtistic License 2.0. It is currently maintained by Lee Johnson with help from many\ncontributors.\n\nAddress bug reports and comments to: https://github.com/leejo/CGI.pm/issues\n\nThe original bug tracker can be found at:\nhttps://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm\n\nWhen sending bug reports, please provide the version of CGI.pm, the version of Perl, the name\nand version of your Web server, and the name and version of the operating system you are using.\nIf the problem is even remotely browser dependent, please provide information about the affected\nbrowsers as well.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form, CGI::Response.\n",
            "subsections": []
        }
    },
    "summary": "CGI::Carp - CGI routines for writing to the HTTPD (or other) error log",
    "flags": [],
    "examples": [],
    "see_also": []
}