Minor tweak to allow reading of a line containing "0" :

while (defined(my $line = <$in>)) { chomp $line; # Zap trailing "\n" efficiently print $out $line,"\r"; }

Not required:

#!/usr/bin/perl use strict; use warnings; use autodie; open my $out,'>:raw',"file.tmp"; print $out "a line\na second line\n0"; close $out; open my $in,'<:raw',"file.tmp"; while (my $line=<$in>) { chomp $line; print "read: $line\n"; } close $in;
>perl foo.pl read: a line read: a second line read: 0 >

Any line but the last in a file always contains a trailing newline, so $line is guaranteed to be true for all but the last line, and reading empty lines and lines containing only "0" is no problem for all but the last line:

>perl -E '"0\n" and say "true"' true >perl -E '"\n" and say "true"' true >perl -E '"0" or say "false"' false > perl -E '"" or say "false"' false >perl -E 'undef or say "false"' false >

The last line WOULD BE a problem if perl would check for truth. But in the special case of readline a.k.a. <HANDLE>, it checks for definedness, not for truth. Quoting perlfunc:

readline EXPR

readline

Reads from the filehandle whose typeglob is contained in EXPR (or from *ARGV if EXPR is not provided). In scalar context, each call reads and returns the next line until end-of-file is reached, whereupon the subsequent call returns undef. [...]

This is the internal function implementing the <EXPR> operator, but you can use it directly.

[...]If either a readline expression or an explicit assignment of a readline expression to a scalar is used as a while/for condition, then the condition actually tests for definedness of the expression's value, not for its regular truth value.

(Emphasis mine)

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: Why Is Writing to a Binary File so Hard? by afoken
in thread Why Is Writing to a Binary File so Hard?[SOLVED] by jmlynesjr

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.