warn on -i with no input files given [perl #113410]
authorJesse Luehrs <doy@tozt.net>
Tue, 26 Jun 2012 07:16:57 +0000 (02:16 -0500)
committerJesse Luehrs <doy@tozt.net>
Tue, 26 Jun 2012 07:45:05 +0000 (02:45 -0500)
dist/B-Deparse/t/deparse.t
perl.c
pod/perldiag.pod
t/run/switches.t

index 5085a5e..04d64df 100644 (file)
@@ -104,6 +104,7 @@ my $path = join " ", map { qq["-I$_"] } @INC;
 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
 $a =~ s/-e syntax OK\n//g;
 $a =~ s/.*possible typo.*\n//;    # Remove warning line
+$a =~ s/.*-i used with no filenames.*\n//;     # Remove warning line
 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
 $b = <<'EOF';
diff --git a/perl.c b/perl.c
index ae4390e..9496f5c 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -4150,6 +4150,11 @@ Perl_init_argv_symbols(pTHX_ register int argc, register char **argv)
                 (void)sv_utf8_decode(sv);
        }
     }
+
+    if (PL_inplace && (!PL_argvgv || AvFILL(GvAV(PL_argvgv)) == -1))
+        Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
+                         "-i used with no filenames on the command line, "
+                         "reading from STDIN");
 }
 
 STATIC void
index 57be1d5..4260634 100644 (file)
@@ -2109,6 +2109,16 @@ Further error messages would likely be uninformative.
 (4294967295) and therefore non-portable between systems.  See
 L<perlport> for more on portability concerns.
 
+=item -i used with no filenames on the command line, reading from STDIN
+
+(S inplace) The C<-i> option was passed on the command line, indicating
+that the script is intended to edit files inplace, but no files were
+given. This is usually a mistake, since editing STDIN inplace doesn't
+make sense, and can be confusing because it can make perl look like it
+is hanging when it is really just trying to read from STDIN.  You should
+either pass a filename to edit, or remove C<-i> from the command line.
+See L<perlrun> for more details.
+
 =item Identifier too long
 
 (F) Perl limits identifiers (names for variables, functions, etc.) to
index 57ae324..5c3b5ee 100644 (file)
@@ -11,7 +11,7 @@ BEGIN {
 
 BEGIN { require "./test.pl"; }
 
-plan(tests => 112);
+plan(tests => 114);
 
 use Config;
 
@@ -350,6 +350,26 @@ __EOF__
     is(join(":", @bak),
        "foo yada dada:bada foo bing:king kong foo",
        "-i backup file");
+
+    my $out1 = runperl(
+        switches => ['-i.bak -p'],
+        prog     => 'exit',
+        stderr   => 1,
+        stdin    => "1\n",
+    );
+    is(
+        $out1,
+        "-i used with no filenames on the command line, reading from STDIN.\n",
+        "warning when no files given"
+    );
+    my $out2 = runperl(
+        switches => ['-i.bak -p'],
+        prog     => 'exit',
+        stderr   => 1,
+        stdin    => "1\n",
+        args     => ['file'],
+    );
+    is($out2, "", "no warning when files given");
 }
 
 # Tests for -E