xref: /openbsd-src/gnu/usr.bin/perl/t/comp/fold.t (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1#!./perl
2
3BEGIN {
4    chdir 't';
5    @INC = '../lib';
6    require './test.pl';
7}
8use strict;
9use warnings;
10
11plan (13);
12
13# Historically constant folding was performed by evaluating the ops, and if
14# they threw an exception compilation failed. This was seen as buggy, because
15# even illegal constants in unreachable code would cause failure. So now
16# illegal expressions are reported at runtime, if the expression is reached,
17# making constant folding consistent with many other languages, and purely an
18# optimisation rather than a behaviour change.
19
20
21my $a;
22$a = eval '$b = 0/0 if 0; 3';
23is ($a, 3);
24is ($@, "");
25
26my $b = 0;
27$a = eval 'if ($b) {return sqrt -3} 3';
28is ($a, 3);
29is ($@, "");
30
31$a = eval q{
32	$b = eval q{if ($b) {return log 0} 4};
33 	is ($b, 4);
34	is ($@, "");
35	5;
36};
37is ($a, 5);
38is ($@, "");
39
40# warn and die hooks should be disabled during constant folding
41
42{
43    my $c = 0;
44    local $SIG{__WARN__} = sub { $c++   };
45    local $SIG{__DIE__}  = sub { $c+= 2 };
46    eval q{
47	is($c, 0, "premature warn/die: $c");
48	my $x = "a"+5;
49	is($c, 1, "missing warn hook");
50	is($x, 5, "a+5");
51	$c = 0;
52	$x = 1/0;
53    };
54    like ($@, qr/division/, "eval caught division");
55    is($c, 2, "missing die hook");
56}
57