SEARCH
NEW RPMS
DIRECTORIES
ABOUT
FAQ
VARIOUS
BLOG
DONATE


YUM REPOSITORY

 
 

MAN page from RedHat EL 7 rh-perl526-perl-Carp-1.42-395.el7.noarch.rpm

Carp

Section: User Contributed Perl Documentation (3)
Updated: 2017-12-18
Index 

NAME

Carp - alternative warn and die for modules 

SYNOPSIS

    use Carp;    # warn user (from perspective of caller)    carp "string trimmed to 80 chars";    # die of errors (from perspective of caller)    croak "We're outta here!";    # die of errors with stack backtrace    confess "not implemented";    # cluck, longmess and shortmess not exported by default    use Carp qw(cluck longmess shortmess);    cluck "This is how we got here!"; # warn with stack backtrace    $long_message   = longmess( "message from cluck() or confess()" );    $short_message  = shortmess( "message from carp() or croak()" );
 

DESCRIPTION

The Carp routines are useful in your own modules becausethey act like "die()" or "warn()", but with a message which is morelikely to be useful to a user of your module. In the case of"cluck()" and "confess()", that context is a summary of everycall in the call-stack; "longmess()" returns the contents of the errormessage.

For a shorter message you can use "carp()" or "croak()" which report theerror as being from where your module was called. "shortmess()" returns thecontents of this error message. There is no guarantee that that is where theerror was, but it is a good educated guess.

"Carp" takes care not to clobber the status variables $! and $^Ein the course of assembling its error messages. This means that a$SIG{__DIE__} or $SIG{__WARN__} handler can capture the errorinformation held in those variables, if it is required to augment theerror message, and if the code calling "Carp" left useful values there.Of course, "Carp" can't guarantee the latter.

You can also alter the way the output and logic of "Carp" works, bychanging some global variables in the "Carp" namespace. See thesection on "GLOBAL VARIABLES" below.

Here is a more complete description of how "carp" and "croak" work.What they do is search the call-stack for a function call stack wherethey have not been told that there shouldn't be an error. If everycall is marked safe, they give up and give a full stack backtraceinstead. In other words they presume that the first likely lookingpotential suspect is guilty. Their rules for telling whethera call shouldn't generate errors work as follows:

1.
Any call from a package to itself is safe.
2.
Packages claim that there won't be errors on calls to or frompackages explicitly marked as safe by inclusion in @CARP_NOT, or(if that array is empty) @ISA. The ability to override what@ISA says is new in 5.8.
3.
The trust in item 2 is transitive. If A trusts B, and Btrusts C, then A trusts C. So if you do not override @ISAwith @CARP_NOT, then this trust relationship is identical to,``inherits from''.
4.
Any call from an internal Perl module is safe. (Nothing keepsuser modules from marking themselves as internal to Perl, butthis practice is discouraged.)
5.
Any call to Perl's warning system (eg Carp itself) is safe.(This rule is what keeps it from reporting the error at thepoint where you call "carp" or "croak".)
6.
$Carp::CarpLevel can be set to skip a fixed number of additionalcall levels. Using this is not recommended because it is verydifficult to get it to behave correctly.
 

Forcing a Stack Trace

As a debugging aid, you can force Carp to treat a croak as a confessand a carp as a cluck across all modules. In other words, force adetailed stack trace to be given. This can be very helpful when tryingto understand why, or from where, a warning or error is being generated.

This feature is enabled by 'importing' the non-existent symbol'verbose'. You would typically enable it by saying

    perl -MCarp=verbose script.pl

or by including the string "-MCarp=verbose" in the PERL5OPTenvironment variable.

Alternately, you can set the global variable $Carp::Verbose to true.See the "GLOBAL VARIABLES" section below. 

Stack Trace formatting

At each stack level, the subroutine's name is displayed along withits parameters. For simple scalars, this is sufficient. For complexdata types, such as objects and other references, this can simplydisplay 'HASH(0x1ab36d8)'.

Carp gives two ways to control this.

1.
For objects, a method, "CARP_TRACE", will be called, if it exists. Ifthis method doesn't exist, or it recurses into "Carp", or it otherwisethrows an exception, this is skipped, and Carp moves on to the next option,otherwise checking stops and the string returned is used. It is recommendedthat the object's type is part of the string to make debugging easier.
2.
For any type of reference, $Carp::RefArgFormatter is checked (see below).This variable is expected to be a code reference, and the current parameteris passed in. If this function doesn't exist (the variable is undef), orit recurses into "Carp", or it otherwise throws an exception, this isskipped, and Carp moves on to the next option, otherwise checking stopsand the string returned is used.
3.
Otherwise, if neither "CARP_TRACE" nor $Carp::RefArgFormatter isavailable, stringify the value ignoring any overloading.
 

GLOBAL VARIABLES

 

$Carp::MaxEvalLen

This variable determines how many characters of a string-eval are tobe shown in the output. Use a value of 0 to show all text.

Defaults to 0. 

$Carp::MaxArgLen

This variable determines how many characters of each argument to afunction to print. Use a value of 0 to show the full length of theargument.

Defaults to 64. 

$Carp::MaxArgNums

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 allarguments, use "-1" or '0 but true'.

Defaults to 8. 

$Carp::Verbose

This variable makes "carp()" and "croak()" generate stack backtracesjust like "cluck()" and "confess()". This is how "use Carp 'verbose'"is implemented internally.

Defaults to 0. 

$Carp::RefArgFormatter

This variable sets a general argument formatter to display references.Plain scalars and objects that implement "CARP_TRACE" will not go throughthis formatter. Calling "Carp" from within this function is not supported.

local $Carp::RefArgFormatter = sub {
    require Data::Dumper;
    Data::Dumper::Dump($_[0]); # not necessarily safe}; 

@CARP_NOT

This variable, in your package, says which packages are not to beconsidered as the location of an error. The "carp()" and "cluck()"functions will skip over callers when reporting where an error occurred.

NB: This variable must be in the package's symbol table, thus:

    # These work    our @CARP_NOT; # file scope    use vars qw(@CARP_NOT); # package scope    @My::Package::CARP_NOT = ... ; # explicit package variable    # These don't work    sub xyz { ... @CARP_NOT = ... } # w/o declarations above    my @CARP_NOT; # even at top-level

Example of use:

    package My::Carping::Package;    use Carp;    our @CARP_NOT;    sub bar     { .... or _error('Wrong input') }    sub _error  {        # temporary control of where'ness, __PACKAGE__ is implicit        local @CARP_NOT = qw(My::Friendly::Caller);        carp(@_)    }

This would make "Carp" report the error as coming from a caller notin "My::Carping::Package", nor from "My::Friendly::Caller".

Also read the ``DESCRIPTION'' section above, about how "Carp" decideswhere the error is reported from.

Use @CARP_NOT, instead of $Carp::CarpLevel.

Overrides "Carp"'s use of @ISA. 

%Carp::Internal

This says what packages are internal to Perl. "Carp" will neverreport an error as being from a line in a package that is internal toPerl. For example:

    $Carp::Internal{ (__PACKAGE__) }++;    # time passes...    sub foo { ... or confess("whatever") };

would give a full stack backtrace starting from the first calleroutside of __PACKAGE__. (Unless that package was also internal toPerl.) 

%Carp::CarpInternal

This says which packages are internal to Perl's warning system. Forgenerating a full stack backtrace this is the same as being internalto Perl, the stack backtrace will not start inside packages that arelisted in %Carp::CarpInternal. But it is slightly different forthe summary message generated by "carp" or "croak". There errorswill not be reported on any lines that are calling packages in%Carp::CarpInternal.

For example "Carp" itself is listed in %Carp::CarpInternal.Therefore the full stack backtrace from "confess" will not startinside of "Carp", and the short message from calling "croak" isnot placed on the line where "croak" was called. 

$Carp::CarpLevel

This variable determines how many additional call frames are to beskipped that would not otherwise be when reporting where an erroroccurred on a call to one of "Carp"'s functions. It is fairly easyto count these call frames on calls that generate a full stackbacktrace. However it is much harder to do this accounting for callsthat generate a short message. Usually people skip too many callframes. If they are lucky they skip enough that "Carp" goes all ofthe way through the call stack, realizes that something is wrong, andthen generates a full stack backtrace. If they are unlucky then theerror is reported from somewhere misleading very high in the callstack.

Therefore it is best to avoid $Carp::CarpLevel. Instead use@CARP_NOT, %Carp::Internal and %Carp::CarpInternal.

Defaults to 0. 

BUGS

The Carp routines don't handle exception objects currently.If called with a first argument that is a reference, they simplycall die() or warn(), as appropriate. 

SEE ALSO

Carp::Always,Carp::Clan 

CONTRIBUTING

Carp is maintained by the perl 5 porters as part of the core perl 5version control repository. Please see the perlhack perldoc for how tosubmit patches and contribute to it. 

AUTHOR

The Carp module first appeared in Larry Wall's perl 5.000 distribution.Since then it has been modified by several of the perl 5 porters.Andrew Main (Zefram) <zeframAATTfysh.org> divested Carp into an independentdistribution. 

COPYRIGHT

Copyright (C) 1994-2013 Larry Wall

Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zeframAATTfysh.org> 

LICENSE

This module is free software; you can redistribute it and/or modify itunder the same terms as Perl itself.


 

Index

NAME
SYNOPSIS
DESCRIPTION
Forcing a Stack Trace
Stack Trace formatting
GLOBAL VARIABLES
$Carp::MaxEvalLen
$Carp::MaxArgLen
$Carp::MaxArgNums
$Carp::Verbose
$Carp::RefArgFormatter
@CARP_NOT
%Carp::Internal
%Carp::CarpInternal
$Carp::CarpLevel
BUGS
SEE ALSO
CONTRIBUTING
AUTHOR
COPYRIGHT
LICENSE

This document was created byman2html,using the manual pages.