This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #37725] perl segfaults on reversed array reference
[perl5.git] / t / op / anonsub.t
index 17889d9..970440b 100755 (executable)
@@ -1,16 +1,22 @@
 #!./perl
 
+# Note : we're not using t/test.pl here, because we would need
+# fresh_perl_is, and fresh_perl_is uses a closure -- a special
+# case of what this program tests for.
+
 chdir 't' if -d 't';
 @INC = '../lib';
 $Is_VMS = $^O eq 'VMS';
 $Is_MSWin32 = $^O eq 'MSWin32';
+$Is_MacOS = $^O eq 'MacOS';
+$Is_NetWare = $^O eq 'NetWare';
 $ENV{PERL5LIB} = "../lib" unless $Is_VMS;
 
 $|=1;
 
 undef $/;
 @prgs = split "\n########\n", <DATA>;
-print "1..", scalar @prgs, "\n";
+print "1..", 6 + scalar @prgs, "\n";
 
 $tmpfile = "asubtmp000";
 1 while -f ++$tmpfile;
@@ -24,12 +30,16 @@ for (@prgs){
     my($prog,$expected) = split(/\nEXPECT\n/, $_);
     open TEST, ">$tmpfile";
     print TEST "$prog\n";
-    close TEST;
+    close TEST or die "Could not close: $!";
     my $results = $Is_VMS ?
-                  `MCR $^X "-I[-.lib]" $switch $tmpfile 2>&1` :
-                     $Is_MSWin32 ?  
-                         `.\\perl -I../lib $switch $tmpfile 2>&1` :
-                             `./perl $switch $tmpfile 2>&1`;
+               `$^X "-I[-.lib]" $switch $tmpfile 2>&1` :
+                 $Is_MSWin32 ?
+                   `.\\perl -I../lib $switch $tmpfile 2>&1` :
+                     $Is_MacOS ?  
+                       `$^X -I::lib $switch $tmpfile` :
+                           $Is_NetWare ?
+                               `perl -I../lib $switch $tmpfile 2>&1` :
+                                   `./perl $switch $tmpfile 2>&1`;
     my $status = $?;
     $results =~ s/\n+$//;
     # allow expected output to be written as if $prog is on STDIN
@@ -45,6 +55,30 @@ for (@prgs){
     print "ok ", ++$i, "\n";
 }
 
+sub test_invalid_decl {
+    my ($code,$todo) = @_;
+    $todo //= '';
+    eval $code;
+    if ($@ =~ /^Illegal declaration of anonymous subroutine at/) {
+       print "ok ", ++$i, " - '$code' is illegal$todo\n";
+    } else {
+       print "not ok ", ++$i, " - '$code' is illegal$todo\n# GOT: $@";
+    }
+}
+
+test_invalid_decl('sub;');
+test_invalid_decl('sub ($) ;');
+test_invalid_decl('{ $x = sub }');
+test_invalid_decl('sub ($) && 1');
+test_invalid_decl('sub ($) : lvalue;',' # TODO');
+
+eval "sub #foo\n{print 1}";
+if ($@ eq '') {
+    print "ok ", ++$i, "\n";
+} else {
+    print "not ok ", ++$i, "\n# GOT: $@";
+}
+
 __END__
 sub X {
     my $n = "ok 1\n";
@@ -91,3 +125,7 @@ sub X {
 X();
 EXPECT
 ok 1
+########
+print sub { return "ok 1\n" } -> ();
+EXPECT
+ok 1