11 eval 'print "ok 1\n";';
13 if ($@ eq '') {print "ok 2\n";} else {print "not ok 2\n";}
15 eval "\$foo\n = # this is a comment\n'ok 3';";
18 eval "\$foo\n = # this is a comment\n'ok 4\n';";
22 $foo =;'; # this tests for a call through yyerror()
23 if ($@ =~ /line 2/) {print "ok 5\n";} else {print "not ok 5\n";}
25 print eval '$foo = /'; # this tests for a call through fatal()
26 if ($@ =~ /Search/) {print "ok 6\n";} else {print "not ok 6\n";}
28 print eval '"ok 7\n";';
30 # calculate a factorial with recursive evals
33 $fact = 'if ($foo <= 1) {1;} else {push(@x,$foo--); (eval $fact) * pop(@x);}';
35 if ($ans == 120) {print "ok 8\n";} else {print "not ok 8\n";}
38 $fact = 'local($foo)=$foo; $foo <= 1 ? 1 : $foo-- * (eval $fact);';
40 if ($ans == 120) {print "ok 9\n";} else {print "not ok 9 $ans\n";}
42 my $tempfile = tempfile();
43 open(try,'>',$tempfile);
44 print try 'print "ok 10\n";',"\n";
47 do "./$tempfile"; print $@;
49 # Test the singlequoted eval optimizer
53 eval 'print "ok ", $i++, "\n"';
60 } || print "ok 15\n$@";
62 # check whether eval EXPR determines value of EXPR correctly
67 print "@b" eq '4' ? "ok 17\n" : "not ok 17\n";
68 print $@ ? "not ok 18\n" : "ok 18\n";
70 my $a = q[defined(wantarray) ? (wantarray ? ($b='A') : ($b='S')) : ($b='V')];
73 print "@a" eq 'A' ? "ok 19\n" : "# $b\nnot ok 19\n";
74 print $b eq 'A' ? "ok 20\n" : "# $b\nnot ok 20\n";
76 print $b eq 'S' ? "ok 21\n" : "# $b\nnot ok 21\n";
78 print $b eq 'V' ? "ok 22\n" : "# $b\nnot ok 22\n";
83 print eval('"$b"') eq $b ? "ok 23\n" : "not ok 23\n";
91 print eval('"$b"') eq $b ? "ok 24\n" : "not ok 24\n";
96 # check navigation of multiple eval boundaries to find lexicals
99 eval <<'EOT'; die if $@;
100 print "# $x\n"; # clone into eval's pad
102 eval $_[0]; die if $@;
105 do_eval1('print "ok $x\n"');
107 do_eval1('eval q[print "ok $x\n"]');
109 do_eval1('sub { print "# $x\n"; eval q[print "ok $x\n"] }->()');
112 # calls from within eval'' should clone outer lexicals
114 eval <<'EOT'; die if $@;
116 eval $_[0]; die if $@;
118 do_eval2('print "ok $x\n"');
120 do_eval2('eval q[print "ok $x\n"]');
122 do_eval2('sub { print "# $x\n"; eval q[print "ok $x\n"] }->()');
126 # calls outside eval'' should NOT clone lexicals from called context
128 $main::ok = 'not ok';
130 eval <<'EOT'; die if $@;
133 eval $_[0]; die if $@;
138 do_eval3('print "$ok ' . $x++ . '\n"');
139 do_eval3('eval q[print "$ok ' . $x++ . '\n"]');
140 do_eval3('sub { eval q[print "$ok ' . $x++ . '\n"] }->()');
143 # can recursive subroutine-call inside eval'' see its own lexicals?
148 eval 'print "# level $l\n"; recurse($l);';
156 local $SIG{__WARN__} = sub { die "not ok $x\n" if $_[0] =~ /^Deep recurs/ };
161 # do closures created within eval bind correctly?
170 create_closure("ok $x\n")->();
173 # does lexical search terminate correctly at subroutine boundary?
174 $main::r = "ok $x\n";
175 sub terminal { eval 'print $r' }
177 my $r = "not ok $x\n";
182 # Have we cured panic which occurred with require/eval in die handler ?
183 $SIG{__DIE__} = sub { eval {1}; die shift };
184 eval { die "ok ".$x++,"\n" };
187 # does scalar eval"" pop stack correctly?
189 my $c = eval "(1,2)x10";
190 print $c eq '2222222222' ? "ok $x\n" : "# $c\nnot ok $x\n";
194 # return from eval {} should clear $@ correctly
198 print "# eval { return } test\n";
199 return; # removing this changes behavior
210 print "# eval q{ return } test\n";
211 return; # removing this changes behavior
218 # Check that eval catches bad goto calls
219 # (BUG ID 20010305.003)
223 print ($@ ? "ok 41\n" : "not ok 41\n");
226 foo: print "not ok 41\n";
227 print "# jumped into foreach\n";
230 print "not ok 41\n" if $@;
233 # Make sure that "my $$x" is forbidden
237 print $@ ? "ok 42\n" : "not ok 42\n";
239 print $@ ? "ok 43\n" : "not ok 43\n";
241 print $@ ? "ok 44\n" : "not ok 44\n";
243 print $@ ? "ok 45\n" : "not ok 45\n";
246 # [ID 20020623.002] eval "" doesn't clear $@
250 print length($@) ? "not ok 46\t# \$\@ = '$@'\n" : "ok 46\n";
253 # DAPM Nov-2002. Perl should now capture the full lexical context during
261 eval q{ print eval '$zzz' == 1 ? 'ok' : 'not ok', " $_[0]\n"}
264 { my $zzz = 2; fred1(48) }
269 print eval('$zzz') == 1 ? 'ok' : 'not ok', " $_[0]\n";
273 { my $zzz = 2; fred2(50) }
275 # sort() starts a new context stack. Make sure we can still find
276 # the lexically enclosing sub
281 { print eval('$zzz') == 2 ? 'ok' : 'not ok', " 51\n"; $a <=> $b }
286 # more recursion and lexical scope leak tests
295 return 0 if eval '$zzz' != 1;
296 return 0 if $yyy != 9;
297 return 0 if eval '$yyy' != 9;
298 return 0 if eval '$l' != $l;
299 return $l * fred3($l-1);
302 print $r == 120 ? 'ok' : 'not ok', " 52\n";
304 print $r == 120 ? 'ok' : 'not ok', " 53\n";
306 eval '$r = fred3(5)';
307 print $r == 120 ? 'ok' : 'not ok', " 54\n";
309 { my $yyy = 4; my $zzz = 5; my $l = 6; $r = eval 'fred3(5)' };
310 print $r == 120 ? 'ok' : 'not ok', " 55\n";
313 print $r == 120 ? 'ok' : 'not ok', " 56\n";
315 print $r == 120 ? 'ok' : 'not ok', " 57\n";
318 print $r == 120 ? 'ok' : 'not ok', " 58\n";
320 { my $yyy = 4; my $zzz = 5; my $l = 6; $r = eval 'fred3(5)' };
321 print $r == 120 ? 'ok' : 'not ok', " 59\n";
323 # check that goto &sub within evals doesn't leak lexical scope
330 print +($zzz == 3 && eval '$zzz' == 3) ? 'ok' : 'not ok', " $test\n";
332 print eval '$yyy' == 2 ? 'ok' : 'not ok', " $test\n";
340 print +($zzz == 4 && eval '$zzz' == 4) ? 'ok' : 'not ok', " $test\n";
342 print eval '$yyy' == 2 ? 'ok' : 'not ok', " $test\n";
349 { my $yyy = 88; my $zzz = 99; fred5(); }
350 eval q{ my $yyy = 888; my $zzz = 999; fred5(); };
352 # [perl #9728] used to dump core
354 $eval = eval 'sub { eval "sub { %S }" }';
360 # evals that appear in the DB package should see the lexical scope of the
361 # thing outside DB that called them (usually the debugged code), rather
362 # than the usual surrounding scope
368 sub db1 { $x; eval '$x' }
369 sub DB::db2 { $x; eval '$x' }
371 sub db3 { eval '$x' }
372 sub DB::db4 { eval '$x' }
373 sub db5 { my $x=4; eval '$x' }
375 sub db6 { my $x=4; eval '$x' }
379 print db1() == 2 ? 'ok' : 'not ok', " $test\n"; $test++;
380 print DB::db2() == 2 ? 'ok' : 'not ok', " $test\n"; $test++;
381 print DB::db3() == 3 ? 'ok' : 'not ok', " $test\n"; $test++;
382 print DB::db4() == 3 ? 'ok' : 'not ok', " $test\n"; $test++;
383 print DB::db5() == 3 ? 'ok' : 'not ok', " $test\n"; $test++;
384 print db6() == 4 ? 'ok' : 'not ok', " $test\n"; $test++;
388 # [perl #19022] used to end up with shared hash warnings
389 # The program should generate no output, so anything we see is on stderr
390 my $got = runperl (prog => '$h{a}=1; foreach my $k (keys %h) {eval qq{\$k}}',
396 print "not ok $test\n";
397 _diag ("# Got '$got'\n");
401 # And a buggy way of fixing #19022 made this fail - $k became undef after the
402 # eval for a build with copy on write
406 foreach my $k (keys %h) {
407 if (defined $k and $k eq 'a') {
410 print "not $test # got ", _q ($k), "\n";
416 if (defined $k and $k eq 'a') {
419 print "not $test # got ", _q ($k), "\n";
425 sub Foo {} print Foo(eval {});
426 print "ok ",$test++," - #20798 (used to dump core)\n";
428 # check for context in string eval
431 sub context { defined(wantarray) ? (wantarray ? ($c='A') : ($c='S')) : ($c='V') }
433 my $code = q{ context() };
437 print "@r$c" eq 'AA' ? "ok " : "# '@r$c' ne 'AA'\nnot ok ", $test++, "\n";
439 print "$r$c" eq 'SS' ? "ok " : "# '$r$c' ne 'SS'\nnot ok ", $test++, "\n";
441 print $c eq 'V' ? "ok " : "# '$c' ne 'V'\nnot ok ", $test++, "\n";
444 # [perl #34682] escaping an eval with last could coredump or dup output
448 'sub A::TIEARRAY { L: { eval { last L } } } tie @a, A; warn qq(ok\n)',
451 print "not " unless $got eq "ok\n";
452 print "ok $test - eval and last\n"; $test++;
454 # eval undef should be the same as eval "" barring any warnings
459 print "not " unless $@ eq "";
460 print "ok $test # eval undef \n"; $test++;
466 print "not " unless $@ =~ /^syntax error/;
467 print "ok $test # eval syntax error, no warnings \n"; $test++;
471 # a syntax error in an eval called magically 9eg vie tie or overload)
472 # resulted in an assertion failure in S_docatch, since doeval had already
473 # poppedthe EVAL context due to the failure, but S_docatch expected the
474 # context to still be there.
479 sub STORE { eval '('; $ok = 1 }
480 sub TIESCALAR { bless [] }
485 print "not " unless $ok;
486 print "ok $test # eval docatch \n"; $test++;
490 # [perl #51370] eval { die "\x{a10d}" } followed by eval { 1 } did not reset
493 eval { die "\x{a10d}"; };
497 print "not " if ($@ ne "");
498 print "ok $test # length of \$@ after eval\n"; $test++;
500 print "not " if (length $@ != 0);
501 print "ok $test # length of \$@ after eval\n"; $test++;
503 # Check if eval { 1 }; compeltly resets $@
504 if (eval "use Devel::Peek; 1;") {
505 $tempfile = tempfile();
506 $outfile = tempfile();
507 open PROG, ">", $tempfile or die "Can't create test file";
508 my $prog = <<'END_EVAL_TEST';
513 open(SAVERR, ">&STDERR") or die "Can't dup STDERR: $!";
514 if (open(OUT, '>', '@@@@')) {
515 open(STDERR, ">&OUT") or die "Can't dup OUT: $!";
517 print STDERR "******\n";
518 eval { die "\x{a10d}"; };
522 open(STDERR, ">&SAVERR") or die "Can't restore STDERR: $!";
524 if (open(IN, '<', '@@@@')) {
527 my ($first, $second) = split (/\*\*\*\*\*\*\n/, $in, 2);
529 $ok = 1 if ($first eq $second);
535 $prog =~ s/\@\@\@\@/$outfile/g;
539 my $ok = runperl(progfile => $tempfile);
540 print "not " unless $ok;
541 print "ok $test # eval { 1 } completly resets \$@\n";
544 print "ok $test # skipped - eval { 1 } completly resets \$@\n";
548 # Test that "use feature" and other hint transmission in evals and s///ee
551 use feature qw(:5.10);
552 my $count_expected = ($^H & 0x20000) ? 2 : 1;
555 $s =~ s/a/$t = \%^H; qq( qq() );/ee;
556 print "not " if Internals::SvREFCNT(%$t) != $count_expected;
557 print "ok $test - RT 63110\n";