home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / assert.pl < prev    next >
Text File  |  1997-05-18  |  1KB  |  64 lines

  1. # assert.pl
  2. # tchrist@convex.com (Tom Christiansen)
  3. # Usage:
  4. #     &assert('@x > @y');
  5. #     &assert('$var > 10', $var, $othervar, @various_info);
  6. # That is, if the first expression evals false, we blow up.  The
  7. # rest of the args, if any, are nice to know because they will
  8. # be printed out by &panic, which is just the stack-backtrace
  9. # routine shamelessly borrowed from the perl debugger.
  10.  
  11. sub assert {
  12.     &panic("ASSERTION BOTCHED: $_[$[]",$@) unless eval $_[$[];
  13.  
  14. sub panic {
  15.     package DB;
  16.  
  17.     select(STDERR);
  18.  
  19.     if ($^O eq 'MacOS') {
  20.         print "\n# Panic: @_\n";
  21.     } else {
  22.         print "\npanic: @_\n";
  23.     }
  24.  
  25.     exit 1 if $] <= 4.003;  # caller broken
  26.  
  27.     # stack traceback gratefully borrowed from perl debugger
  28.  
  29.     local $_;
  30.     my $i;
  31.     my ($p,$f,$l,$s,$h,$a,@a,@frames);
  32.     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  33.     @a = @args;
  34.     for (@a) {
  35.         if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  36.         $_ = sprintf("%s",$_);
  37.         }
  38.         else {
  39.         s/'/\\'/g;
  40.         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  41.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  42.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  43.         }
  44.     }
  45.     $w = $w ? '@ = ' : '$ = ';
  46.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  47.     if ($^O eq 'MacOS') {
  48.         push(@frames, "# $w&$s$a\nFile \'$f\'; Line $l\n");
  49.     } else {
  50.         push(@frames, "$w&$s$a from file $f line $l\n");
  51.     }
  52.     }
  53.     for ($i=0; $i <= $#frames; $i++) {
  54.     print $frames[$i];
  55.     }
  56.     exit 1;
  57.  
  58. 1;
  59.