Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.2 KiB

  1. package Carp;
  2. =head1 NAME
  3. carp - warn of errors (from perspective of caller)
  4. cluck - warn of errors with stack backtrace
  5. (not exported by default)
  6. croak - die of errors (from perspective of caller)
  7. confess - die of errors with stack backtrace
  8. =head1 SYNOPSIS
  9. use Carp;
  10. croak "We're outta here!";
  11. use Carp qw(cluck);
  12. cluck "This is how we got here!";
  13. =head1 DESCRIPTION
  14. The Carp routines are useful in your own modules because
  15. they act like die() or warn(), but report where the error
  16. was in the code they were called from. Thus if you have a
  17. routine Foo() that has a carp() in it, then the carp()
  18. will report the error as occurring where Foo() was called,
  19. not where carp() was called.
  20. =head2 Forcing a Stack Trace
  21. As a debugging aid, you can force Carp to treat a croak as a confess
  22. and a carp as a cluck across I<all> modules. In other words, force a
  23. detailed stack trace to be given. This can be very helpful when trying
  24. to understand why, or from where, a warning or error is being generated.
  25. This feature is enabled by 'importing' the non-existent symbol
  26. 'verbose'. You would typically enable it by saying
  27. perl -MCarp=verbose script.pl
  28. or by including the string C<MCarp=verbose> in the L<PERL5OPT>
  29. environment variable.
  30. =head1 BUGS
  31. The Carp routines don't handle exception objects currently.
  32. If called with a first argument that is a reference, they simply
  33. call die() or warn(), as appropriate.
  34. =cut
  35. # This package is heavily used. Be small. Be fast. Be good.
  36. # Comments added by Andy Wardley <[email protected]> 09-Apr-98, based on an
  37. # _almost_ complete understanding of the package. Corrections and
  38. # comments are welcome.
  39. # The $CarpLevel variable can be set to "strip off" extra caller levels for
  40. # those times when Carp calls are buried inside other functions. The
  41. # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
  42. # text and function arguments should be formatted when printed.
  43. $CarpLevel = 0; # How many extra package levels to skip on carp.
  44. $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
  45. $MaxArgLen = 64; # How much of each argument to print. 0 = all.
  46. $MaxArgNums = 8; # How many arguments to print. 0 = all.
  47. $Verbose = 0; # If true then make shortmess call longmess instead
  48. require Exporter;
  49. @ISA = ('Exporter');
  50. @EXPORT = qw(confess croak carp);
  51. @EXPORT_OK = qw(cluck verbose);
  52. @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
  53. # if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
  54. # then the following method will be called by the Exporter which knows
  55. # to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
  56. # 'verbose'.
  57. sub export_fail {
  58. shift;
  59. $Verbose = shift if $_[0] eq 'verbose';
  60. return @_;
  61. }
  62. # longmess() crawls all the way up the stack reporting on all the function
  63. # calls made. The error string, $error, is originally constructed from the
  64. # arguments passed into longmess() via confess(), cluck() or shortmess().
  65. # This gets appended with the stack trace messages which are generated for
  66. # each function call on the stack.
  67. sub longmess {
  68. { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
  69. goto &longmess_heavy;
  70. }
  71. # shortmess() is called by carp() and croak() to skip all the way up to
  72. # the top-level caller's package and report the error from there. confess()
  73. # and cluck() generate a full stack trace so they call longmess() to
  74. # generate that. In verbose mode shortmess() calls longmess() so
  75. # you always get a stack trace
  76. sub shortmess { # Short-circuit &longmess if called via multiple packages
  77. { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
  78. goto &shortmess_heavy;
  79. }
  80. # the following four functions call longmess() or shortmess() depending on
  81. # whether they should generate a full stack trace (confess() and cluck())
  82. # or simply report the caller's package (croak() and carp()), respectively.
  83. # confess() and croak() die, carp() and cluck() warn.
  84. sub croak { die shortmess @_ }
  85. sub confess { die longmess @_ }
  86. sub carp { warn shortmess @_ }
  87. sub cluck { warn longmess @_ }
  88. 1;