This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move the require './test.pl' to the end of t/comp/hints.t
[perl5.git] / t / comp / fold.t
CommitLineData
2eaa2210
NC
1#!./perl -w
2
3require './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;
a5871da3 8
5f2d9966 9plan (13);
a5871da3
NC
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
5f2d9966 18
a5871da3
NC
19my $a;
20$a = eval '$b = 0/0 if 0; 3';
21is ($a, 3);
22is ($@, "");
23
24my $b = 0;
25$a = eval 'if ($b) {return sqrt -3} 3';
26is ($a, 3);
27is ($@, "");
28
29$a = eval q{
30 $b = eval q{if ($b) {return log 0} 4};
31 is ($b, 4);
32 is ($@, "");
33 5;
34};
35is ($a, 5);
36is ($@, "");
37
5f2d9966
DM
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}