1N/A# assert.pl
1N/A# tchrist@convex.com (Tom Christiansen)
1N/A#
1N/A# Usage:
1N/A#
1N/A# &assert('@x > @y');
1N/A# &assert('$var > 10', $var, $othervar, @various_info);
1N/A#
1N/A# That is, if the first expression evals false, we blow up. The
1N/A# rest of the args, if any, are nice to know because they will
1N/A# be printed out by &panic, which is just the stack-backtrace
1N/A# routine shamelessly borrowed from the perl debugger.
1N/A
1N/Asub assert {
1N/A &panic("ASSERTION BOTCHED: $_[$[]",$@) unless eval $_[$[];
1N/A}
1N/A
1N/Asub panic {
1N/A package DB;
1N/A
1N/A select(STDERR);
1N/A
1N/A print "\npanic: @_\n";
1N/A
1N/A exit 1 if $] <= 4.003; # caller broken
1N/A
1N/A # stack traceback gratefully borrowed from perl debugger
1N/A
1N/A local $_;
1N/A my $i;
1N/A my ($p,$f,$l,$s,$h,$a,@a,@frames);
1N/A for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
1N/A @a = @args;
1N/A for (@a) {
1N/A if (/^StB\000/ && length($_) == length($_main{'_main'})) {
1N/A $_ = sprintf("%s",$_);
1N/A }
1N/A else {
1N/A s/'/\\'/g;
1N/A s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
1N/A s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
1N/A s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
1N/A }
1N/A }
1N/A $w = $w ? '@ = ' : '$ = ';
1N/A $a = $h ? '(' . join(', ', @a) . ')' : '';
1N/A push(@frames, "$w&$s$a from file $f line $l\n");
1N/A }
1N/A for ($i=0; $i <= $#frames; $i++) {
1N/A print $frames[$i];
1N/A }
1N/A exit 1;
1N/A}
1N/A
1N/A1;