Explaining the redo operator, The Camel (3rd edititon, page 122/top) presents the following code snippet:
# Taken from The Camel, 3rd ed. while (<>) { chomp; if (s/\\//) { $_ .= <>; redo unless eof; # don't read past each file's eof } # now proces $_ }
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):
# file_1 (this line doesn't belong to the file's content) Line one\ Line two
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_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
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:
# My code while (<>) { chomp; if (s/\\//) { unless (eof) { # don't read past each file's eof $_ .= <>; redo; } } # now process $_ }
Thus making code and comment correlate and prevent $_ from containing some strings that are chomped and some that are not.

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.


In reply to Comment not matching code in Programming Perl by jsrn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.