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()
25 print eval '$foo = /'; # this tests for a call through fatal()
28 is scalar(eval '++'), undef, 'eval syntax error in scalar context';
29 is scalar(eval 'die'), undef, 'eval run-time error in scalar context';
30 is +()=eval '++', 0, 'eval syntax error in list context';
31 is +()=eval 'die', 0, 'eval run-time error in list context';
33 is(eval '"ok 7\n";', "ok 7\n");
36 $fact = 'if ($foo <= 1) {1;} else {push(@x,$foo--); (eval $fact) * pop(@x);}';
38 is($ans, 120, 'calculate a factorial with recursive evals');
41 $fact = 'local($foo)=$foo; $foo <= 1 ? 1 : $foo-- * (eval $fact);';
43 is($ans, 120, 'calculate a factorial with recursive evals');
45 my $curr_test = curr_test();
46 my $tempfile = tempfile();
47 open(try,'>',$tempfile);
48 print try 'print "ok $curr_test\n";',"\n";
51 do "./$tempfile"; print $@;
53 # Test the singlequoted eval optimizer
57 eval 'print "ok ", $i++, "\n"';
63 print "ok $curr_test\n";
64 die sprintf "ok %d\n", $curr_test + 2;
66 } || printf "ok %d\n$@", $curr_test + 1;
68 curr_test($curr_test + 3);
70 # check whether eval EXPR determines value of EXPR correctly
78 my $a = q[defined(wantarray) ? (wantarray ? ($b='A') : ($b='S')) : ($b='V')];
100 is(eval('"$b"'), $b);
105 # check navigation of multiple eval boundaries to find lexicals
108 eval <<'EOT'; die if $@;
109 print "# $x\n"; # clone into eval's pad
111 eval $_[0]; die if $@;
114 do_eval1('is($x, "aa")');
116 do_eval1('eval q[is($x, "ab")]');
118 do_eval1('sub { print "# $x\n"; eval q[is($x, "ac")] }->()');
121 # calls from within eval'' should clone outer lexicals
123 eval <<'EOT'; die if $@;
125 eval $_[0]; die if $@;
127 do_eval2('is($x, "ad")');
129 do_eval2('eval q[is($x, "ae")]');
131 do_eval2('sub { print "# $x\n"; eval q[is($x, "af")] }->()');
134 # calls outside eval'' should NOT clone lexicals from called context
136 $main::ok = 'not ok';
138 eval <<'EOT'; die if $@;
141 eval $_[0]; die if $@;
146 do_eval3('is($ok, q{ok})');
147 do_eval3('eval q[is($ok, q{ok})]');
148 do_eval3('sub { eval q[is($ok, q{ok})] }->()');
158 eval 'print "# level $l\n"; recurse($l);';
165 local $SIG{__WARN__} = sub { fail() if $_[0] =~ /^Deep recurs/ };
166 recurse(curr_test() - 5);
169 "recursive subroutine-call inside eval'' see its own lexicals");
181 is(create_closure("good")->(), "good",
182 'closures created within eval bind correctly');
185 sub terminal { eval '$r . q{!}' }
189 }, 'good!', 'lexical search terminates correctly at subroutine boundary');
192 # Have we cured panic which occurred with require/eval in die handler ?
193 local $SIG{__DIE__} = sub { eval {1}; die shift };
194 eval { die "wham_eth\n" };
195 is($@, "wham_eth\n");
199 my $c = eval "(1,2)x10";
200 is($c, '2222222222', 'scalar eval"" pops stack correctly');
203 # return from eval {} should clear $@ correctly
207 print "# eval { return } test\n";
208 return; # removing this changes behavior
210 is($@, '', 'return from eval {} should clear $@ correctly');
217 print "# eval q{ return } test\n";
218 return; # removing this changes behavior
220 is($@, '', 'return from eval "" should clear $@ correctly');
223 # Check that eval catches bad goto calls
224 # (BUG ID 20010305.003)
228 like($@, qr/Can't "goto" into the middle of a foreach loop/,
229 'eval catches bad goto calls');
232 foo: fail('jumped into foreach');
235 fail("Outer eval didn't execute the last");
239 # Make sure that "my $$x" is forbidden
242 foreach (qw($$x @$x %$x $$$x)) {
244 isnt($@, '', "my $_ is forbidden");
251 cmp_ok(length $@, '==', 0, '[ID 20020623.002] eval "" doesn\'t clear $@');
254 # DAPM Nov-2002. Perl should now capture the full lexical context during
262 eval q{ is(eval '$zzz', 1); }
265 { my $zzz = 2; fred1(48) }
274 { my $zzz = 2; fred2(50) }
276 # sort() starts a new context stack. Make sure we can still find
277 # the lexically enclosing sub
282 { is(eval('$zzz'), 2); $a <=> $b }
287 # more recursion and lexical scope leak tests
296 return 0 if eval '$zzz' != 1;
297 return 0 if $yyy != 9;
298 return 0 if eval '$yyy' != 9;
299 return 0 if eval '$l' != $l;
300 return $l * fred3($l-1);
307 eval '$r = fred3(5)';
310 { my $yyy = 4; my $zzz = 5; my $l = 6; $r = eval 'fred3(5)' };
321 { my $yyy = 4; my $zzz = 5; my $l = 6; $r = eval 'fred3(5)' };
324 # check that goto &sub within evals doesn't leak lexical scope
347 { my $yyy = 88; my $zzz = 99; fred5(); }
348 eval q{ my $yyy = 888; my $zzz = 999; fred5(); };
351 $eval = eval 'sub { eval "sub { %S }" }';
353 pass('[perl #9728] used to dump core');
356 # evals that appear in the DB package should see the lexical scope of the
357 # thing outside DB that called them (usually the debugged code), rather
358 # than the usual surrounding scope
363 sub db1 { $x; eval '$x' }
364 sub DB::db2 { $x; eval '$x' }
366 sub db3 { eval '$x' }
367 sub DB::db4 { eval '$x' }
368 sub db5 { my $x=4; eval '$x' }
370 sub db6 { my $x=4; eval '$x' }
382 # [perl #19022] used to end up with shared hash warnings
383 # The program should generate no output, so anything we see is on stderr
384 my $got = runperl (prog => '$h{a}=1; foreach my $k (keys %h) {eval qq{\$k}}',
388 # And a buggy way of fixing #19022 made this fail - $k became undef after the
389 # eval for a build with copy on write
393 foreach my $k (keys %h) {
402 sub Foo {} print Foo(eval {});
403 pass('#20798 (used to dump core)');
405 # check for context in string eval
408 sub context { defined(wantarray) ? (wantarray ? ($c='A') : ($c='S')) : ($c='V') }
410 my $code = q{ context() };
414 is("@r$c", 'AA', 'string eval list context');
416 is("$r$c", 'SS', 'string eval scalar context');
418 is("$c", 'V', 'string eval void context');
421 # [perl #34682] escaping an eval with last could coredump or dup output
425 'sub A::TIEARRAY { L: { eval { last L } } } tie @a, A; warn qq(ok\n)',
428 is($got, "ok\n", 'eval and last');
430 # eval undef should be the same as eval "" barring any warnings
435 is($@, "", 'eval undef');
441 like($@, qr/^syntax error/, 'eval syntax error, no warnings');
444 # a syntax error in an eval called magically (eg via tie or overload)
445 # resulted in an assertion failure in S_docatch, since doeval had already
446 # popped the EVAL context due to the failure, but S_docatch expected the
447 # context to still be there.
452 sub STORE { eval '('; $ok = 1 }
453 sub TIESCALAR { bless [] }
458 ::is($ok, 1, 'eval docatch');
461 # [perl #51370] eval { die "\x{a10d}" } followed by eval { 1 } did not reset
464 eval { die "\x{a10d}"; };
468 cmp_ok($@, 'eq', "", 'length of $@ after eval');
469 cmp_ok(length $@, '==', 0, 'length of $@ after eval');
471 # Check if eval { 1 }; completely resets $@
473 skip_if_miniperl('no dynamic loading on miniperl, no Devel::Peek', 2);
475 skip('Devel::Peek was not built', 2)
476 unless $Config::Config{extensions} =~ /\bDevel\/Peek\b/;
478 my $tempfile = tempfile();
479 open $prog, ">", $tempfile or die "Can't create test file";
480 print $prog <<'END_EVAL_TEST';
485 print STDERR "******\n";
486 eval { die "\x{a10d}"; };
490 print STDERR "******\n";
491 print STDERR "Done\n";
493 close $prog or die "Can't close $tempfile: $!";
494 my $got = runperl(progfile => $tempfile, stderr => 1);
495 my ($first, $second, $tombstone) = split (/\*\*\*\*\*\*\n/, $got);
497 is($tombstone, "Done\n", 'Program completed successfully');
499 $first =~ s/p?[NI]OK,//g;
500 s/ PV = 0x[0-9a-f]+/ PV = 0x/ foreach $first, $second;
501 s/ LEN = [0-9]+/ LEN = / foreach $first, $second;
502 # Dump may double newlines through pipes, though not files
503 # which is what this test used to use.
504 $second =~ s/ IV = 0\n\n/ IV = 0\n/ if $^O eq 'VMS';
506 is($second, $first, 'eval { 1 } completely resets $@');
509 # Test that "use feature" and other hint transmission in evals and s///ee
512 use feature qw(:5.10);
513 my $count_expected = ($^H & 0x20000) ? 2 : 1;
516 $s =~ s/a/$t = \%^H; qq( qq() );/ee;
517 is(Internals::SvREFCNT(%$t), $count_expected, 'RT 63110');
521 # test that the CV compiled for the eval is freed by checking that no additional
522 # reference to outside lexicals are made.
524 is(Internals::SvREFCNT($x), 1, "originally only 1 reference");
526 is(Internals::SvREFCNT($x), 1, "execution eval doesn't create new references");
529 fresh_perl_is(<<'EOP', "ok\n", undef, 'RT #70862');
535 fresh_perl_is(<<'EOP', "ok\n", undef, 'variant of RT #70862');
542 fresh_perl_is(<<'EOP', "ok\n", undef, 'related to RT #70862');
548 fresh_perl_is(<<'EOP', "ok\n", undef, 'related to RT #70862');
555 fresh_perl_is(<<'EOP', "ok\n", undef, 'segfault on syntax errors in block evals');
556 # localize the hits hash so the eval ends up with the pad offset of a copy of it in its targ
557 BEGIN { $^H |= 0x00020000 }
558 eval q{ eval { + } };
562 fresh_perl_is(<<'EOP', "ok\n", undef, 'assert fail on non-string in Perl_lex_start');
563 use overload '""' => sub { '1;' };
570 eval 'my $do_something_with = $k';
573 "string eval leaves readonly lexicals readonly [perl #19135]";
577 fresh_perl_is(<<'EOP', "ok\nok\nok\n", undef, 'eval clears %^H');
579 require re; re->import('/x'); # should only affect surrounding scope
581 print "a b" =~ /a b/ ? "ok\n" : "nokay\n";
583 print "a b" =~ /a b/ ? "ok\n" : "nokay\n";
586 print "ab" =~ /a b/ ? "ok\n" : "nokay\n";
591 BEGIN { eval 'require re; import re "/x"' }
592 ok "ab" =~ /a b/, 'eval does not localise %^H at run time';
595 # The fix for perl #70151 caused an assertion failure that broke
596 # SNMP::Trapinfo, when toke.c finds no syntax errors but perly.y fails.
598 pass("phew! dodged the assertion after a parsing (not lexing) error");
602 local $ENV{PERL_DESTRUCT_LEVEL} = 1;
605 prog => 'BEGIN { $^H{foo} = bar }'
606 .'our %FIELDS; my main $x; eval q[$x->{foo}]',
609 qr/Unbalanced string table/,
610 'Errors in finalize_optree do not leak string eval op tree';
613 # [perl #114658] Line numbers at end of string eval
615 eval $_; is $@ =~ s/eval \d+/eval 1/rag, <<'EOE',
616 Missing right curly or square bracket at (eval 1) line 1, at end of line
617 syntax error at (eval 1) line 1, at EOF
619 qq'Right line number for eval "$_"';