croak on an attempt to run a directory as a script
authorRicardo Signes <rjbs@cpan.org>
Sun, 6 Jan 2013 01:30:48 +0000 (20:30 -0500)
committerRicardo Signes <rjbs@cpan.org>
Sun, 13 Jan 2013 00:39:26 +0000 (19:39 -0500)
How many times have I meant to run "perl -I lib myprog" but instead
run "perl lib myprog" only to exit 0 because that's what perl does
when you try to run a directory as a script (at least on unix)?  Many.

perl should croak when instructed to execute a directory.
[perl #61362] suggests it already does so on Win32.  Now it does it
everywhere.  Tests not yet written.

perl.c

diff --git a/perl.c b/perl.c
index c7e1d54..d4f13df 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -3649,6 +3649,7 @@ S_open_script(pTHX_ const char *scriptname, bool dosearch, bool *suidscript)
     int fdscript = -1;
     PerlIO *rsfp = NULL;
     dVAR;
+    Stat_t tmpstatbuf;
 
     PERL_ARGS_ASSERT_OPEN_SCRIPT;
 
@@ -3758,6 +3759,13 @@ S_open_script(pTHX_ const char *scriptname, bool dosearch, bool *suidscript)
     /* ensure close-on-exec */
     fcntl(PerlIO_fileno(rsfp), F_SETFD, 1);
 #endif
+
+    if (PerlLIO_fstat(PerlIO_fileno(rsfp), &tmpstatbuf) >= 0
+        && S_ISDIR(tmpstatbuf.st_mode))
+        Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n",
+            CopFILE(PL_curcop),
+            strerror(EISDIR));
+
     return rsfp;
 }