This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
2c2073c7eaa9166f315e4bfc09319c5d2b08609e
[perl5.git] / lib / assert.pl
1 #
2 # This library is no longer being maintained, and is included for backward
3 # compatibility with Perl 4 programs which may require it.
4 # This legacy library is deprecated and will be removed in a future
5 # release of perl.
6 #
7 # assert.pl
8 # tchrist@convex.com (Tom Christiansen)
9
10 # Usage:
11
12 #     &assert('@x > @y');
13 #     &assert('$var > 10', $var, $othervar, @various_info);
14
15 # That is, if the first expression evals false, we blow up.  The
16 # rest of the args, if any, are nice to know because they will
17 # be printed out by &panic, which is just the stack-backtrace
18 # routine shamelessly borrowed from the perl debugger.
19
20 sub assert {
21     &panic("ASSERTION BOTCHED: $_[$[]",$@) unless eval $_[$[];
22
23
24 sub panic {
25     package DB;
26
27     select(STDERR);
28
29     print "\npanic: @_\n";
30
31     exit 1 if $] <= 4.003;  # caller broken
32
33     # stack traceback gratefully borrowed from perl debugger
34
35     local $_;
36     my $i;
37     my ($p,$f,$l,$s,$h,$a,@a,@frames);
38     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
39         @a = @args;
40         for (@a) {
41             if (/^StB\000/ && length($_) == length($_main{'_main'})) {
42                 $_ = sprintf("%s",$_);
43             }
44             else {
45                 s/'/\\'/g;
46                 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
47                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
48                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
49             }
50         }
51         $w = $w ? '@ = ' : '$ = ';
52         $a = $h ? '(' . join(', ', @a) . ')' : '';
53         push(@frames, "$w&$s$a from file $f line $l\n");
54     }
55     for ($i=0; $i <= $#frames; $i++) {
56         print $frames[$i];
57     }
58     exit 1;
59
60
61 1;