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