This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add cautionary comment to .t
[perl5.git] / lib / assert.pl
CommitLineData
870abcc9
S
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#
f1ca563b
LW
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
20sub assert {
859172fe 21 &panic("ASSERTION BOTCHED: $_[0]",$@) unless eval $_[0];
f1ca563b
LW
22}
23
24sub panic {
748a9306
LW
25 package DB;
26
f1ca563b
LW
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
748a9306
LW
35 local $_;
36 my $i;
37 my ($p,$f,$l,$s,$h,$a,@a,@frames);
f1ca563b 38 for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
748a9306 39 @a = @args;
f1ca563b
LW
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) . ')' : '';
748a9306 53 push(@frames, "$w&$s$a from file $f line $l\n");
f1ca563b 54 }
748a9306
LW
55 for ($i=0; $i <= $#frames; $i++) {
56 print $frames[$i];
f1ca563b
LW
57 }
58 exit 1;
59}
60
611;