This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid using pragmata in fold.t - use may not work yet.
[perl5.git] / t / comp / fold.t
1 #!./perl -w
2
3 require './test.pl';
4
5 # Uncomment this for testing, but don't leave it in for "production", as
6 # we've not yet verified that use works.
7 # use strict;
8
9 plan (13);
10
11 # Historically constant folding was performed by evaluating the ops, and if
12 # they threw an exception compilation failed. This was seen as buggy, because
13 # even illegal constants in unreachable code would cause failure. So now
14 # illegal expressions are reported at runtime, if the expression is reached,
15 # making constant folding consistent with many other languages, and purely an
16 # optimisation rather than a behaviour change.
17
18
19 my $a;
20 $a = eval '$b = 0/0 if 0; 3';
21 is ($a, 3);
22 is ($@, "");
23
24 my $b = 0;
25 $a = eval 'if ($b) {return sqrt -3} 3';
26 is ($a, 3);
27 is ($@, "");
28
29 $a = eval q{
30         $b = eval q{if ($b) {return log 0} 4};
31         is ($b, 4);
32         is ($@, "");
33         5;
34 };
35 is ($a, 5);
36 is ($@, "");
37
38 # warn and die hooks should be disabled during constant folding
39
40 {
41     my $c = 0;
42     local $SIG{__WARN__} = sub { $c++   };
43     local $SIG{__DIE__}  = sub { $c+= 2 };
44     eval q{
45         is($c, 0, "premature warn/die: $c");
46         my $x = "a"+5;
47         is($c, 1, "missing warn hook");
48         is($x, 5, "a+5");
49         $c = 0;
50         $x = 1/0;
51     };
52     like ($@, qr/division/, "eval caught division");
53     is($c, 2, "missing die hook");
54 }