File Coverage

blib/lib/assert.pl
Criterion Covered Total %
statement 3 27 11.1
branch 0 12 0.0
condition 0 3 0.0
subroutine 1 3 33.3
pod n/a
total 4 45 8.8


line stmt bran cond sub pod time code
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              
14 1     1   1263 no warnings "ambiguous";
  1         2  
  1         435  
15              
16             sub assert {
17 0 0   0     &panic("ASSERTION BOTCHED: $_[0]",$@) unless eval $_[0];
18             }
19              
20             sub panic {
21             package DB;
22              
23 0     0     select(STDERR);
24              
25 0           print "\npanic: @_\n";
26              
27 0 0         exit 1 if $] <= 4.003; # caller broken
28              
29             # stack traceback gratefully borrowed from perl debugger
30              
31 0           local $_;
32 0           my $i;
33 0           my ($p,$f,$l,$s,$h,$a,@a,@frames);
34 0           for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
35 0           @a = @args;
36 0           for (@a) {
37 0 0 0       if (/^StB\000/ && length($_) == length($_main{'_main'})) {
38 0           $_ = sprintf("%s",$_);
39             }
40             else {
41 0           s/'/\\'/g;
42 0 0         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
43 0           s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  0            
44 0           s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  0            
45             }
46             }
47 0 0         $w = $w ? '@ = ' : '$ = ';
48 0 0         $a = $h ? '(' . join(', ', @a) . ')' : '';
49 0           push(@frames, "$w&$s$a from file $f line $l\n");
50             }
51 0           for ($i=0; $i <= $#frames; $i++) {
52 0           print $frames[$i];
53             }
54 0           exit 1;
55             }
56              
57             1;