From: Dominic Dunlop Date: Sun, 15 Jun 1997 23:36:14 +0000 (+1200) Subject: -p does not check for failure of implicit print X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=08e9d68e93f9880dc9a1edcde3abb85026f49784;p=p5sagit%2Fp5-mst-13.2.git -p does not check for failure of implicit print Unlike modern incarnations of, say, awk and sed, perl -p does not check the return status of the implicit print statement executed for each input record. Here's a patch against 5.004_01. ('ware wrapping: there's a long line in it.) There's no test case: I couldn't think up a reliable, portable and polite way of inducing a write error. I think, in the specific case of the implicit print in -p, this is non-controversial, and can go in the maintenance branch. That's not to say that there are not programs using -p out there (probably CGI scripts) which will surprise people by exiting noisily on encountering an error, rather than continuing to do thewrong thing quietly. Does anybody know of any widespread examples? Do we care? More controversial error checking patch for implicit close of to follow in separate bug report. p5p-msgid: v0311070aafea3fa83061@[194.51.248.75] --- diff --git a/pod/perldiag.pod b/pod/perldiag.pod index d3ea144..ab40fd1 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -153,6 +153,12 @@ which provides a race condition that breaks security. (F) Perl can't peek at the stdio buffer of filehandles when it doesn't know about your kind of stdio. You'll have to use a filename instead. +=item C<-p> destination: %s + +(F) An error occurred during the implicit output invoked by the C<-p> +command-line switch. (This output goes to STDOUT unless you've +redirected it with select().) + =item 500 Server error See Server error. @@ -656,8 +662,11 @@ buffer. =item Can't open %s: %s -(S) An inplace edit couldn't open the original file for the indicated reason. -Usually this is because you don't have read permission for the file. +(S) The implicit opening of a file through use of the CE> +filehandle, either implicitly under the C<-n> or C<-p> command-line +switches, or explicitly, failed for the indicated reason. Usually this +is because you don't have read permission for a file which you named +on the command line. =item Can't open bidirectional pipe diff --git a/pod/perlrun.pod b/pod/perlrun.pod index de7116d..61e40f8 100644 --- a/pod/perlrun.pod +++ b/pod/perlrun.pod @@ -376,8 +376,10 @@ B: } Note that the lines are not printed by default. See B<-p> to have -lines printed. Here is an efficient way to delete all files older than -a week: +lines printed. If a file named by an argument cannot be opened for +some reason, Perl warns you about it, and moves on to the next file. + +Here is an efficient way to delete all files older than a week: find . -mtime +7 -print | perl -nle 'unlink;' @@ -396,11 +398,14 @@ makes it iterate over filename arguments somewhat like B: while (<>) { ... # your script goes here } continue { - print; + print or die "-p destination: $!\n"; } -Note that the lines are printed automatically. To suppress printing -use the B<-n> switch. A B<-p> overrides a B<-n> switch. +If a file named by an argument cannot be opened for some reason, Perl +warns you about it, and moves on to the next file. Note that the +lines are printed automatically. An error occuring during printing is +treated as fatal. To suppress printing use the B<-n> switch. A B<-p> +overrides a B<-n> switch. C and C blocks may be used to capture control before or after the implicit loop, just as in awk. diff --git a/toke.c b/toke.c index c5c32bf..f443af7 100644 --- a/toke.c +++ b/toke.c @@ -369,7 +369,9 @@ register char *s; return s; if ((s = filter_gets(linestr, rsfp, (prevlen = SvCUR(linestr)))) == Nullch) { if (minus_n || minus_p) { - sv_setpv(linestr,minus_p ? ";}continue{print" : ""); + sv_setpv(linestr,minus_p ? + ";}continue{print or die qq(-p destination: $!\\n)" : + ""); sv_catpv(linestr,";}"); minus_n = minus_p = 0; }