home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Carp.pm < prev    next >
Text File  |  1997-05-23  |  4KB  |  154 lines

  1. package Carp;
  2.  
  3. =head1 NAME
  4.  
  5. carp - warn of errors (from perspective of caller)
  6.  
  7. croak - die of errors (from perspective of caller)
  8.  
  9. confess - die of errors with stack backtrace
  10.  
  11. =head1 SYNOPSIS
  12.  
  13.     use Carp;
  14.     croak "We're outta here!";
  15.  
  16. =head1 DESCRIPTION
  17.  
  18. The Carp routines are useful in your own modules because
  19. they act like die() or warn(), but report where the error
  20. was in the code they were called from.  Thus if you have a 
  21. routine Foo() that has a carp() in it, then the carp() 
  22. will report the error as occurring where Foo() was called, 
  23. not where carp() was called.
  24.  
  25. =cut
  26.  
  27. # This package implements handy routines for modules that wish to throw
  28. # exceptions outside of the current package.
  29.  
  30. $CarpLevel = 0;        # How many extra package levels to skip on carp.
  31. $MaxEvalLen = 0;    # How much eval '...text...' to show. 0 = all.
  32. $MaxArgLen = 64;        # How much of each argument to print. 0 = all.
  33. $MaxArgNums = 8;        # How many arguments to print. 0 = all.
  34.  
  35. require Exporter;
  36. @ISA = Exporter;
  37. @EXPORT = qw(confess croak carp);
  38.  
  39. sub longmess {
  40.     my $error = join '', @_;
  41.     my $mess = "";
  42.     my $i = 1 + $CarpLevel;
  43.     my ($pack,$file,$line,$sub,$hargs,$eval,$require);
  44.     my (@a);
  45.     while (do { { package DB; @a = caller($i++) } } ) {
  46.       ($pack,$file,$line,$sub,$hargs,undef,$eval,$require) = @a;
  47.       $mess .= "# " if ($^O eq 'MacOS');
  48.     if ($error =~ m/\n$/) {
  49.         $mess .= $error;
  50.     } else {
  51.         if (defined $eval) {
  52.             if ($require) {
  53.             $sub = "require $eval";
  54.         } else {
  55.             $eval =~ s/([\\\'])/\\$1/g;
  56.             if ($MaxEvalLen && length($eval) > $MaxEvalLen) {
  57.             substr($eval,$MaxEvalLen) = '...';
  58.             }
  59.             $sub = "eval '$eval'";
  60.         }
  61.         } elsif ($sub eq '(eval)') {
  62.         $sub = 'eval {...}';
  63.         }
  64.         if ($hargs) {
  65.           @a = @DB::args;    # must get local copy of args
  66.           if ($MaxArgNums and @a > $MaxArgNums) {
  67.         $#a = $MaxArgNums;
  68.         $a[$#a] = "...";
  69.           }
  70.           for (@a) {
  71.         $_ = "undef", next unless defined $_;
  72.         if (ref $_) {
  73.           $_ .= '';
  74.           s/'/\\'/g;
  75.         }
  76.         else {
  77.           s/'/\\'/g;
  78.           substr($_,$MaxArgLen) = '...'
  79.             if $MaxArgLen and $MaxArgLen < length;
  80.         }
  81.         $_ = "'$_'" unless /^-?[\d.]+$/;
  82.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  83.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  84.           }
  85.           $sub .= '(' . join(', ', @a) . ')';
  86.         }
  87.         $mess .= "\t$sub " if $error eq "called";
  88.         if ($^O eq 'MacOS') {
  89.             $mess .= "$error\nFile \'$file\'; Line $line\n";
  90.         } else {
  91.             $mess .= "$error at $file line $line\n";
  92.         }
  93.     }
  94.     $error = "called";
  95.     }
  96.     # this kludge circumvents die's incorrect handling of NUL
  97.     my $msg = \($mess || $error);
  98.     $$msg =~ tr/\0//d;
  99.     $$msg;
  100. }
  101.  
  102. sub shortmess {    # Short-circuit &longmess if called via multiple packages
  103.     my $error = join '', @_;
  104.     my ($prevpack) = caller(1);
  105.     my $extra = $CarpLevel;
  106.     my $i = 2;
  107.     my ($pack,$file,$line);
  108.     my %isa = ($prevpack,1);
  109.  
  110.     @isa{@{"${prevpack}::ISA"}} = ()
  111.     if(defined @{"${prevpack}::ISA"});
  112.  
  113.     while (($pack,$file,$line) = caller($i++)) {
  114.     if(defined @{$pack . "::ISA"}) {
  115.         my @i = @{$pack . "::ISA"};
  116.         my %i;
  117.         @i{@i} = ();
  118.         @isa{@i,$pack} = ()
  119.         if(exists $i{$prevpack} || exists $isa{$pack});
  120.     }
  121.  
  122.     next
  123.         if(exists $isa{$pack});
  124.  
  125.     if ($extra-- > 0) {
  126.         %isa = ($pack,1);
  127.         @isa{@{$pack . "::ISA"}} = ()
  128.         if(defined @{$pack . "::ISA"});
  129.     }
  130.     else {
  131.         # this kludge circumvents die's incorrect handling of NUL
  132.         my($msg);
  133.         if ($^O eq 'MacOS') {
  134.             $msg .= "$error\nFile \'$file\'; Line $line\n";
  135.         } else {
  136.             $msg .= "$error at $file line $line\n";
  137.         }
  138.         $msg =~ tr/\0//d;
  139.         return $msg;
  140.     }
  141.     }
  142.     continue {
  143.     $prevpack = $pack;
  144.     }
  145.  
  146.     goto &longmess;
  147. }
  148.  
  149. sub confess { die longmess @_; }
  150. sub croak { die shortmess @_; }
  151. sub carp { warn shortmess @_; }
  152.  
  153. 1;
  154.