{
    "mode": "perldoc",
    "parameter": "Carp",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Carp/json",
    "generated": "2026-06-13T21:20:53Z",
    "synopsis": "use Carp;\n# warn user (from perspective of caller)\ncarp \"string trimmed to 80 chars\";\n# die of errors (from perspective of caller)\ncroak \"We're outta here!\";\n# die of errors with stack backtrace\nconfess \"not implemented\";\n# cluck, longmess and shortmess not exported by default\nuse Carp qw(cluck longmess shortmess);\ncluck \"This is how we got here!\"; # warn with stack backtrace\n$longmessage   = longmess( \"message from cluck() or confess()\" );\n$shortmessage  = shortmess( \"message from carp() or croak()\" );",
    "sections": {
        "NAME": {
            "content": "Carp - alternative warn and die for modules\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Carp;\n\n# warn user (from perspective of caller)\ncarp \"string trimmed to 80 chars\";\n\n# die of errors (from perspective of caller)\ncroak \"We're outta here!\";\n\n# die of errors with stack backtrace\nconfess \"not implemented\";\n\n# cluck, longmess and shortmess not exported by default\nuse Carp qw(cluck longmess shortmess);\ncluck \"This is how we got here!\"; # warn with stack backtrace\n$longmessage   = longmess( \"message from cluck() or confess()\" );\n$shortmessage  = shortmess( \"message from carp() or croak()\" );\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The Carp routines are useful in your own modules because they act like \"die()\" or \"warn()\", but\nwith a message which is more likely to be useful to a user of your module. In the case of\n\"cluck()\" and \"confess()\", that context is a summary of every call in the call-stack;\n\"longmess()\" returns the contents of the error message.\n\nFor a shorter message you can use \"carp()\" or \"croak()\" which report the error as being from\nwhere your module was called. \"shortmess()\" returns the contents of this error message. There is\nno guarantee that that is where the error was, but it is a good educated guess.\n\n\"Carp\" takes care not to clobber the status variables $! and $^E in the course of assembling its\nerror messages. This means that a $SIG{DIE} or $SIG{WARN} handler can capture the error\ninformation held in those variables, if it is required to augment the error message, and if the\ncode calling \"Carp\" left useful values there. Of course, \"Carp\" can't guarantee the latter.\n\nYou can also alter the way the output and logic of \"Carp\" works, by changing some global\nvariables in the \"Carp\" namespace. See the section on \"GLOBAL VARIABLES\" below.\n\nHere is a more complete description of how \"carp\" and \"croak\" work. What they do is search the\ncall-stack for a function call stack where they have not been told that there shouldn't be an\nerror. If every call is marked safe, they give up and give a full stack backtrace instead. In\nother words they presume that the first likely looking potential suspect is guilty. Their rules\nfor telling whether a call shouldn't generate errors work as follows:\n\n1.  Any call from a package to itself is safe.\n\n2.  Packages claim that there won't be errors on calls to or from packages explicitly marked as\nsafe by inclusion in @CARPNOT, or (if that array is empty) @ISA. The ability to override\nwhat @ISA says is new in 5.8.\n\n3.  The trust in item 2 is transitive. If A trusts B, and B trusts C, then A trusts C. So if you\ndo not override @ISA with @CARPNOT, then this trust relationship is identical to, \"inherits\nfrom\".\n\n4.  Any call from an internal Perl module is safe. (Nothing keeps user modules from marking\nthemselves as internal to Perl, but this practice is discouraged.)\n\n5.  Any call to Perl's warning system (eg Carp itself) is safe. (This rule is what keeps it from\nreporting the error at the point where you call \"carp\" or \"croak\".)\n\n6.  $Carp::CarpLevel can be set to skip a fixed number of additional call levels. Using this is\nnot recommended because it is very difficult to get it to behave correctly.\n",
            "subsections": [
                {
                    "name": "Forcing a Stack Trace",
                    "content": "As a debugging aid, you can force Carp to treat a croak as a confess and a carp as a cluck\nacross *all* modules. In other words, force a detailed stack trace to be given. This can be very\nhelpful when trying to understand why, or from where, a warning or error is being generated.\n\nThis feature is enabled by 'importing' the non-existent symbol 'verbose'. You would typically\nenable it by saying\n\nperl -MCarp=verbose script.pl\n\nor by including the string \"-MCarp=verbose\" in the PERL5OPT environment variable.\n\nAlternately, you can set the global variable $Carp::Verbose to true. See the \"GLOBAL VARIABLES\"\nsection below.\n"
                },
                {
                    "name": "Stack Trace formatting",
                    "content": "At each stack level, the subroutine's name is displayed along with its parameters. For simple\nscalars, this is sufficient. For complex data types, such as objects and other references, this\ncan simply display 'HASH(0x1ab36d8)'.\n\nCarp gives two ways to control this.\n\n1.  For objects, a method, \"CARPTRACE\", will be called, if it exists. If this method doesn't\nexist, or it recurses into \"Carp\", or it otherwise throws an exception, this is skipped, and\nCarp moves on to the next option, otherwise checking stops and the string returned is used.\nIt is recommended that the object's type is part of the string to make debugging easier.\n\n2.  For any type of reference, $Carp::RefArgFormatter is checked (see below). This variable is\nexpected to be a code reference, and the current parameter is passed in. If this function\ndoesn't exist (the variable is undef), or it recurses into \"Carp\", or it otherwise throws an\nexception, this is skipped, and Carp moves on to the next option, otherwise checking stops\nand the string returned is used.\n\n3.  Otherwise, if neither \"CARPTRACE\" nor $Carp::RefArgFormatter is available, stringify the\nvalue ignoring any overloading.\n"
                }
            ]
        },
        "GLOBAL VARIABLES": {
            "content": "$Carp::MaxEvalLen\nThis variable determines how many characters of a string-eval are to be shown in the output. Use\na value of 0 to show all text.\n\nDefaults to 0.\n\n$Carp::MaxArgLen\nThis variable determines how many characters of each argument to a function to print. Use a\nvalue of 0 to show the full length of the argument.\n\nDefaults to 64.\n\n$Carp::MaxArgNums\nThis variable determines how many arguments to each function to show. Use a false value to show\nall arguments to a function call. To suppress all arguments, use -1 or '0 but true'.\n\nDefaults to 8.\n\n$Carp::Verbose\nThis variable makes \"carp()\" and \"croak()\" generate stack backtraces just like \"cluck()\" and\n\"confess()\". This is how \"use Carp 'verbose'\" is implemented internally.\n\nDefaults to 0.\n\n$Carp::RefArgFormatter\nThis variable sets a general argument formatter to display references. Plain scalars and objects\nthat implement \"CARPTRACE\" will not go through this formatter. Calling \"Carp\" from within this\nfunction is not supported.\n\nlocal $Carp::RefArgFormatter = sub {\nrequire Data::Dumper;\nData::Dumper->Dump($[0]); # not necessarily safe\n};\n\n@CARPNOT\nThis variable, *in your package*, says which packages are *not* to be considered as the location\nof an error. The \"carp()\" and \"cluck()\" functions will skip over callers when reporting where an\nerror occurred.\n\nNB: This variable must be in the package's symbol table, thus:\n\n# These work\nour @CARPNOT; # file scope\nuse vars qw(@CARPNOT); # package scope\n@My::Package::CARPNOT = ... ; # explicit package variable\n\n# These don't work\nsub xyz { ... @CARPNOT = ... } # w/o declarations above\nmy @CARPNOT; # even at top-level\n\nExample of use:\n\npackage My::Carping::Package;\nuse Carp;\nour @CARPNOT;\nsub bar     { .... or error('Wrong input') }\nsub error  {\n# temporary control of where'ness, PACKAGE is implicit\nlocal @CARPNOT = qw(My::Friendly::Caller);\ncarp(@)\n}\n\nThis would make \"Carp\" report the error as coming from a caller not in \"My::Carping::Package\",\nnor from \"My::Friendly::Caller\".\n\nAlso read the \"DESCRIPTION\" section above, about how \"Carp\" decides where the error is reported\nfrom.\n\nUse @CARPNOT, instead of $Carp::CarpLevel.\n\nOverrides \"Carp\"'s use of @ISA.\n\n%Carp::Internal\nThis says what packages are internal to Perl. \"Carp\" will never report an error as being from a\nline in a package that is internal to Perl. For example:\n\n$Carp::Internal{ (PACKAGE) }++;\n# time passes...\nsub foo { ... or confess(\"whatever\") };\n\nwould give a full stack backtrace starting from the first caller outside of PACKAGE. (Unless\nthat package was also internal to Perl.)\n\n%Carp::CarpInternal\nThis says which packages are internal to Perl's warning system. For generating a full stack\nbacktrace this is the same as being internal to Perl, the stack backtrace will not start inside\npackages that are listed in %Carp::CarpInternal. But it is slightly different for the summary\nmessage generated by \"carp\" or \"croak\". There errors will not be reported on any lines that are\ncalling packages in %Carp::CarpInternal.\n\nFor example \"Carp\" itself is listed in %Carp::CarpInternal. Therefore the full stack backtrace\nfrom \"confess\" will not start inside of \"Carp\", and the short message from calling \"croak\" is\nnot placed on the line where \"croak\" was called.\n\n$Carp::CarpLevel\nThis variable determines how many additional call frames are to be skipped that would not\notherwise be when reporting where an error occurred on a call to one of \"Carp\"'s functions. It\nis fairly easy to count these call frames on calls that generate a full stack backtrace. However\nit is much harder to do this accounting for calls that generate a short message. Usually people\nskip too many call frames. If they are lucky they skip enough that \"Carp\" goes all of the way\nthrough the call stack, realizes that something is wrong, and then generates a full stack\nbacktrace. If they are unlucky then the error is reported from somewhere misleading very high in\nthe call stack.\n\nTherefore it is best to avoid $Carp::CarpLevel. Instead use @CARPNOT, %Carp::Internal and\n%Carp::CarpInternal.\n\nDefaults to 0.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "The Carp routines don't handle exception objects currently. If called with a first argument that\nis a reference, they simply call die() or warn(), as appropriate.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Carp::Always, Carp::Clan\n",
            "subsections": []
        },
        "CONTRIBUTING": {
            "content": "Carp is maintained by the perl 5 porters as part of the core perl 5 version control repository.\nPlease see the perlhack perldoc for how to submit patches and contribute to it.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "The Carp module first appeared in Larry Wall's perl 5.000 distribution. Since then it has been\nmodified by several of the perl 5 porters. Andrew Main (Zefram) <zefram@fysh.org> divested Carp\ninto an independent distribution.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1994-2013 Larry Wall\n\nCopyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zefram@fysh.org>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "Carp - alternative warn and die for modules",
    "flags": [],
    "examples": [],
    "see_also": []
}