Almost-minimal filesystem based blog.
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.

222 lines
7.7 KiB

17 years ago
  1. ##
  2. ## Based on Carp.pm from Perl 5.005_03.
  3. ## Last modified 12-Jun-2001 by Steffen Beyer.
  4. ## Should be reasonably backwards compatible.
  5. ##
  6. ## This module is free software and can
  7. ## be used, modified and redistributed
  8. ## under the same terms as Perl itself.
  9. ##
  10. @DB::args = (); # Avoid warning "used only once" in Perl 5.003
  11. package Carp::Clan;
  12. use strict;
  13. use vars qw( $MaxEvalLen $MaxArgLen $MaxArgNums $Verbose $VERSION );
  14. use overload ();
  15. # Original comments by Andy Wardley <abw@kfs.org> 09-Apr-1998.
  16. # The $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how
  17. # the eval text and function arguments should be formatted when printed.
  18. $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
  19. $MaxArgLen = 64; # How much of each argument to print. 0 = all.
  20. $MaxArgNums = 8; # How many arguments to print. 0 = all.
  21. $Verbose = 0; # If true then make _shortmsg call _longmsg instead.
  22. $VERSION = '5.8';
  23. # _longmsg() crawls all the way up the stack reporting on all the function
  24. # calls made. The error string, $error, is originally constructed from the
  25. # arguments passed into _longmsg() via confess(), cluck() or _shortmsg().
  26. # This gets appended with the stack trace messages which are generated for
  27. # each function call on the stack.
  28. sub _longmsg {
  29. return (@_) if ( ref $_[0] );
  30. local $_; # Protect surrounding program - just in case...
  31. my ( $pack, $file, $line, $sub, $hargs, $eval, $require, @parms, $push );
  32. my $error = join( '', @_ );
  33. my $msg = '';
  34. my $i = 0;
  35. while (
  36. do {
  37. {
  38. package DB;
  39. ( $pack, $file, $line, $sub, $hargs, undef, $eval, $require )
  40. = caller( $i++ )
  41. }
  42. }
  43. )
  44. {
  45. next if ( $pack eq 'Carp::Clan' );
  46. if ( $error eq '' ) {
  47. if ( defined $eval ) {
  48. $eval =~ s/([\\\'])/\\$1/g unless ($require); # Escape \ and '
  49. $eval
  50. =~ s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
  51. substr( $eval, $MaxEvalLen ) = '...'
  52. if ( $MaxEvalLen && length($eval) > $MaxEvalLen );
  53. if ($require) { $sub = "require $eval"; }
  54. else { $sub = "eval '$eval'"; }
  55. }
  56. elsif ( $sub eq '(eval)' ) { $sub = 'eval {...}'; }
  57. else {
  58. @parms = ();
  59. if ($hargs) {
  60. $push = 0;
  61. @parms = @DB::args
  62. ; # We may trash some of the args so we take a copy
  63. if ( $MaxArgNums and @parms > $MaxArgNums ) {
  64. $#parms = $MaxArgNums;
  65. pop(@parms);
  66. $push = 1;
  67. }
  68. for (@parms) {
  69. if ( defined $_ ) {
  70. if ( ref $_ ) {
  71. $_ = overload::StrVal($_);
  72. }
  73. else {
  74. unless ( /^-?\d+(?:\.\d+(?:[eE][+-]\d+)?)?$/
  75. ) # Looks numeric
  76. {
  77. s/([\\\'])/\\$1/g; # Escape \ and '
  78. s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
  79. substr( $_, $MaxArgLen ) = '...'
  80. if ( $MaxArgLen
  81. and length($_) > $MaxArgLen );
  82. $_ = "'$_'";
  83. }
  84. }
  85. }
  86. else { $_ = 'undef'; }
  87. }
  88. push( @parms, '...' ) if ($push);
  89. }
  90. $sub .= '(' . join( ', ', @parms ) . ')';
  91. }
  92. if ( $msg eq '' ) { $msg = "$sub called"; }
  93. else { $msg .= "\t$sub called"; }
  94. }
  95. else {
  96. if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
  97. else { $msg = "$sub: $error"; }
  98. }
  99. $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
  100. $error = '';
  101. }
  102. $msg ||= $error;
  103. $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
  104. $msg;
  105. }
  106. # _shortmsg() is called by carp() and croak() to skip all the way up to
  107. # the top-level caller's package and report the error from there. confess()
  108. # and cluck() generate a full stack trace so they call _longmsg() to
  109. # generate that. In verbose mode _shortmsg() calls _longmsg() so you
  110. # always get a stack trace.
  111. sub _shortmsg {
  112. my $pattern = shift;
  113. my $verbose = shift;
  114. return (@_) if ( ref $_[0] );
  115. goto &_longmsg if ( $Verbose or $verbose );
  116. my ( $pack, $file, $line, $sub );
  117. my $error = join( '', @_ );
  118. my $msg = '';
  119. my $i = 0;
  120. while ( ( $pack, $file, $line, $sub ) = caller( $i++ ) ) {
  121. next if ( $pack eq 'Carp::Clan' or $pack =~ /$pattern/ );
  122. if ( $error eq '' ) { $msg = "$sub() called"; }
  123. elsif ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
  124. else { $msg = "$sub: $error"; }
  125. $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
  126. $msg =~ tr/\0//d
  127. ; # Circumvent die's incorrect handling of NUL characters
  128. return $msg;
  129. }
  130. goto &_longmsg;
  131. }
  132. # The following four functions call _longmsg() or _shortmsg() depending on
  133. # whether they should generate a full stack trace (confess() and cluck())
  134. # or simply report the caller's package (croak() and carp()), respectively.
  135. # confess() and croak() die, carp() and cluck() warn.
  136. # Following code kept for calls with fully qualified subroutine names:
  137. # (For backward compatibility with the original Carp.pm)
  138. sub croak {
  139. my $callpkg = caller(0);
  140. my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
  141. die _shortmsg( $pattern, 0, @_ );
  142. }
  143. sub confess { die _longmsg(@_); }
  144. sub carp {
  145. my $callpkg = caller(0);
  146. my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
  147. warn _shortmsg( $pattern, 0, @_ );
  148. }
  149. sub cluck { warn _longmsg(@_); }
  150. # The following method imports a different closure for every caller.
  151. # I.e., different modules can use this module at the same time
  152. # and in parallel and still use different patterns.
  153. sub import {
  154. my $pkg = shift;
  155. my $callpkg = caller(0);
  156. my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
  157. my $verbose = 0;
  158. my $item;
  159. my $file;
  160. for $item (@_) {
  161. if ( $item =~ /^\d/ ) {
  162. if ( $VERSION < $item ) {
  163. $file = "$pkg.pm";
  164. $file =~ s!::!/!g;
  165. $file = $INC{$file};
  166. die _shortmsg( '^:::', 0,
  167. "$pkg $item required--this is only version $VERSION ($file)"
  168. );
  169. }
  170. }
  171. elsif ( $item =~ /^verbose$/i ) { $verbose = 1; }
  172. else { $pattern = $item; }
  173. }
  174. # Speed up pattern matching in Perl versions >= 5.005:
  175. # (Uses "eval ''" because qr// is a syntax error in previous Perl versions)
  176. if ( $] >= 5.005 ) {
  177. eval '$pattern = qr/$pattern/;';
  178. }
  179. else {
  180. eval { $pkg =~ /$pattern/; };
  181. }
  182. if ($@) {
  183. $@ =~ s/\s+$//;
  184. $@ =~ s/\s+at\s.+$//;
  185. die _shortmsg( '^:::', 0, $@ );
  186. }
  187. {
  188. local ($^W) = 0;
  189. no strict "refs";
  190. *{"${callpkg}::croak"}
  191. = sub { die _shortmsg( $pattern, $verbose, @_ ); };
  192. *{"${callpkg}::confess"} = sub { die _longmsg(@_); };
  193. *{"${callpkg}::carp"}
  194. = sub { warn _shortmsg( $pattern, $verbose, @_ ); };
  195. *{"${callpkg}::cluck"} = sub { warn _longmsg(@_); };
  196. }
  197. }
  198. 1;