jsrn has asked for the wisdom of the Perl Monks concerning the following question:
What's the effect of the redo unless eof statement? Certainly it's *not* preventing the code from reading past each file's eof (as the first comment suggests). Consider an input file like this (presuming that the trailing visible character of each line of the file is immedeately followed by a newline):# Taken from The Camel, 3rd ed. while (<>) { chomp; if (s/\\//) { $_ .= <>; redo unless eof; # don't read past each file's eof } # now proces $_ }
Packaging the above code snippet in a file named readbackslash.pl, making this file executable (not forgetting to insert the shebang, of course) and executing it with the input file as an argument ./readbackslash.pl file_1 has the following effect: After the conditional of the while loop has been evaluated, $_ has the value "Line one\\n". It is then chomped and the trailing backslash is removed. The next line is appended, thus $_ now contains "Line oneLine two\n". Reading once more from file_1 would return end-of-file, therefore eof returns true, thus the loop is not redone, meaning that $_ is not chomped once more, the newline remains at its place. Thus, the effect of the redo unless eof statement is to prevent the chomping of the last newline of the file, if the line before (the "second last line") has a trailing backslash in it. *This* is the effect of the redo unless eof statement. It *does not* prevent from crossing file boundaries, just consider the following two input files:# file_1 (this line doesn't belong to the file's content) Line one\ Line two
Executing the script as readbackslash.pl file_2 file_3, $_ successively becomes "Line three", "Line fourLine five" (file bounds clearly crossed), "Line six". IMHO, the code as presented in The Camel is not very useful (but, on the other hand, as I'm not very experienced yet, I could easily have missed something). I would rewrite the above snippet as:# file_2 (this line doesn't belong to the file's content) Line three Line four\ # file_3 (this line doesn't belong to the file's content) Line five Line six
Thus making code and comment correlate and prevent $_ from containing some strings that are chomped and some that are not.# My code while (<>) { chomp; if (s/\\//) { unless (eof) { # don't read past each file's eof $_ .= <>; redo; } } # now process $_ }
I looked at the errata page of The Camel but I didn't find anything concerning this issue.
Any explanations -- Or have I completely missed something?
Aside: I have looked through the perlsyn manpage and found the same example as in The Camel , except that instead of eof, eof() is used. AFAIK, this reveals the same behaviour as described above (leaving the last line of the file occasionally un-chomped), but only in the last file in @ARGV.
Jonas
Edited 2001-01-21 by Ovid. View source to see details.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comment not matching code in a href
by trs80 (Priest) on Jan 22, 2002 at 06:10 UTC | |
|
Re: Comment not matching code in a href
by goldclaw (Scribe) on Jan 22, 2002 at 04:00 UTC |