Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I was referring to being wary when $line=<FH> is used on its own.

Yes, but while takes care of that - and we/you were talking about a while loop, weren't you?

1.    while (<>) {
which is Perl short-hand for the more explicitly written version:
1.    LINE: while (defined($line = <ARGV>)) {

But of course,

We see that there is a possibility that a line read ($line3 in this case) may return undef.

- if you read past EOF, for instance. Then readline returns undef:

open(FH, "<", "a.txt") or die $!; my $line1 = <FH>; my $line2 = <FH>; print "eof!\n" if eof FH; my $line3 = <FH>; close FH; __END__ eof!
That's what I meant: we need to be wary that a line read will not always return defined.

That's exactly the condition when both the while($line = <FH>) { } and while(defined($line = <FH>)) { } loops terminate, so there's no difference in a while loop.

In reply to Re^5: File Reading and Closing confusion by shmem
in thread File Reading and Closing confusion by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2018-05-27 08:09 GMT
    Find Nodes?
      Voting Booth?