Commit | Line | Data |
---|---|---|
f1ca563b LW |
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 | ||
d94ee62e S |
14 | warn( "The 'assert.pl' legacy library is deprecated and will be" |
15 | . " removed in the next major release of perl." ); | |
16 | ||
f1ca563b | 17 | sub assert { |
79072805 | 18 | &panic("ASSERTION BOTCHED: $_[$[]",$@) unless eval $_[$[]; |
f1ca563b LW |
19 | } |
20 | ||
21 | sub panic { | |
748a9306 LW |
22 | package DB; |
23 | ||
f1ca563b LW |
24 | select(STDERR); |
25 | ||
26 | print "\npanic: @_\n"; | |
27 | ||
28 | exit 1 if $] <= 4.003; # caller broken | |
29 | ||
30 | # stack traceback gratefully borrowed from perl debugger | |
31 | ||
748a9306 LW |
32 | local $_; |
33 | my $i; | |
34 | my ($p,$f,$l,$s,$h,$a,@a,@frames); | |
f1ca563b | 35 | for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) { |
748a9306 | 36 | @a = @args; |
f1ca563b LW |
37 | for (@a) { |
38 | if (/^StB\000/ && length($_) == length($_main{'_main'})) { | |
39 | $_ = sprintf("%s",$_); | |
40 | } | |
41 | else { | |
42 | s/'/\\'/g; | |
43 | s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/; | |
44 | s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg; | |
45 | s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg; | |
46 | } | |
47 | } | |
48 | $w = $w ? '@ = ' : '$ = '; | |
49 | $a = $h ? '(' . join(', ', @a) . ')' : ''; | |
748a9306 | 50 | push(@frames, "$w&$s$a from file $f line $l\n"); |
f1ca563b | 51 | } |
748a9306 LW |
52 | for ($i=0; $i <= $#frames; $i++) { |
53 | print $frames[$i]; | |
f1ca563b LW |
54 | } |
55 | exit 1; | |
56 | } | |
57 | ||
58 | 1; |