home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / assert.pl < prev    next >
Text File  |  1997-11-25  |  1KB  |  56 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.     print "\npanic: @_\n";
  20.  
  21.     exit 1 if $] <= 4.003;  # caller broken
  22.  
  23.     # stack traceback gratefully borrowed from perl debugger
  24.  
  25.     local $_;
  26.     my $i;
  27.     my ($p,$f,$l,$s,$h,$a,@a,@frames);
  28.     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  29.     @a = @args;
  30.     for (@a) {
  31.         if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  32.         $_ = sprintf("%s",$_);
  33.         }
  34.         else {
  35.         s/'/\\'/g;
  36.         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  37.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  38.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  39.         }
  40.     }
  41.     $w = $w ? '@ = ' : '$ = ';
  42.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  43.     push(@frames, "$w&$s$a from file $f line $l\n");
  44.     }
  45.     for ($i=0; $i <= $#frames; $i++) {
  46.     print $frames[$i];
  47.     }
  48.     exit 1;
  49.  
  50. 1;
  51.