Commit | Line | Data |
---|---|---|
a5871da3 NC |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't'; | |
5 | @INC = '../lib'; | |
6 | require './test.pl'; | |
7 | } | |
8 | use strict; | |
9 | use warnings; | |
10 | ||
5f2d9966 | 11 | plan (13); |
a5871da3 NC |
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 | ||
5f2d9966 | 20 | |
a5871da3 NC |
21 | my $a; |
22 | $a = eval '$b = 0/0 if 0; 3'; | |
23 | is ($a, 3); | |
24 | is ($@, ""); | |
25 | ||
26 | my $b = 0; | |
27 | $a = eval 'if ($b) {return sqrt -3} 3'; | |
28 | is ($a, 3); | |
29 | is ($@, ""); | |
30 | ||
31 | $a = eval q{ | |
32 | $b = eval q{if ($b) {return log 0} 4}; | |
33 | is ($b, 4); | |
34 | is ($@, ""); | |
35 | 5; | |
36 | }; | |
37 | is ($a, 5); | |
38 | is ($@, ""); | |
39 | ||
5f2d9966 DM |
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 | } |