CINXE.COM

Carp - alternative warn and die for modules - metacpan.org

<!DOCTYPE html> <html lang="en-US"> <head> <title>Carp - alternative warn and die for modules - metacpan.org</title> <link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" /> <link href="/assets/style-XFEQ536G.css" rel="stylesheet" type="text/css"> <script src="/assets/main-WXBSEDQJ.js" type="module"></script> <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Carp - MetaCPAN" href="/dist/Carp/releases.rss" /> <link rel="canonical" href="https://metacpan.org/pod/Carp" /> <meta name="description" content="alternative warn and die for modules" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5"> <link rel="shortcut icon" href="/static/icons/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-touch-icon.png"> <link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN"> <script async src="https://perl-ads.perlhacks.com/perl-ads.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-E82Q2V8LVD"></script> <meta property="og:site_name" content="MetaCPAN" /> <meta name="twitter:site" content="@metacpan" /> <meta property="og:type" content="article" /> <meta name="twitter:card" content="summary" /> <meta property="og:url" content="https://metacpan.org/pod/Carp" /> <meta property="og:title" content="Carp" /> <meta property="og:description" content="alternative warn and die for modules" /> <meta property="og:image" content="https://metacpan.org/static/images/dots.png" /> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="header-logo-large hidden-xs"> <a href="/" tabindex="0"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#logo" /> </svg> </a> </div> <div class="header-logo-icon visible-xs"> <a href="/"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#dots" /> </svg> </a> </div> <ul class="nav navbar-nav menu-items hidden-xs hidden-sm"> <li><a href="/about">About</a></li> <li><a href="/about/sponsors">Sponsor</a></li> <li><a href="https://grep.metacpan.org/">grep::cpan</a></li> <li><a href="/recent">Recent</a></li> <li><a href="/about/faq">FAQ</a></li> <li><a href="/tools">Tools</a></li> <li><a href="https://fastapi.metacpan.org/">API</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <button type="button" class="searchbar-btn visible-xs visible-sm"> <i class="fa fa-search button-fa-icon"></i> </button> <form action="/search" class="searchbar-form visible-md visible-lg search-form form-horizontal"> <input type="hidden" name="size" id="metacpan_search-size" value="20"> <div class="form-group"> <div class="search-group"> <i class="fa fa-search"></i> <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value=""> </div> </div> </form> <li class="icon-slidepanel visible-xs visible-sm"> <button data-toggle="slidepanel" data-target=".slidepanel"> <span class="button-fa-icon"> <i class="fa fa-bars slidepanel-open"></i> <i class="fa fa-times slidepanel-close"></i> </span> </button> </li> <li class="dropdown login-dropdown show-logged-in"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li><a href="/account/identities">Identities</a></li> <li><a href="/account/profile">Profile</a></li> <li><a href="/account/favorite/list">Favorites</a></li> <li> <a href="#" type="button" class="logout-button"> Logout </a> </li> </ul> </li> <li class="dropdown login-dropdown show-logged-out"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon avatar-placeholder" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li> <a href="/login/github"> <i class="fab fa-github fa-fw"></i> GitHub </a> </li> <li> <a href="/login/twitter"> <i class="fab fa-twitter fa-fw"></i> Twitter </a> </li> <li> <a href="/login/google"> <i class="fab fa-google fa-fw"></i> Google </a> </li> </ul> </li> <li class="dropdown login-dropdown hide-logged-in hide-logged-out"> <button> <i class="fa fa-user button-fa-icon" aria-hidden="true"></i> </button> </li> </ul> </nav> <div class="page-content "> <div id="perl-ad-target" class="top-notify-banner perl-ad-target"> </div> <nav class="sidebar"> <div class="slidepanel"> <ul class="nav-list "> <li class="nav-header no-margin-top"> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Carp was released."> <span class="relatize">08 Sep 2018 08:12:02 UTC</span> </div> </li> <li> Distribution: <a href="/dist/Carp">Carp</a> </li> <li> Module version: 1.50 </li> <li> <a data-keyboard-shortcut="g s" href="/dist/Carp/source/lib/Carp.pm">Source</a> (<a href="/dist/Carp/source/lib/Carp.pm?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g b" href="/dist/Carp/source/lib">Browse</a> (<a href="/dist/Carp/source/lib?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g c" href="/dist/Carp/changes">Changes</a> </li> <li> <a class="nopopup" href="/dist/Carp/contribute">How to Contribute</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Carp">Issues</a> (10) </li> <li> <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Carp+1.50" title="Matrix">Testers</a> <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/C/Carp.html?oncpan=1&amp;distmat=1&amp;version=1.50&amp;grade=2" style="color: #090">6828</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/C/Carp.html?oncpan=1&amp;distmat=1&amp;version=1.50&amp;grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/C/Carp.html?oncpan=1&amp;distmat=1&amp;version=1.50&amp;grade=4">0</a>)</span> </li> <li> <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/XSAWYERX/Carp-1.50">Kwalitee</a> </li> <li> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Carp who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)"> Bus factor: 5 </div> </li> <li> <a rel="noopener nofollow" href="http://cpancover.com/latest/Carp-1.50/index.html">% Coverage </a> </li> <li> License: perl_5 </li> <li class="nav-header">Activity</li> <li> <div class="activity-graph"> <img src="/dist/Carp/activity.svg?res=month" /> <div class="comment">24 month</div> </div> </li> <li class="nav-header">Tools</li> <li> <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/X/XS/XSAWYERX/Carp-1.50.tar.gz"> Download (<span itemprop="fileSize">24.31KB</span>)</a> </li> <li> <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FXSAWYERX%2FCarp-1.50%2Flib%2FCarp.pm"> MetaCPAN Explorer </a> </li> <li> <a href="/dist/Carp/permissions"> Permissions </a> </li> <li> <a href="/dist/Carp/releases.rss"> Subscribe to distribution </a> </li> <li> <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog"> Install Instructions </button> </li> <li> <form action="/search"> <input type="hidden" name="q" value="dist:Carp"> <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li> <form action="https://grep.metacpan.org/search"> <input type="hidden" name="qd" value="Carp"> <input type="hidden" name="source" value="metacpan"> <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li class="version-jump"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Jump to version</option> <option disabled value="/release/XSAWYERX/Carp-1.50/view/lib/Carp.pm" >1.50 (XSAWYERX on 2018-09-08)</option> <option value="/release/RJBS/Carp-1.38/view/lib/Carp.pm" >1.38 (RJBS on 2015-11-06)</option> <option value="/release/RJBS/Carp-1.37_02/view/lib/Carp.pm" >1.37_02 DEV (RJBS on 2015-10-30)</option> <option value="/release/RJBS/Carp-1.37_01/view/lib/Carp.pm" >1.37_01 DEV (RJBS on 2015-10-26)</option> <option value="/release/ZEFRAM/Carp-1.3301/view/lib/Carp.pm" >1.3301 (ZEFRAM on 2014-04-01)</option> <option value="/release/ZEFRAM/Carp-1.33/view/lib/Carp.pm" >1.33 (ZEFRAM on 2014-03-24)</option> <option value="/release/ZEFRAM/Carp-1.32/view/lib/Carp.pm" >1.32 (ZEFRAM on 2013-09-03)</option> <option value="/release/ZEFRAM/Carp-1.26/view/lib/Carp.pm" >1.26 (ZEFRAM on 2012-06-18)</option> <option value="/release/ZEFRAM/Carp-1.25/view/lib/Carp.pm" >1.25 (ZEFRAM on 2012-02-16)</option> <option value="/release/ZEFRAM/Carp-1.24/view/lib/Carp.pm" >1.24 (ZEFRAM on 2012-02-02)</option> <option value="/release/ZEFRAM/Carp-1.23/view/lib/Carp.pm" >1.23 (ZEFRAM on 2011-09-09)</option> <option value="/release/ZEFRAM/Carp-1.22/view/lib/Carp.pm" >1.22 (ZEFRAM on 2011-09-04)</option> <optgroup label="BackPAN">' <option value="/release/RJBS/Carp-1.36/view/lib/Carp.pm" >1.36 (RJBS on 2015-03-19)</option> <option value="/release/RJBS/Carp-1.35/view/lib/Carp.pm" >1.35 (RJBS on 2015-03-15)</option> <option value="/release/RJBS/Carp-1.34_02/view/lib/Carp.pm" >1.34_02 DEV (RJBS on 2015-03-06)</option> <option value="/release/RJBS/Carp-1.34_01/view/lib/Carp.pm" >1.34_01 DEV (RJBS on 2015-03-05)</option> </optgroup> </select> </li> <li class="version-diff"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Diff with version</option> <option disabled value="/release/XSAWYERX/Carp-1.50/diff/XSAWYERX/Carp-1.50/lib/Carp.pm" >1.50 (XSAWYERX on 2018-09-08)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.38/lib/Carp.pm" >1.38 (RJBS on 2015-11-06)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.37_02/lib/Carp.pm" >1.37_02 DEV (RJBS on 2015-10-30)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.37_01/lib/Carp.pm" >1.37_01 DEV (RJBS on 2015-10-26)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.3301/lib/Carp.pm" >1.3301 (ZEFRAM on 2014-04-01)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.33/lib/Carp.pm" >1.33 (ZEFRAM on 2014-03-24)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.32/lib/Carp.pm" >1.32 (ZEFRAM on 2013-09-03)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.26/lib/Carp.pm" >1.26 (ZEFRAM on 2012-06-18)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.25/lib/Carp.pm" >1.25 (ZEFRAM on 2012-02-16)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.24/lib/Carp.pm" >1.24 (ZEFRAM on 2012-02-02)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.23/lib/Carp.pm" >1.23 (ZEFRAM on 2011-09-09)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/ZEFRAM/Carp-1.22/lib/Carp.pm" >1.22 (ZEFRAM on 2011-09-04)</option> <optgroup label="BackPAN">' <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.36/lib/Carp.pm" >1.36 (RJBS on 2015-03-19)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.35/lib/Carp.pm" >1.35 (RJBS on 2015-03-15)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.34_02/lib/Carp.pm" >1.34_02 DEV (RJBS on 2015-03-06)</option> <option value="/release/XSAWYERX/Carp-1.50/diff/RJBS/Carp-1.34_01/lib/Carp.pm" >1.34_01 DEV (RJBS on 2015-03-05)</option> </optgroup> </select> </li> <li> <ul class="dependencies"> <li class="nav-header">Dependencies</li> <li><a href="/pod/Config" title="Config" class="ellipsis">Config</a></li> <li><a href="/pod/Exporter" title="Exporter" class="ellipsis">Exporter</a></li> <li><a href="/pod/ExtUtils::MakeMaker" title="ExtUtils::MakeMaker" class="ellipsis">ExtUtils::MakeMaker</a></li> <li><a href="/pod/IPC::Open3" title="IPC::Open3" class="ellipsis">IPC::Open3</a></li> <li><a href="/pod/Test::More" title="Test::More" class="ellipsis">Test::More</a></li> <li><a href="/pod/overload" title="overload" class="ellipsis">overload</a></li> <li><a href="/pod/strict" title="strict" class="ellipsis">strict</a></li> <li><a href="/pod/warnings" title="warnings" class="ellipsis">warnings</a></li> <li><i class="ttip" title="dynamic_config enabled">and possibly others</i></li> <li> <hr> </li> <li> <a href="/module/Carp/requires">Reverse dependencies</a> </li> <li> <a href="http://deps.cpantesters.org/?module=Carp">CPAN Testers List</a> </li> <li> <a href="https://cpandeps.grinnz.com/?dist=Carp">Dependency graph</a> </li> </ul> </li> <li class="nav-header">Permalinks</li> <li> <a href="/release/XSAWYERX/Carp-1.50/view/lib/Carp.pm">This version</a> </li> <li> <a href="/pod/Carp">Latest version</a> </li> <li> <div class="plussers"> <div class="nav-header">++ed by:</div> <div> <a class="display-all" href="/author/SYP"><img src="https://www.gravatar.com/avatar/9b6fa62677be03c25ee1af4d95472042?d=identicon&amp;s=20" title="SYP" alt="SYP"></a> <a class="display-all" href="/author/MPERRY"><img src="https://www.gravatar.com/avatar/5b9c91a7e965d444f1c444fd8c03d80c?d=identicon&amp;s=20" title="MPERRY" alt="MPERRY"></a> <a class="display-all" href="/author/DMOL"><img src="https://www.gravatar.com/avatar/78b28a931707c0fa696996adc7702db9?d=identicon&amp;s=20" title="DMOL" alt="DMOL"></a> <a class="display-all" href="/author/KOORCHIK"><img src="https://www.gravatar.com/avatar/388f0564e6cb0de0996b4f0d3c50e9f8?d=identicon&amp;s=20" title="KOORCHIK" alt="KOORCHIK"></a> <a class="display-all" href="/author/PINGAN"><img src="https://www.gravatar.com/avatar/a02222e5e0e4fd24d52fc29a08ac6bcb?d=identicon&amp;s=20" title="PINGAN" alt="PINGAN"></a> </div> <!-- Display counts of plussers--> <div> <a href="/dist/Carp/plussers">37 PAUSE users</a> </div> <div> 38 non-PAUSE users </div> </div> </li> <li> </li> </ul> </div> </nav> <div class="content-navigation"> <div class="breadcrumbs"> <span> <a data-keyboard-shortcut="g a" rel="author" href="/author/XSAWYERX" class="author-name">Sawyer X</a> </span> <span>&nbsp;/&nbsp;</span> <div class="release dist-release status-latest maturity-released"> <span class="dropdown"><b class="caret"></b></span> <select class="select-navigator "> <option selected value="/release/XSAWYERX/Carp-1.50/view/lib/Carp.pm" >1.50 (XSAWYERX on 2018-09-08)</option> <option value="/release/RJBS/Carp-1.38/view/lib/Carp.pm" >1.38 (RJBS on 2015-11-06)</option> <option value="/release/RJBS/Carp-1.37_02/view/lib/Carp.pm" >1.37_02 DEV (RJBS on 2015-10-30)</option> <option value="/release/RJBS/Carp-1.37_01/view/lib/Carp.pm" >1.37_01 DEV (RJBS on 2015-10-26)</option> <option value="/release/ZEFRAM/Carp-1.3301/view/lib/Carp.pm" >1.3301 (ZEFRAM on 2014-04-01)</option> <option value="/release/ZEFRAM/Carp-1.33/view/lib/Carp.pm" >1.33 (ZEFRAM on 2014-03-24)</option> <option value="/release/ZEFRAM/Carp-1.32/view/lib/Carp.pm" >1.32 (ZEFRAM on 2013-09-03)</option> <option value="/release/ZEFRAM/Carp-1.26/view/lib/Carp.pm" >1.26 (ZEFRAM on 2012-06-18)</option> <option value="/release/ZEFRAM/Carp-1.25/view/lib/Carp.pm" >1.25 (ZEFRAM on 2012-02-16)</option> <option value="/release/ZEFRAM/Carp-1.24/view/lib/Carp.pm" >1.24 (ZEFRAM on 2012-02-02)</option> <option value="/release/ZEFRAM/Carp-1.23/view/lib/Carp.pm" >1.23 (ZEFRAM on 2011-09-09)</option> <option value="/release/ZEFRAM/Carp-1.22/view/lib/Carp.pm" >1.22 (ZEFRAM on 2011-09-04)</option> <optgroup label="BackPAN">' <option value="/release/RJBS/Carp-1.36/view/lib/Carp.pm" >1.36 (RJBS on 2015-03-19)</option> <option value="/release/RJBS/Carp-1.35/view/lib/Carp.pm" >1.35 (RJBS on 2015-03-15)</option> <option value="/release/RJBS/Carp-1.34_02/view/lib/Carp.pm" >1.34_02 DEV (RJBS on 2015-03-06)</option> <option value="/release/RJBS/Carp-1.34_01/view/lib/Carp.pm" >1.34_01 DEV (RJBS on 2015-03-05)</option> </optgroup> </select> <a data-keyboard-shortcut="g d" class="release-name" href="/dist/Carp">Carp-1.50</a> </div> <span class="river-gauge-gauge"> <svg width="24px" height="15px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <title> River stage five &#10; • 5041 direct dependents &#10; • 33320 total dependents </title> <rect x="0" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="5" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="10" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="15" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="20" y="0" width="4" height="15" fill="#7ea3f2" /> </g> </svg> </span> <div id="Carp-fav" class="show-logged-in"> <form action="/account/favorite/add" style="display: inline" method="POST"> <input type="hidden" name="remove" value="0"> <input type="hidden" name="release" value="Carp-1.50"> <input type="hidden" name="author" value="XSAWYERX"> <input type="hidden" name="distribution" value="Carp"> <button type="submit" class="favorite highlight"><span>75</span> ++</button> </form> </div> <div class="show-logged-out"> <button class="fav-not-logged-in favorite highlight"><span>75</span> ++</button> </div> / <span>Carp</span> </div> </div> <main class="content"> <nav class="toc"> <div class="toc-header"><strong>Contents</strong></div> <ul> <li><a href="#NAME">NAME</a></li> <li><a href="#SYNOPSIS">SYNOPSIS</a></li> <li><a href="#DESCRIPTION">DESCRIPTION</a> <ul> <li><a href="#Forcing-a-Stack-Trace">Forcing a Stack Trace</a></li> <li><a href="#Stack-Trace-formatting">Stack Trace formatting</a></li> </ul> </li> <li><a href="#GLOBAL-VARIABLES">GLOBAL VARIABLES</a> <ul> <li><a href="#%24Carp%3A%3AMaxEvalLen">$Carp::MaxEvalLen</a></li> <li><a href="#%24Carp%3A%3AMaxArgLen">$Carp::MaxArgLen</a></li> <li><a href="#%24Carp%3A%3AMaxArgNums">$Carp::MaxArgNums</a></li> <li><a href="#%24Carp%3A%3AVerbose">$Carp::Verbose</a></li> <li><a href="#%24Carp%3A%3ARefArgFormatter">$Carp::RefArgFormatter</a></li> <li><a href="#%40CARP_NOT">@CARP_NOT</a></li> <li><a href="#%25Carp%3A%3AInternal">%Carp::Internal</a></li> <li><a href="#%25Carp%3A%3ACarpInternal">%Carp::CarpInternal</a></li> <li><a href="#%24Carp%3A%3ACarpLevel">$Carp::CarpLevel</a></li> </ul> </li> <li><a href="#BUGS">BUGS</a></li> <li><a href="#SEE-ALSO">SEE ALSO</a></li> <li><a href="#CONTRIBUTING">CONTRIBUTING</a></li> <li><a href="#AUTHOR">AUTHOR</a></li> <li><a href="#COPYRIGHT">COPYRIGHT</a></li> <li><a href="#LICENSE">LICENSE</a></li> </ul></nav> <div class="pod anchors"> <h1 id="NAME">NAME</h1> <p>Carp - alternative warn and die for modules</p> <h1 id="SYNOPSIS">SYNOPSIS</h1> <pre><code>use Carp; # warn user (from perspective of caller) carp &quot;string trimmed to 80 chars&quot;; # die of errors (from perspective of caller) croak &quot;We&#39;re outta here!&quot;; # die of errors with stack backtrace confess &quot;not implemented&quot;; # cluck, longmess and shortmess not exported by default use Carp qw(cluck longmess shortmess); cluck &quot;This is how we got here!&quot;; # warn with stack backtrace $long_message = longmess( &quot;message from cluck() or confess()&quot; ); $short_message = shortmess( &quot;message from carp() or croak()&quot; );</code></pre> <h1 id="DESCRIPTION">DESCRIPTION</h1> <p>The Carp routines are useful in your own modules because they act like <code>die()</code> or <code>warn()</code>, but with a message which is more likely to be useful to a user of your module. In the case of <code>cluck()</code> and <code>confess()</code>, that context is a summary of every call in the call-stack; <code>longmess()</code> returns the contents of the error message.</p> <p>For a shorter message you can use <code>carp()</code> or <code>croak()</code> which report the error as being from where your module was called. <code>shortmess()</code> returns the contents of this error message. There is no guarantee that that is where the error was, but it is a good educated guess.</p> <p><code>Carp</code> takes care not to clobber the status variables <code>$!</code> and <code>$^E</code> in the course of assembling its error messages. This means that a <code>$SIG{__DIE__}</code> or <code>$SIG{__WARN__}</code> handler can capture the error information held in those variables, if it is required to augment the error message, and if the code calling <code>Carp</code> left useful values there. Of course, <code>Carp</code> can&#39;t guarantee the latter.</p> <p>You can also alter the way the output and logic of <code>Carp</code> works, by changing some global variables in the <code>Carp</code> namespace. See the section on <code>GLOBAL VARIABLES</code> below.</p> <p>Here is a more complete description of how <code>carp</code> and <code>croak</code> work. What they do is search the call-stack for a function call stack where they have not been told that there shouldn&#39;t be an error. If every call is marked safe, they give up and give a full stack backtrace instead. In other words they presume that the first likely looking potential suspect is guilty. Their rules for telling whether a call shouldn&#39;t generate errors work as follows:</p> <ol> <li><p>Any call from a package to itself is safe.</p> </li> <li><p>Packages claim that there won&#39;t be errors on calls to or from packages explicitly marked as safe by inclusion in <code>@CARP_NOT</code>, or (if that array is empty) <code>@ISA</code>. The ability to override what @ISA says is new in 5.8.</p> </li> <li><p>The trust in item 2 is transitive. If A trusts B, and B trusts C, then A trusts C. So if you do not override <code>@ISA</code> with <code>@CARP_NOT</code>, then this trust relationship is identical to, &quot;inherits from&quot;.</p> </li> <li><p>Any call from an internal Perl module is safe. (Nothing keeps user modules from marking themselves as internal to Perl, but this practice is discouraged.)</p> </li> <li><p>Any call to Perl&#39;s warning system (eg Carp itself) is safe. (This rule is what keeps it from reporting the error at the point where you call <code>carp</code> or <code>croak</code>.)</p> </li> <li><p><code>$Carp::CarpLevel</code> can be set to skip a fixed number of additional call levels. Using this is not recommended because it is very difficult to get it to behave correctly.</p> </li> </ol> <h2 id="Forcing-a-Stack-Trace"><a id="Forcing"></a>Forcing a Stack Trace</h2> <p>As a debugging aid, you can force Carp to treat a croak as a confess and a carp as a cluck across <i>all</i> modules. In other words, force a detailed stack trace to be given. This can be very helpful when trying to understand why, or from where, a warning or error is being generated.</p> <p>This feature is enabled by &#39;importing&#39; the non-existent symbol &#39;verbose&#39;. You would typically enable it by saying</p> <pre><code>perl -MCarp=verbose script.pl</code></pre> <p>or by including the string <code>-MCarp=verbose</code> in the PERL5OPT environment variable.</p> <p>Alternately, you can set the global variable <code>$Carp::Verbose</code> to true. See the <code>GLOBAL VARIABLES</code> section below.</p> <h2 id="Stack-Trace-formatting"><a id="Stack"></a>Stack Trace formatting</h2> <p>At each stack level, the subroutine&#39;s name is displayed along with its parameters. For simple scalars, this is sufficient. For complex data types, such as objects and other references, this can simply display <code>&#39;HASH(0x1ab36d8)&#39;</code>.</p> <p>Carp gives two ways to control this.</p> <ol> <li><p>For objects, a method, <code>CARP_TRACE</code>, will be called, if it exists. If this method doesn&#39;t exist, or it recurses into <code>Carp</code>, or it otherwise throws an exception, this is skipped, and Carp moves on to the next option, otherwise checking stops and the string returned is used. It is recommended that the object&#39;s type is part of the string to make debugging easier.</p> </li> <li><p>For any type of reference, <code>$Carp::RefArgFormatter</code> is checked (see below). This variable is expected to be a code reference, and the current parameter is passed in. If this function doesn&#39;t exist (the variable is undef), or it recurses into <code>Carp</code>, or it otherwise throws an exception, this is skipped, and Carp moves on to the next option, otherwise checking stops and the string returned is used.</p> </li> <li><p>Otherwise, if neither <code>CARP_TRACE</code> nor <code>$Carp::RefArgFormatter</code> is available, stringify the value ignoring any overloading.</p> </li> </ol> <h1 id="GLOBAL-VARIABLES"><a id="GLOBAL"></a>GLOBAL VARIABLES</h1> <h2 id="$Carp::MaxEvalLen"><a id="Carp::MaxEvalLen"></a>$Carp::MaxEvalLen</h2> <p>This variable determines how many characters of a string-eval are to be shown in the output. Use a value of <code>0</code> to show all text.</p> <p>Defaults to <code>0</code>.</p> <h2 id="$Carp::MaxArgLen"><a id="Carp::MaxArgLen"></a>$Carp::MaxArgLen</h2> <p>This variable determines how many characters of each argument to a function to print. Use a value of <code>0</code> to show the full length of the argument.</p> <p>Defaults to <code>64</code>.</p> <h2 id="$Carp::MaxArgNums"><a id="Carp::MaxArgNums"></a>$Carp::MaxArgNums</h2> <p>This variable determines how many arguments to each function to show. Use a false value to show all arguments to a function call. To suppress all arguments, use <code>-1</code> or <code>&#39;0 but true&#39;</code>.</p> <p>Defaults to <code>8</code>.</p> <h2 id="$Carp::Verbose"><a id="Carp::Verbose"></a>$Carp::Verbose</h2> <p>This variable makes <code>carp()</code> and <code>croak()</code> generate stack backtraces just like <code>cluck()</code> and <code>confess()</code>. This is how <code>use Carp &#39;verbose&#39;</code> is implemented internally.</p> <p>Defaults to <code>0</code>.</p> <h2 id="$Carp::RefArgFormatter"><a id="Carp::RefArgFormatter"></a>$Carp::RefArgFormatter</h2> <p>This variable sets a general argument formatter to display references. Plain scalars and objects that implement <code>CARP_TRACE</code> will not go through this formatter. Calling <code>Carp</code> from within this function is not supported.</p> <p>local $Carp::RefArgFormatter = sub { require Data::Dumper; Data::Dumper::Dump($_[0]); # not necessarily safe };</p> <h2 id="@CARP_NOT"><a id="CARP_NOT"></a>@CARP_NOT</h2> <p>This variable, <i>in your package</i>, says which packages are <i>not</i> to be considered as the location of an error. The <code>carp()</code> and <code>cluck()</code> functions will skip over callers when reporting where an error occurred.</p> <p>NB: This variable must be in the package&#39;s symbol table, thus:</p> <pre><code># These work our @CARP_NOT; # file scope use vars qw(@CARP_NOT); # package scope @My::Package::CARP_NOT = ... ; # explicit package variable # These don&#39;t work sub xyz { ... @CARP_NOT = ... } # w/o declarations above my @CARP_NOT; # even at top-level</code></pre> <p>Example of use:</p> <pre><code>package My::Carping::Package; use Carp; our @CARP_NOT; sub bar { .... or _error(&#39;Wrong input&#39;) } sub _error { # temporary control of where&#39;ness, __PACKAGE__ is implicit local @CARP_NOT = qw(My::Friendly::Caller); carp(@_) }</code></pre> <p>This would make <code>Carp</code> report the error as coming from a caller not in <code>My::Carping::Package</code>, nor from <code>My::Friendly::Caller</code>.</p> <p>Also read the <a href="#DESCRIPTION">&quot;DESCRIPTION&quot;</a> section above, about how <code>Carp</code> decides where the error is reported from.</p> <p>Use <code>@CARP_NOT</code>, instead of <code>$Carp::CarpLevel</code>.</p> <p>Overrides <code>Carp</code>&#39;s use of <code>@ISA</code>.</p> <h2 id="%Carp::Internal"><a id="Carp::Internal"></a>%Carp::Internal</h2> <p>This says what packages are internal to Perl. <code>Carp</code> will never report an error as being from a line in a package that is internal to Perl. For example:</p> <pre><code>$Carp::Internal{ (__PACKAGE__) }++; # time passes... sub foo { ... or confess(&quot;whatever&quot;) };</code></pre> <p>would give a full stack backtrace starting from the first caller outside of __PACKAGE__. (Unless that package was also internal to Perl.)</p> <h2 id="%Carp::CarpInternal"><a id="Carp::CarpInternal"></a>%Carp::CarpInternal</h2> <p>This says which packages are internal to Perl&#39;s warning system. For generating a full stack backtrace this is the same as being internal to Perl, the stack backtrace will not start inside packages that are listed in <code>%Carp::CarpInternal</code>. But it is slightly different for the summary message generated by <code>carp</code> or <code>croak</code>. There errors will not be reported on any lines that are calling packages in <code>%Carp::CarpInternal</code>.</p> <p>For example <code>Carp</code> itself is listed in <code>%Carp::CarpInternal</code>. Therefore the full stack backtrace from <code>confess</code> will not start inside of <code>Carp</code>, and the short message from calling <code>croak</code> is not placed on the line where <code>croak</code> was called.</p> <h2 id="$Carp::CarpLevel"><a id="Carp::CarpLevel"></a>$Carp::CarpLevel</h2> <p>This variable determines how many additional call frames are to be skipped that would not otherwise be when reporting where an error occurred on a call to one of <code>Carp</code>&#39;s functions. It is fairly easy to count these call frames on calls that generate a full stack backtrace. However it is much harder to do this accounting for calls that generate a short message. Usually people skip too many call frames. If they are lucky they skip enough that <code>Carp</code> goes all of the way through the call stack, realizes that something is wrong, and then generates a full stack backtrace. If they are unlucky then the error is reported from somewhere misleading very high in the call stack.</p> <p>Therefore it is best to avoid <code>$Carp::CarpLevel</code>. Instead use <code>@CARP_NOT</code>, <code>%Carp::Internal</code> and <code>%Carp::CarpInternal</code>.</p> <p>Defaults to <code>0</code>.</p> <h1 id="BUGS">BUGS</h1> <p>The Carp routines don&#39;t handle exception objects currently. If called with a first argument that is a reference, they simply call die() or warn(), as appropriate.</p> <h1 id="SEE-ALSO"><a id="SEE"></a>SEE ALSO</h1> <p><a href="/pod/Carp::Always">Carp::Always</a>, <a href="/pod/Carp::Clan">Carp::Clan</a></p> <h1 id="CONTRIBUTING">CONTRIBUTING</h1> <p><a href="/pod/Carp">Carp</a> is maintained by the perl 5 porters as part of the core perl 5 version control repository. Please see the <a href="/pod/perlhack">perlhack</a> perldoc for how to submit patches and contribute to it.</p> <h1 id="AUTHOR">AUTHOR</h1> <p>The Carp module first appeared in Larry Wall&#39;s perl 5.000 distribution. Since then it has been modified by several of the perl 5 porters. Andrew Main (Zefram) &lt;zefram@fysh.org&gt; divested Carp into an independent distribution.</p> <h1 id="COPYRIGHT">COPYRIGHT</h1> <p>Copyright (C) 1994-2013 Larry Wall</p> <p>Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) &lt;zefram@fysh.org&gt;</p> <h1 id="LICENSE">LICENSE</h1> <p>This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p></div> <div id="metacpan_install-instructions-dialog" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Module Install Instructions</h4> </div> <div class="modal-body"> <p>To install Carp, copy and paste the appropriate command in to your terminal.</p> <p><a href="/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p> <pre><code>cpanm Carp</code></pre> <p><a href="/pod/CPAN">CPAN shell</a></p> <pre><code>perl -MCPAN -e shell install Carp</code></pre> <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> </div> </div> </div> </div> </main> <div class="content-pagination"> </div> </div> <footer class="footer"> <div class="footer-container"> <div class="footer-social"> <div class="footer-link footer-logo"> <a href="/"> <img src="/static/images/metacpan-logo.svg" alt="MetaCPAN" /> </a> </div> <a class="footer-social-link" href="https://github.com/metacpan"> <i class="fab fa-github-square"></i> </a> <a class="footer-social-link" href="https://fosstodon.org/@metacpan"> <i class="fab fa-mastodon"></i> </a> </div> <div class="footer-links"> <div class="footer-link"> <a href="/about">About</a> </div> <div class="footer-link"> <a href="/about/sponsors">Sponsor</a> </div> <div class="footer-link"> <a href="https://grep.metacpan.org">grep::cpan</a> </div> <div class="footer-link"> <a href="/recent">Recent</a> </div> <div class="footer-link"> <a href="/about/faq">FAQ</a> </div> <div class="footer-link"> <a href="/tools">Tools</a> </div> <div class="footer-link"> <a href="https://fastapi.metacpan.org/">API</a> </div> <div class="footer-link"> <a href="https://www.perl.org/">Perl.org</a> </div> </div> <div class="footer-sponsors"> <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener"> <img class="footer-sponsor-bytemark" src="/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener"> <img class="footer-sponsor-liquidweb" src="/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener"> <img class="footer-sponsor-deriv" src="/static/images/sponsors/deriv.svg" alt="Deriv logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener"> <img class="footer-sponsor-geocode" src="/static/images/sponsors/geocodelogo.svg" alt="Geocode logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener"> <img class="footer-sponsor-fastly" src="/static/images/sponsors/fastly_logo.svg" alt="Fastly logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener"> <img class="footer-sponsor-opencage" src="/static/images/sponsors/open-cage.svg" alt="OpenCage logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://www.elastic.co/" rel="noopener"> <img class="footer-sponsor-elastic" src="/static/images/sponsors/elastic.svg" alt="Elastic logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://route4me.com/" rel="noopener"> <img class="footer-sponsor-route4me" src="/static/images/sponsors/route4me.png" alt="Route4Me logo"> </a> </div> </div> </footer> <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Keyboard Shortcuts</h4> </div> <div class="modal-body row"> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Global</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>s</kbd> </td> <td>Focus search bar</td> </tr> <tr> <td class="keys"> <kbd>?</kbd> </td> <td>Bring up this help dialog</td> </tr> </tbody> </table> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>GitHub</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>p</kbd> </td> <td>Go to pull requests</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>go to github issues (only if github is preferred repository)</td> </tr> </tbody> </table> </div> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>POD</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>a</kbd> </td> <td>Go to author</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>c</kbd> </td> <td>Go to changes</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>Go to issues</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>d</kbd> </td> <td>Go to dist</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>r</kbd> </td> <td>Go to repository/SCM</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>s</kbd> </td> <td>Go to source</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>b</kbd> </td> <td>Go to file browse</td> </tr> </tbody> </table> </div> <div class="col-md-12"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Search terms</th> </tr> </thead> <tbody> <tr> <td><em>module:</em> (e.g. <a href="/search?q=module%3APlugin">module:Plugin</a>)</td> </tr> <tr> <td><em>distribution:</em> (e.g. <a href="/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td> </tr> <tr> <td><em>author:</em> (e.g. <a href="/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td> </tr> <tr> <td><em>version:</em> (e.g. <a href="/search?q=version%3A1.00">version:1.00</a>)</td> </tr> </tbody> </table> </div> </div> <div class="modal-footer"></div> </div> </div> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10