This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Magic flags harmonization.
[perl5.git] / t / op / eval.t
index eacc3c5..9866ca7 100644 (file)
@@ -6,7 +6,7 @@ BEGIN {
     require './test.pl';
 }
 
-plan(tests => 118);
+plan(tests => 126);
 
 eval 'pass();';
 
@@ -25,6 +25,11 @@ like($@, qr/line 2/);
 print eval '$foo = /'; # this tests for a call through fatal()
 like($@, qr/Search/);
 
+is scalar(eval '++'), undef, 'eval syntax error in scalar context';
+is scalar(eval 'die'), undef, 'eval run-time error in scalar context';
+is +()=eval '++', 0, 'eval syntax error in list context';
+is +()=eval 'die', 0, 'eval run-time error in list context';
+
 is(eval '"ok 7\n";', "ok 7\n");
 
 $foo = 5;
@@ -432,13 +437,13 @@ is($got, "ok\n", 'eval and last');
 
 {
     no warnings;
-    eval "/ /b;";
+    eval "&& $b;";
     like($@, qr/^syntax error/, 'eval syntax error, no warnings');
 }
 
-# a syntax error in an eval called magically 9eg vie tie or overload)
+# a syntax error in an eval called magically (eg via tie or overload)
 # resulted in an assertion failure in S_docatch, since doeval had already
-# poppedthe EVAL context due to the failure, but S_docatch expected the
+# popped the EVAL context due to the failure, but S_docatch expected the
 # context to still be there.
 
 {
@@ -465,8 +470,10 @@ cmp_ok(length $@, '==', 0, 'length of $@ after eval');
 
 # Check if eval { 1 }; completely resets $@
 SKIP: {
-    skip("Can't load Devel::Peek: $@", 2)
-       unless eval "use Devel::Peek; 1;";
+    skip_if_miniperl('no dynamic loading on miniperl, no Devel::Peek', 2);
+    require Config;
+    skip('Devel::Peek was not built', 2)
+       unless $Config::Config{extensions} =~ /\bDevel\/Peek\b/;
 
     my $tempfile = tempfile();
     open $prog, ">", $tempfile or die "Can't create test file";
@@ -489,9 +496,12 @@ END_EVAL_TEST
 
     is($tombstone, "Done\n", 'Program completed successfully');
 
-    $first =~ s/,pNOK//;
+    $first =~ s/p?[NI]OK,//g;
     s/ PV = 0x[0-9a-f]+/ PV = 0x/ foreach $first, $second;
     s/ LEN = [0-9]+/ LEN = / foreach $first, $second;
+    # Dump may double newlines through pipes, though not files
+    # which is what this test used to use.
+    $second =~ s/ IV = 0\n\n/ IV = 0\n/ if $^O eq 'VMS';
 
     is($second, $first, 'eval { 1 } completely resets $@');
 }
@@ -511,7 +521,7 @@ END_EVAL_TEST
     # test that the CV compiled for the eval is freed by checking that no additional 
     # reference to outside lexicals are made.
     my $x;
-    is(Internals::SvREFCNT($x), 1, "originally only 1 referece");
+    is(Internals::SvREFCNT($x), 1, "originally only 1 reference");
     eval '$x';
     is(Internals::SvREFCNT($x), 1, "execution eval doesn't create new references");
 }
@@ -562,3 +572,40 @@ for my $k (!0) {
   is "a" =~ /a/, "1",
     "string eval leaves readonly lexicals readonly [perl #19135]";
 }
+
+# [perl #68750]
+fresh_perl_is(<<'EOP', "ok\nok\nok\n", undef, 'eval clears %^H');
+  BEGIN {
+    require re; re->import('/x'); # should only affect surrounding scope
+    eval '
+      print "a b" =~ /a b/ ? "ok\n" : "nokay\n";
+      use re "/m";
+      print "a b" =~ /a b/ ? "ok\n" : "nokay\n";
+   ';
+  }
+  print "ab" =~ /a b/ ? "ok\n" : "nokay\n";
+EOP
+
+# [perl #70151]
+{
+    BEGIN { eval 'require re; import re "/x"' }
+    ok "ab" =~ /a b/, 'eval does not localise %^H at run time';
+}
+
+# The fix for perl #70151 caused an assertion failure that broke
+# SNMP::Trapinfo, when toke.c finds no syntax errors but perly.y fails.
+eval(q|""!=!~//|);
+pass("phew! dodged the assertion after a parsing (not lexing) error");
+
+# [perl #111462]
+{
+   local $ENV{PERL_DESTRUCT_LEVEL} = 1;
+   unlike
+     runperl(
+      prog => 'BEGIN { $^H{foo} = bar }'
+             .'our %FIELDS; my main $x; eval q[$x->{foo}]',
+      stderr => 1,
+     ),
+     qr/Unbalanced string table/,
+    'Errors in finalize_optree do not leak string eval op tree';
+}