This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid literal control characters in change#3039
[perl5.git] / t / op / eval.t
CommitLineData
a559c259
LW
1#!./perl
2
2680586e 3print "1..30\n";
a559c259
LW
4
5eval 'print "ok 1\n";';
6
7if ($@ eq '') {print "ok 2\n";} else {print "not ok 2\n";}
8
9eval "\$foo\n = # this is a comment\n'ok 3';";
10print $foo,"\n";
11
12eval "\$foo\n = # this is a comment\n'ok 4\n';";
13print $foo;
14
378cc40b 15print eval '
79072805 16$foo =;'; # this tests for a call through yyerror()
a559c259
LW
17if ($@ =~ /line 2/) {print "ok 5\n";} else {print "not ok 5\n";}
18
378cc40b 19print eval '$foo = /'; # this tests for a call through fatal()
a559c259 20if ($@ =~ /Search/) {print "ok 6\n";} else {print "not ok 6\n";}
378cc40b
LW
21
22print eval '"ok 7\n";';
23
24# calculate a factorial with recursive evals
25
26$foo = 5;
27$fact = 'if ($foo <= 1) {1;} else {push(@x,$foo--); (eval $fact) * pop(@x);}';
28$ans = eval $fact;
29if ($ans == 120) {print "ok 8\n";} else {print "not ok 8\n";}
30
31$foo = 5;
a687059c 32$fact = 'local($foo)=$foo; $foo <= 1 ? 1 : $foo-- * (eval $fact);';
378cc40b
LW
33$ans = eval $fact;
34if ($ans == 120) {print "ok 9\n";} else {print "not ok 9 $ans\n";}
35
36open(try,'>Op.eval');
37print try 'print "ok 10\n"; unlink "Op.eval";',"\n";
38close try;
39
40do 'Op.eval'; print $@;
99b89507
LW
41
42# Test the singlequoted eval optimizer
43
44$i = 11;
45for (1..3) {
46 eval 'print "ok ", $i++, "\n"';
47}
48
49eval {
50 print "ok 14\n";
51 die "ok 16\n";
52 1;
53} || print "ok 15\n$@";
54
c7cc6f1c
GS
55# check whether eval EXPR determines value of EXPR correctly
56
57{
58 my @a = qw(a b c d);
59 my @b = eval @a;
60 print "@b" eq '4' ? "ok 17\n" : "not ok 17\n";
61 print $@ ? "not ok 18\n" : "ok 18\n";
62
63 my $a = q[defined(wantarray) ? (wantarray ? ($b='A') : ($b='S')) : ($b='V')];
64 my $b;
65 @a = eval $a;
66 print "@a" eq 'A' ? "ok 19\n" : "# $b\nnot ok 19\n";
67 print $b eq 'A' ? "ok 20\n" : "# $b\nnot ok 20\n";
68 $_ = eval $a;
69 print $b eq 'S' ? "ok 21\n" : "# $b\nnot ok 21\n";
70 eval $a;
71 print $b eq 'V' ? "ok 22\n" : "# $b\nnot ok 22\n";
fc360e46
AB
72
73 $b = 'wrong';
74 $x = sub {
75 my $b = "right";
76 print eval('"$b"') eq $b ? "ok 23\n" : "not ok 23\n";
77 };
78 &$x();
c7cc6f1c 79}
155fc61f
GS
80
81my $b = 'wrong';
82my $X = sub {
83 my $b = "right";
84 print eval('"$b"') eq $b ? "ok 24\n" : "not ok 24\n";
85};
86&$X();
87
88
89# check navigation of multiple eval boundaries to find lexicals
90
91my $x = 25;
92eval <<'EOT'; die if $@;
93 sub do_eval {
94 eval $_[0]; die if $@;
95 }
96EOT
97do_eval('print "ok $x\n"');
98$x++;
99do_eval('eval q[print "ok $x\n"]');
100$x++;
101do_eval('sub { eval q[print "ok $x\n"] }->()');
102$x++;
6b35e009
GS
103
104# can recursive subroutine-call inside eval'' see its own lexicals?
105sub recurse {
106 my $l = shift;
107 if ($l < $x) {
108 ++$l;
109 eval 'print "# level $l\n"; recurse($l);';
110 die if $@;
111 }
112 else {
113 print "ok $l\n";
114 }
115}
116{
117 local $SIG{__WARN__} = sub { die "not ok $x\n" if $_[0] =~ /^Deep recurs/ };
118 recurse($x-5);
119}
33b8ce05
GS
120$x++;
121
122# do closures created within eval bind correctly?
123eval <<'EOT';
124 sub create_closure {
125 my $self = shift;
126 return sub {
127 print $self;
128 };
129 }
130EOT
131create_closure("ok $x\n")->();
2680586e
GS
132$x++;
133
134# does lexical search terminate correctly at subroutine boundary?
135$main::r = "ok $x\n";
136sub terminal { eval 'print $r' }
137{
138 my $r = "not ok $x\n";
139 eval 'terminal($r)';
140}
141$x++;
142